Use this code create html for your mail content and class to send mail
namespace MailerService
{
public class MailerController
{
public static void SendUserConfirmationMail(User _user)
{
Mailer mailers = new Mailer();
mailers.RecipientsEmailId = "abc@abc.com";
mailers.RecipientsName = _"Test";
mailers.Subject = "Testing Mail";
mailers.MessageSendingTime = DateTime.Now;
mailers.UserName = "Abc";
MailHelper mailHelper = new MailHelper();
mailHelper.SendMail(mailers, "Test.htm");
}
}
}
namespace MailerService
{
public class MailHelper
{
#region Public variable
public string MailTo
{ get; set; }
public string MailFrom
{ get; set; }
public string MailSubject
{ get; set; }
public string MailBody
{ get; set; }
public string MailKey
{ get; set; }
#endregion
public MailHelper()
{
}
public void SendMail(Mailer mailers, string filePath)
{
StringBuilder sb = new StringBuilder();
sb.Append(System.IO.File.ReadAllText(Settings.GetApplicationRootPath() + "HtmlMails//" + filePath));
sb = sb.Replace("Identifier1", mailers.Identifier1);
sb = sb.Replace("Identifier2", mailers.Identifier2);
sb = sb.Replace("Identifier3", mailers.Identifier3);
sb = sb.Replace("Identifier4", mailers.Identifier4);
MailTo = mailers.RecipientsEmailId;
MailFrom = WebConfigurationManager.AppSettings["SmtpServerUserName"];
MailSubject = mailers.Subject;
MailBody = sb.ToString();
SendMail();
}
#region To send mail
public void SendMail()
{
MailHelper objMail = new MailHelper();
objMail.MailBody = this.MailBody;
objMail.MailFrom = this.MailFrom;
objMail.MailSubject = this.MailSubject;
objMail.MailTo = this.MailTo;
Thread th = new Thread(delegate() { ThreadSend(objMail); });
th.Start();
}
public void ThreadSend(MailHelper obj)
{
SmtpClient smtp = new SmtpClient();
MailMessage eMail = new MailMessage();
eMail.IsBodyHtml = true;
eMail.From = new MailAddress(obj.MailFrom);
eMail.To.Add(obj.MailTo);
smtp.Host = WebConfigurationManager.AppSettings["SmtpServer"];
smtp.EnableSsl = false;
eMail.Body = obj.MailBody;
eMail.Subject = obj.MailSubject;
smtp.Port = WebConfigurationManager.AppSettings["SmtpServerPort"].ToInteger();
smtp.Credentials = new System.Net.NetworkCredential(WebConfigurationManager.AppSettings["SmtpServerUserName"], WebConfigurationManager.AppSettings["SmtpServerPassword"]);
smtp.Send(eMail);
}
void smtp_SendCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
throw new NotImplementedException();
}
#endregion
}
}