I want to send email using Javascript/jQuery using REST API in SharePoint.

I have tried _api/SP.Utilities.Utility.SendEmail and this is my code:

Code

$.ajax({
        contentType: 'application/json',
        url: urlEmail,
        type: "POST",
        data: JSON.stringify({
                  'properties': {
                  '__metadata': { 'type': 'SP.Utilities.EmailProperties' },
                  'Body': 'Hello',
                  'To' : { 'results': ['kaushal.khamar@xxxxxx.com'] },
                  'Subject': "From REST API"
                }
            }),
        headers: {
                    "Accept": "application/json;odata=verbose",
                    "content-type": "application/json;odata=verbose",
                    "X-RequestDigest": $("#__REQUESTDIGEST").val()
                },
        success: function (data) {
                alert("Successful");
                },
        error: function (err) {
                alert(err.responseText);
                }
        });

Error

The e-mail message cannot be sent. Make sure the e-mail has a valid recipient.

  • Please add your code block to get more clarity of your exception. – Mancy Desaee Jul 6 '15 at 7:27
  • 2
    Reason of which i am getting while sending emails is i have added Email address of mine which is not included in any sharepoint user. Thats why i am getting error like "The e-mail message cannot be sent. Make sure the e-mail has a valid recipient". So whenever try to send mails make sure that email should be email address one of the users of sharepoint. – Kaushal Khamar Jul 7 '15 at 8:47
  • @KaushalKhamar: how about not advertising your own blog which contains the same information as posted here? – Alexxus Dec 20 '17 at 10:36
up vote 31 down vote accepted

In SharePoint 2013 On Premise we can send emails using REST API where we can utilise SP.Utilities.Utility.SendEmail for doing the job.

Note: The recipient is limited to a valid SharePoint user for security reasons.

function processSendEmails() {

    var from = 'asad@Example.com',
        to = 'someone@Example.com',
        body = 'Hello World Body',
        subject = 'Hello World Subject';

    // Call sendEmail function
    //
    sendEmail(from, to, body, subject);
}


function sendEmail(from, to, body, subject) {
    //Get the relative url of the site
    var siteurl = _spPageContextInfo.webServerRelativeUrl;
    var urlTemplate = siteurl + "/_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": jQuery("#__REQUESTDIGEST").val()
        },
        success: function(data) {
            alert('Email Sent Successfully');
        },
        error: function(err) {
            alert('Error in sending Email: ' + JSON.stringify(err));
        }
    });
}

$(document).ready(function () {

    SP.SOD.executeFunc('sp.js', 'SP.ClientContext', processSendEmails);

});
  • Hi, I used _spPageContextInfo.userLoginName to get the current logged in user email address. It worked, but when I see the mail it shows From value as "no-reply@sharepointonline.com". How can I get actual email address here? – Trimantra Software Solution Nov 10 '16 at 9:53
  • You mean to say, you used var from = spPageContextInfo.userLoginName, in the function. but the From address shows "no-reply@sharepointonline.com"? – Asad Refai Nov 10 '16 at 9:58
  • yes...you got the right. – Trimantra Software Solution Nov 10 '16 at 10:03
  • I am afraid I have not tried it in SPO. – Asad Refai Nov 10 '16 at 11:30
  • i want to send HTML in body example:- var body='<html><body><a>click here</a></body></html>'; how we can do this?@AsadRefai – Savan Gupta Jan 18 '17 at 14:36

Please refer this link for for detailed code, you shall realize the issue.

This link is also useful.

Also: Make sure that the recipient is limited to a valid SharePoint user for security reasons.

Info: You have to provide the sharepoint Id for the specific user in order to send this user an email!

The following is the code:

function sendEmail(from, to, body, subject) {
//Get the relative url of the site
var siteurl = _spPageContextInfo.webServerRelativeUrl;
var urlTemplate = siteurl + "/_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": jQuery("#__REQUESTDIGEST").val()
    },
    success: function(data) {
        alert('Email Sent Successfully');
    },
    error: function(err) {
        alert('Error in sending Email: ' + JSON.stringify(err));
    }
});
}

Please make sure that the email recipient is in the same domain as SP server.

We're looking for long answers that provide some explanation and context. Don't just give a one-line answer; explain why your answer is right, ideally with citations. Answers that don't include explanations may be removed.

  • i have go through this article and implemented same but getting error like "The e-mail message cannot be sent. Make sure the e-mail has a valid recipient". I dont know why this error occur. Send to email id is valid one. still getting error. – Kaushal Khamar Jul 6 '15 at 8:56
  • Try one thing - rather than writing the email id leave it as 'results': [to] abd check what happens. I feel it is this line that is giving error. – Mancy Desaee Jul 6 '15 at 8:59
  • I have tried this but still not working... – Kaushal Khamar Jul 6 '15 at 9:03
  • This work now. Thanks for help...!! – Kaushal Khamar Jul 6 '15 at 9:26
  • Can you add details from the link in your answer? Link only answers become invalid should the links break. – wjervis Jul 6 '15 at 12:48

You can use the function I have added and call it like

sendEmail("domain\\sender", "domain\\recipient","This is the body","Mail Subject");:


    var hostweburl;
    var appweburl;

    $(document).ready(function () {
        SP.SOD.executeFunc('sp.js', 'SP.ClientContext', sendEmail);
    });
    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));
        }
    });
    }

This blog is explains the issue very well.

Summary of the blog:

What that MSDN define as

valid SharePoint user email addresses

means:

a user that has or had permissions in this site and then belong to All People SharePoint Hidden Group.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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