Monday, February 23, 2015

How to Send Email in SharePoint Provider Hosted Apps

There are few methods that we can use to send Emails in SharePoint Provider Hosted apps.

  • Using general Email Sending method
  • Using SharePoint Client Object Model (CSOM)
  • Using SharePoint JavaScript Model (JSOM)

Using general Email Sending method

This is the general method we are using for sending email for asp.net. This is method has advantages over other two methods. this method has few advantages over other methods,

  • Send attachments to the recipients
  • Send emails to external users (SharePoint 2013 email function can not be used to send emails to external users) 

There are many articles in available for this , I’m describing a sample code below.

MailMessage mail = new MailMessage("from@mail.com", "to@mail.com");
SmtpClient client = new SmtpClient();
client.Port = 25;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Host = "smtp.google.com";
mail.Subject = "this is a test email.";
mail.Body = "this is my test email body";
client.Send(mail);

For this you need to either have a smtp server setup by your self or valid authentication for the existing server.


Using SharePoint Client Object Model (CSOM)


This has been a most famous method for a sending email. You can use SharePoint Utility class to send a Email. But you can not send to external users. If you are sending to external users they should be added to your mail exchange but it will take some time to reflect the change.



 
var spContext = SharePointContextProvider.Current.GetSharePointContext(Context);
using (var clientContext = spContext.CreateUserClientContextForSPHost())
{
    var emailp = new EmailProperties();
    emailp.BCC = new List<string>{"a@mail.com"};
    emailp.To = new List<string>{"b@mail.com"};
    emailp= "from@mail.com";
    emailp.Body = "<b>html</b>";
    emailp.Subject = "subject";
    
    Utility.SendEmail(_clientContext, properties);
    _clientContext.ExecuteQuery();}


You can use the same method with remote event receivers only by changing SharePoint client context initiation. 


Using SharePoint JavaScript Model (JSOM)


This is very similar to the CSOM but it will use only JavaScript for sending emails.



var mail = {
        properties: {
            __metadata: { 'type': 'SP.Utilities.EmailProperties' },
            From: 'from@mail.com',
            To: { 'results': ['one@mail.com','two@mail.com'] },
            Body: 'some body',
            Subject: 'subject'
        }
    };
    
var getAppWebUrlUrl = decodeURIComponent(utils.getQueryStringParameter("SPAppWebUrl").replace("#", ""));
var urlTemplate = getAppWebUrlUrl + "/_api/SP.Utilities.Utility.SendEmail";
 
$.ajax({
            contentType: 'application/json',
            url: urlTemplate,
            type: "POST",
            data: JSON.stringify(mail),
            headers: {
                "Accept": "application/json;odata=verbose",
                "content-type": "application/json;odata=verbose",
                "X-RequestDigest": $("#__REQUESTDIGEST").val()
            },
            success: function (data) {
                // code
            },
            error: function (err) {
                // code
            }
        });
Post a Comment