Send an Email using SharePoint Provider Hosted Apps

Using MailMessage class

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 subject.";
mail.Body = "this is my test body...";
client.Send(mail);


Using SharePoint Client Object Model (CSOM)  class

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>Test html</b>";
    emailp.Subject = "Test subject";
    
    Utility.SendEmail(_clientContext, properties);
    _clientContext.ExecuteQuery();}


Using SharePoint JavaScript Model (JSOM)  class


var mail = {
        properties: {
            __metadata: { 'type': 'SP.Utilities.EmailProperties' },
            From: 'from@mail.com',
            To: { 'results': ['Test1@mail.com','Test2@mail.com'] },
            Body: 'Test body',
            Subject: 'Test 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
            }
        });


Hope this will helps youuuuuu..




Comments

Popular posts from this blog

IRM and the Object Model

This content database has a schema version which is not supported in this farm

Activate and Deactivate Feature through PowerShell