Click here to Skip to main content
15,886,806 members
Articles / Productivity Apps and Services / Sharepoint

How to Send Email in SharePoint Provider Hosted Apps

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
23 Feb 2015CPOL1 min read 20.2K   4  
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 method has few advantages over the other two methods:

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

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

JavaScript
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 an SMTP server setup by yourself or valid authentication for the existing server.

Using SharePoint Client Object Model (CSOM)

This has been the most famous method for a sending email. You can use SharePoint Utility class to send an email. But you cannot 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.

JavaScript
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.

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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Sri Lanka Sri Lanka
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --