0
votes

I have created a SharePoint Hosted app which uses a visual studio workflow to send email within same domain.

Since I am in process of migrating custom aspx forms in SP2010 to SharePoint Online, each form is redeveloped as a SP Hosted app and I don't want to include a workflow every time to send email.

Is there any other workaround to handle email in SP Hosted app?

Thanks!

1

1 Answers

1
votes

You can use javascript REST API to send email from hosted app. See the code below:

var urlTemplate = SPAppWebUrl + "/_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': emailBody,
            'Subject': subject
        }
    }
  ),
    headers: {
        "Accept": "application/json;odata=verbose",
        "content-type": "application/json;odata=verbose",
        "X-RequestDigest": $("#__REQUESTDIGEST").val()
    },
    success: function (data) {
        //console.log('success')
        alert('email send successfull..');
    },
    error: function (err) {
        //console.log(JSON.stringify(err));
        alert(JSON.stringify(err));
    }
});