How to send HTML-Formated email?

http://stackoverflow.com/questions/8628683/how-to-send-html-formated-email

Best way to send html formatted Email

This code will be in “Customer.htm

<table>
  <tr>
    <td>
      Dealer's Company Name
    </td>
    <td>
      :
    </td>
    <td>
      #DealerCompanyName#
    </td>
  </tr>
</table>

Read HTML file Using System.IO.File.ReadAllText. get all HTML code in string variable.

string Body = System.IO.File.ReadAllText(Server.MapPath("EmailTemplates/Customer.htm"));

Replace Particular string to your custom value.

Body = Body.Replace("#DealerCompanyName#", _lstGetDealerRoleAndContactInfoByCompanyIDResult[0].CompanyName);

call SendEmail(string Body) Function and do procedure to send email.

public static void SendEmail(string Body)
{
    MailMessage message = new MailMessage();
    message.From = new MailAddress(Session["Email"].Tostring());
    message.To.Add(ConfigurationSettings.AppSettings["RequesEmail"].ToString());
    message.Subject = "Request from " + SessionFactory.CurrentCompany.CompanyName + " to add a new supplier";
    message.IsBodyHtml = true;
    message.Body = Body;

    SmtpClient smtpClient = new SmtpClient();
    smtpClient.UseDefaultCredentials = true;

    smtpClient.Host = ConfigurationSettings.AppSettings["SMTP"].ToString();
    smtpClient.Port = Convert.ToInt32(ConfigurationSettings.AppSettings["PORT"].ToString());
    smtpClient.EnableSsl = true;
    smtpClient.Credentials = new System.Net.NetworkCredential(ConfigurationSettings.AppSettings["USERNAME"].ToString(), ConfigurationSettings.AppSettings["PASSWORD"].ToString());
    smtpClient.Send(message);
};
shareedit
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s