In this post, I will describe how to send an eMail using REST API from within a SharePoint hosted App.
Note: This code works in both on-prem and Office 365, but however there is one restriction that the recipient must be a valid SharePoint user for security reason.
Introduction
The REST API in SharePoint 2013 provides developers with a simple standardised method of accessing information contained within SharePoint. It can be used from any technology that is capable of sending standard http requests and is particularly useful for developers who are not familiar with the Client Side Object Model.
For a full list of advantages and disadvantages of the REST API, and for a comparison with other API’s click here.
You should be able to send an email from a hosted app, if you have the mail server set up correctly in SharePoint Foundation. Check out this link on how to Configure outgoing email for a SharePoint 2013 farm.
REST URI Operators
Once you have the correct base URI the next step is to determine the correct method for sending mail. To send a mail, SharePoint REST API exposes the following method 'SendEmail' as an endpoint which is in the class SP.Utilities.Utility.
The example below is to send a mail using ajax call.
var urlTemplate =_spPageContextInfo.webAbsoluteUrl + "/_api/SP.Utilities.Utility.SendEmail";
$.ajax({
contentType: 'application/json',
url: urlTemplate,
type: "POST",
data: JSON.stringify({
'properties': {
'__metadata': { 'type': 'SP.Utilities.EmailProperties' },
'From': 'Hemanth.Anumolu@somewhere.com',
'To': { 'results': ['Test@somewhere.com'] },
'Body': '<h1>Hello!!</h1><p>This is mail was sent from a client side function</p>',
'Subject':'Send eMail'
}
}
),
headers: {
"Accept": "application/json;odata=verbose",
"content-type": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val()
},
success: function (data) {
alert("eMail sent successfully.");
},
error: function (err) {
alert(JSON.stringify(err));
}
});
Hope this post helps you all.
Note: This code works in both on-prem and Office 365, but however there is one restriction that the recipient must be a valid SharePoint user for security reason.
Introduction
The REST API in SharePoint 2013 provides developers with a simple standardised method of accessing information contained within SharePoint. It can be used from any technology that is capable of sending standard http requests and is particularly useful for developers who are not familiar with the Client Side Object Model.
For a full list of advantages and disadvantages of the REST API, and for a comparison with other API’s click here.
You should be able to send an email from a hosted app, if you have the mail server set up correctly in SharePoint Foundation. Check out this link on how to Configure outgoing email for a SharePoint 2013 farm.
REST URI Operators
Once you have the correct base URI the next step is to determine the correct method for sending mail. To send a mail, SharePoint REST API exposes the following method 'SendEmail' as an endpoint which is in the class SP.Utilities.Utility.
The example below is to send a mail using ajax call.
var urlTemplate =_spPageContextInfo.webAbsoluteUrl + "/_api/SP.Utilities.Utility.SendEmail";
$.ajax({
contentType: 'application/json',
url: urlTemplate,
type: "POST",
data: JSON.stringify({
'properties': {
'__metadata': { 'type': 'SP.Utilities.EmailProperties' },
'From': 'Hemanth.Anumolu@somewhere.com',
'To': { 'results': ['Test@somewhere.com'] },
'Body': '<h1>Hello!!</h1><p>This is mail was sent from a client side function</p>',
'Subject':'Send eMail'
}
}
),
headers: {
"Accept": "application/json;odata=verbose",
"content-type": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val()
},
success: function (data) {
alert("eMail sent successfully.");
},
error: function (err) {
alert(JSON.stringify(err));
}
});
Hope this post helps you all.
0 comments:
Post a Comment