1
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript" src="https://yyyyy.xxxx.org/_layouts/15/sp.runtime.js"></script>
<script type="text/javascript" src="https://yyyyy.xxxx.org/_layouts/15/sp.js"></script>

<script>

function processSendEmails() 
{

 var from = '[email protected]',
        to = '[email protected]',
        body = 'Hello World Body',
        subject = 'Hello World Subject';

      sendEmail(from, to, body, subject);
}

function sendEmail(from, to, body, subject) {


  var urlTemplate = "https://yyyyy.xxxx.org" + "/_api/SP.Utilities.Utility.SendEmail";

   //alert("api " + urlTemplate);

    $.ajax({
        contentType:'application/json; charset=utf-8',
    dataType: 'json',
        url: urlTemplate,
    crossDomain: true,  
        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);

});
</script>

Error in sending Email:

{"readyState":4,"responseText":"{\"error\":{\"code\":\"-2130242040, Microsoft.SharePoint.SPException\",\"message\":{\"lang\":\"en-US\",\"value\":\

"The e-mail message cannot be sent. Make sure the e-mail has a valid recipient.\"}}}","status":400,"statusText":"Bad Request"}

3
  • SPUtility limitation: The recipient is limited to a valid SharePoint user for security reasons.the email recipient must be in the same domain as the SP server. Commented Dec 29, 2016 at 6:04
  • Have you tried to open the Rest API url ("sharepointsite/_api/SP.Utilities.Utility.SendEmail") in browser? Please check that. Commented Dec 29, 2016 at 6:26
  • 2
    SharePoint code can't mail to non AD users, otherwise it would be the greatest Spamming tool in the world and Microsoft shares would plummet. So put accounts in the AD, or call your own (non SharePoint) SMTP server from a proxy Commented Dec 29, 2016 at 8:47

0

Your Answer

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

Browse other questions tagged or ask your own question.