Send Email with Sitecore MainUtil class

Send Email with Sitecore MainUtil class

In my previous blog we were sending email by using SMTP client and reading Parameter from WFFFM actions parameter field. Sitecore also provides a MailUtil class for sending the email by calling SendMail function.

SendMail required System.Net.Mail.MailMessage type for parameter. Please use System.Net.Mail not System.Web.Mail and also we need to update sitecore.config file for SMTP settings.

Here is sample code for sending email using MainUtil Class.

public bool SendXPEmail(string from, List<string> tos, List<string> ccs, List<string> bccs, string subject, string body)

{

try

{

var emailMessage = new MailMessage();

emailMessage.From = from.IsNotNullAndNotEmpty() ? new MailAddress(@from) : new MailAddress(“noreply@sitecorexpblog.wordpress,com”);

foreach (var to in tos)

{

if (to.IsNotNullAndNotEmpty())

emailMessage.To.Add(to);

 

}

if (ccs.Any())

{

foreach (var cc in ccs)

{

if (cc.IsNotNullAndNotEmpty())

emailMessage.CC.Add(cc);

}

}

if (bccs.Any())

{

foreach (var bcc in bccs)

{

if (bcc.IsNotNullAndNotEmpty())

emailMessage.Bcc.Add(bcc);

}

}

emailMessage.Subject = subject;

emailMessage.Body = body;

emailMessage.IsBodyHtml = true;

MainUtil.SendMail(emailMessage);

return true;

}

catch (Exception ex)

{

Log.Error(“Something going wrong with Greeting card send email:”, ex);

return false;

}

}

Advertisements