26

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': ['[email protected]'] },
                  '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.

3
  • Please add your code block to get more clarity of your exception. Commented Jul 6, 2015 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. Commented Jul 7, 2015 at 8:47
  • @KaushalKhamar: how about not advertising your own blog which contains the same information as posted here?
    – Alexxus
    Commented Dec 20, 2017 at 10:36

6 Answers 6

39

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 = '[email protected]',
        to = '[email protected]',
        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);

});
17
  • 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 "[email protected]". How can I get actual email address here? Commented Nov 10, 2016 at 9:53
  • 1
    You mean to say, you used var from = spPageContextInfo.userLoginName, in the function. but the From address shows "[email protected]"?
    – Asad Refai
    Commented Nov 10, 2016 at 9:58
  • yes...you got the right. Commented Nov 10, 2016 at 10:03
  • I am afraid I have not tried it in SPO.
    – Asad Refai
    Commented Nov 10, 2016 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 Commented Jan 18, 2017 at 14:36
5

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.

5
  • 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. Commented Jul 6, 2015 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. Commented Jul 6, 2015 at 8:59
  • I have tried this but still not working... Commented Jul 6, 2015 at 9:03
  • Can you add details from the link in your answer? Link only answers become invalid should the links break.
    – wjervis
    Commented Jul 6, 2015 at 12:48
  • Error in sending Email: {"readyState":0,"responseText":"","status":0,"statusText":"error"} . I was getting this error when i changed this =_spPageContextInfo.webAbsoluteUrl to =_spPageContextInfo.webServerRelativeUrl
    – OM-ॐ
    Commented Jun 17, 2020 at 7:39
2

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));
        }
    });
    }
1

If you wan to send e-mails outside the tenant, to any email, you can do a workaround with MS Flow.

Trigger it with HTTP REST Call from your SharePoint, with input parameters: recepient, subject, email body.

and add a send e-mail action to your flow.

See: https://medium.com/@zaab_it/microsoft-flow-send-email-from-http-request-f6577ad46b2c

0

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.

0

I'm a bit late to the game on this, but hopefully this can help someone else with this headbanging error who's scouring for an answer -- "The e-mail message cannot be sent. Make sure the e-mail has a valid recipient." -- persists even when sent to valid recipients within the same SharePoint domain from within the same server (for instance, if sent to an MS Exchange Shared Mailbox - as was my experience).

After some research, I was able to find a purely front-end solution to overcome this issue (because every other forum solution for the most part proposed SPD, but I was looking to stick to strictly CSOM/JSOM.

Anyways, the solution is to add the Shared Mailbox's email address to an existing "Members" SP group within the site collection (used in conjunction within user Asad Refai's REST API call, and emails will send to that particular Shared Mailbox). As soon as I did this, the "Valid recipient" error subsided and emails were received to the Shared Mailbox from the server.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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