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.

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));
    }
});
}
| improve this answer | |
  • 2
    Is it can work on on-premise SharePoint? – tola Mar 5 '14 at 3:02
  • I have used this for sharepoint hosted apps yes – Glenn Vandamme Mar 5 '14 at 8: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 6 '14 at 2:20
  • 1
    Make sure your outgoing mail settings are configured in Sharepoint General Administration > System Settings > Configure outgoing email. – Glenn Vandamme Mar 6 '14 at 8:32
  • 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 5 '15 at 9:46
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.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.