如何在电子邮件发送中启用HTML

时间:2014-07-22 18:42:15

标签: c#

我想知道如何让我的邮件正文使用HTML?当我在使用此代码时收到电子邮件时,我不会收到它只是作为文本发送的错误。

这是我的代码:

    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);
    }
}

谢谢

1 个答案:

答案 0 :(得分:1)

更改

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

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