3

I've been trying to send an email over REST using /_api/SP.Utilities.Utility.SendEmai, but seems like the emails just never arrive.

Current setup

  • SharePoint Online
  • I send emails to myself and to other internal mailboxes within office 365 tenant
  • When I type a nonexistent email the script fails with an error. And if I use the correct one it is sent successfully, but it just never arrives
  • I tried several ways to send emails with and without AdditionalHeaders, but nothing seems to work

Can someone help me, please, to figure out what I could be doing wrong?

This is the code I've been using:

var subject = "SUBJECT OF THE MAIL";
var mailContent = "Some Heading for the mail Content Content";
var toList = ["correctUser@domain.com"]

//Send email message over REST
function sendMail(toList, subject, mailContent) {
    var restUrl = _spPageContextInfo.webAbsoluteUrl +     "/_api/SP.Utilities.Utility.SendEmail",
restHeaders = {
    "Accept": "application/json;odata=verbose",
    "X-RequestDigest": $("#__REQUESTDIGEST").val(),
    "Content-Type": "application/json;odata=verbose"
},
mailObject = {
    'properties': {
        '__metadata': {
            'type': 'SP.Utilities.EmailProperties'
        },
        'To': {
            'results': toList
        },
        'Subject': subject,
        'Body': mailContent,
        "AdditionalHeaders":
            {
                "__metadata":
                   { "type": "Collection(SP.KeyValue)" },
                "results":
                [
                    {
                        "__metadata": {
                            "type": 'SP.KeyValue'
                        },
                        "Key": "content-type",
                        "Value": 'text/html',
                        "ValueType": "Edm.String"
                    }
                ]
            }

    }
};
return $.ajax({
    contentType: "application/json",
    url: restUrl,
    type: "POST",
    data: JSON.stringify(mailObject),
    headers: restHeaders
});

} 

$(function(){
    sendMail(toList, subject, mailContent).then(function(data)    {console.log(data.d)})
})
1

Turns out, the account I was using when sending these messages did not have Exchange Online license assigned. So, essentially, I need to have a mailbox to send emails via SendEmail service.

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.