167

I could be able to let the web application sends automatic emails using Windows Task Scheduler. Now I want to send HTML-Formatted email using the following method that I wrote for sending emails.

My code-behind:

protected void Page_Load(object sender, EventArgs e)
    {
        SmtpClient sc = new SmtpClient("mail address");
        MailMessage msg = null;

        try
        {
            msg = new MailMessage("xxxx@gmail.com",
                "yyyy@gmail.com", "Message from PSSP System",
                "This email sent by the PSSP system");

             sc.Send(msg);
        }

        catch (Exception ex)
        {
            throw ex;
        }

        finally
        {
            if (msg != null)
            {
                msg.Dispose();
            }
        }
    }

How to do that? I just want to put some bold text with one link and maybe one image in the email.

sshine
  • 15,635
  • 1
  • 41
  • 66
user1093651
  • 1,991
  • 3
  • 20
  • 30

3 Answers3

270

Setting isBodyHtml to true allows you to use HTML tags in the message body:

msg = new MailMessage("xxxx@gmail.com",
                "yyyy@gmail.com", "Message from PSSP System",
                "This email sent by the PSSP system<br />" +
                "<b>this is bold text!</b>");

msg.IsBodyHtml = true;
Shai
  • 25,159
  • 9
  • 44
  • 67
93

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(HttpContext.Current.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);
        }
user3305711
  • 441
  • 5
  • 16
Ashfaq Shaikh
  • 1,648
  • 12
  • 20
  • 3
    Maybe use **MailDefinition** or **RazorEngine** ***http://stackoverflow.com/questions/10512845/how-to-send-email-wth-email-template-c-sharp*** – Kiquenet Oct 07 '15 at 07:43
21

This works for me

msg.BodyFormat = MailFormat.Html;

and then you can use html in your body

msg.Body = "<em>It's great to use HTML in mail!!</em>"
Pawan Nogariya
  • 8,330
  • 12
  • 52
  • 105
  • 12
    This works for `MailMessage` in [`System.Web.Mail`](https://msdn.microsoft.com/en-us/library/system.web.mail.mailmessage.bodyformat%28v=vs.110%29.aspx). Doing `IsBodyHtml` is for the one in [`System.Net.Mail`](https://msdn.microsoft.com/en-us/library/system.net.mail.mailmessage.isbodyhtml(v=vs.110).aspx) – David Sherret Feb 12 '15 at 20:15