Send mail using SharePoint REST API in jQuery/JavaScript:

Send mail using SharePoint REST API in jQuery/JavaScript:

We can able to send email to the SharePoint user using below REST API on SharePoint Online.

We can able to send email to valid SharePoint user of same organization.


     window.getErrorMessage = function (err, listText) {

      var errorMessage = 'An error occurred. Please try again.';

      console.log(err);

      if (err.responseJSON != undefined && err.responseJSON.error != undefined && err.responseJSON.error.message != undefined) {

        errorMessage = err.responseJSON.error.message.value;

      } else {

        if (err.readyState == 0) {

          errorMessage = 'Your browser is not connected with target Server (or Check your internet connection).';

        } else {

          errorMessage = errorMessage;

        }

      }

      if (errorMessage != undefined && errorMessage.indexOf("The field or property") > -1 && listText != undefined && listText != "") {

        errorMessage = errorMessage.replace(".", "") + " in " + listText + ' list.';

      }

      return errorMessage;

    }  


     var mailAPI = _spPageContextInfo.webAbsoluteUrl + "/_api/SP.Utilities.Utility.SendEmail";

        $.ajax({

          contentType: 'application/json',

          url: mailAPI,

          async:false,

          type: "POST",

          data: JSON.stringify({

            'properties': {

              '__metadata': { 'type': 'SP.Utilities.EmailProperties' },//MetatData for Sending mail on SharePoint

              'From': 'admin@myorganization.onmicrosoft.com',//Email is Sending from

              'BCC': { 'results': ['user2@outlook.com'] },//blind carbon copy receipt of the mail

              'CC': { 'results': ['user3@outlook.com'] },//carbon copy receipt of the mail

              'To': { 'results': ['rajparam@myorganization.com'] }, //main receipt of the mail

              'Body': "<p>Hi Guys</p><p>Welcome to New World</p>",// Our main mail message as html format 

              'Subject': 'Testing Mail' //Subject of the mail

            }

          }

          ),

          headers: {

            "Accept": "application/json;odata=verbose",

            "content-type": "application/json;odata=verbose",

            "X-RequestDigest": $("#__REQUESTDIGEST").val()

          },

          success: function (data) {

            console.log(data);

            console.log('Email Sent Successfully');

          },

          error: function (e) {

            console.log(e);

            var defaultMessage = "Something went worng Please try again !!";

            var errorMessage = getErrorMessage(e);

            if (errorMessage != undefined && errorMessage.indexOf("The security validation for this page is invalid") > -1) {

              errorMessage = errorMessage;

            } else if (errorMessage != undefined && errorMessage.indexOf("The e-mail message cannot be sent") > -1) {

              errorMessage = errorMessage;

            } else {

              errorMessage = defaultMessage;

            }

            console.log(errorMessage);

          }

        });




Output: 

Users of the mail (rajparam@myorganization.com, user2@outlook.com and user3@outlook.com) will receive mail as shown below.














Comments

Popular posts from this blog

Get list item entity type full name of a SharePoint list using REST API

Get SharePoint list name by GUID

Get Current Web Logged In User of SharePoint Site in jQuery