Question

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.

Was it helpful?

Solution

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

});

OTHER TIPS

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.

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.

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

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top