12

Did you know how can I send an email in my SharePoint Hosted apps? currently I am create a visual studio 2012 project for SharePoint 2013 foundation hosted apps with JavaScript code. I do not be able to send an email while user submit my request from. Please help, and kindly to comment or post for more detail information. noted that i need to use javascript and the REST API to send and email from a hosted app Thanks.

tola
  • 363
  • 2
  • 4
  • 12

2 Answers2

21

This will only mail to users inside your SharePoint for security reasons, but does the job:

function sendEmail(from, to, body, subject) {

appweburl = decodeURIComponent(getQueryStringParameter('SPAppWebUrl'));
hostweburl = decodeURIComponent(getQueryStringParameter('SPHostUrl'));
var urlTemplate = appweburl + "/_api/SP.Utilities.Utility.SendEmail";
$.ajax({
    contentType: 'application/json',
    url: urlTemplate,
    type: "POST",
    data: JSON.stringify({
        'properties': {
            '__metadata': { 'type': 'SP.Utilities.EmailProperties' },
            'From': from,
            'To': { 'results': [to] },
            'Body': body,
            'Subject': subject
        }
    }
  ),
    headers: {
        "Accept": "application/json;odata=verbose",
        "content-type": "application/json;odata=verbose",
        "X-RequestDigest": $("#__REQUESTDIGEST").val()
    },
    success: function (data) {
       console.log('success')
    },
    error: function (err) {
        console.log(JSON.stringify(err));
    }
});
}
Keerthi
  • 1,113
  • 2
  • 15
  • 32
Glenn Vandamme
  • 663
  • 6
  • 17
  • 2
    Is it can work on on-premise SharePoint? – tola Mar 05 '14 at 03:02
  • I have used this for sharepoint hosted apps yes – Glenn Vandamme Mar 05 '14 at 08:49
  • 1
    I have test with O365 it working, but on my local share point seem not working, and on sharepoint hosted app client webpart also not working. I don't know y... – tola Mar 06 '14 at 02:20
  • 1
    Make sure your outgoing mail settings are configured in Sharepoint General Administration > System Settings > Configure outgoing email. – Glenn Vandamme Mar 06 '14 at 08:32
  • Yes, I configure with smpt.gmail.com and smtp working properly.. – tola Mar 06 '14 at 08:45
  • I got this error message on O365 Client webpart, and really really don't know y https://docs.google.com/file/d/0B9PMJN3pTJsSNVRQdlhxdmlxV0E/edit pls help – tola Mar 06 '14 at 08:54
  • This worked for me Thank you very much . Is there any way to send meeting requests using this API? – Shekar Reddy Jun 09 '14 at 11:56
  • Thanks for your code. It was very helpful. Can you please tell me where we can find this getQueryStringParameter('SPAppWebUrl'). ? We have to explicitly define that or is it in built function ? Thanks in Advance –  Aug 27 '14 at 19:53
  • getQueryStringParameter() is a function that comes with the standard apps by default. If you started from scratch you can just copy paste it from here: function getQueryStringParameter(paramToRetrieve) { var params = document.URL.split("?")[1].split("&"); for (var i = 0; i < params.length; i = i + 1) { var singleParam = params[i].split("="); if (singleParam[0] == paramToRetrieve) return singleParam[1]; } } – Glenn Vandamme Sep 03 '14 at 01:30
  • Can a meeting request can be sent through same approach? – ateet Nov 03 '14 at 06:07
  • 1
    it really helpfull and working on sharepoint hosted apps. But it not working on sharePoint hosted apps Client webpart (it may permission cause). Any advise please share thanks. – tola Feb 05 '15 at 09:46
  • I have the same problem as tola. I use the CSOM via C# and the E-Mail sending function only works on Office 365 SharePoints. On on-premises SharePoints it causes an Access Violation Exception. Is there any tip how to solve this? – Vinz Apr 01 '15 at 17:10
  • It will work if you try to send it on Apps Page, not on Client Webpart. – tola May 22 '15 at 04:22
0

This works on Prem SharePoint, SharePoint online both Web and Hosted APP also. If any error issue mostly with exchange/Email server send email policy. Some organization don't allow send email using program, user has to authenticated before sending email. But it works for internal email. That's sure.