简体   繁体   中英

How to enable HTML in Email Send

I was wondering how to allow my message body to use HTML? I don't get an error it just sends as text when I receive emails while using this code.

Here is my code:

    var fromAddress = new MailAddress("example@gmail.com", "Bivar PDMTool");
    var toAddress = new MailAddress("example@bivar.com", "PDMTool");
    const string fromPassword = "REMOVED";
    const string subject = "Plant Completed PM3-PM5-PR Project";
    const string body = "Plant has completed their data entry on the <span style='text-decoration: font-family: arial, helvetica, sans-serif;'><em>PM3-PM5-PR</em></span> project on PDMTool.<br><br> http://pdmtool.bivar.com/ ";

    var smtp = new SmtpClient
               {
                   Host = "smtp.gmail.com",
                   Port = 587,
                   EnableSsl = true,
                   DeliveryMethod = SmtpDeliveryMethod.Network,
                   UseDefaultCredentials = false,
                   Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
               };
    using (var message = new MailMessage(fromAddress, toAddress)
                         {
                             Subject = subject,
                             Body = body
                         })
    {
        smtp.Send(message);
    }
}

Thank you

Change

 using (var message = new MailMessage(fromAddress, toAddress)
                         {
                             Subject = subject,
                             Body = body
                         })
    {
        smtp.Send(message);
    }

to

using (var message = new MailMessage(fromAddress, toAddress)
                         {
                             Subject = subject,
                             Body = body,
                             IsBodyHtml = true;
                         })
    {
        smtp.Send(message);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM