发送包含HTML文件作为正文的电子邮件(C#)

Pau*_*aul 22 c# asp.net

如何使用HTML文件设置MailMessage的正文?

CMS*_*CMS 44

只需将MailMessage.BodyFormat属性设置为MailFormat.Html,然后将html文件的内容转储到MailMessage.Body属性:

using (StreamReader reader = File.OpenText(htmlFilePath)) // Path to your 
{                                                         // HTML file
    MailMessage myMail = new MailMessage();
    myMail.From = "from@microsoft.com";
    myMail.To = "to@microsoft.com";
    myMail.Subject = "HTML Message";
    myMail.BodyFormat = MailFormat.Html;

    myMail.Body = reader.ReadToEnd();  // Load the content from your file...
    //...
}
Run Code Online (Sandbox Code Playgroud)

  • 所以,只需指定,这个答案对应于`System.Web.Mail.MailMessage`.当试图设置`BodyFormat`时,我遇到了问题,因为Reflector告诉我它不存在.原因是,我正在使用`System.Net.Mail.MailMessage`(在该类中,有一个名为`IsBodyHtml`的`bool`标志,你可以设置).此外,似乎所有事情都相同,建议使用`System.Net.Mail`:http://stackoverflow.com/q/64599/324527 (9认同)
  • 现在已经改变了.你应该使用"myMail.IsBodyHtml = true;" (2认同)

A-S*_*ani 17

我正在使用System.Net.Mail.MailMessage你的情况,你可以使用:

mail.IsBodyHtml = true;
Run Code Online (Sandbox Code Playgroud)

System.Web.Mail.MailMessage已废弃但如果使用它:mail.BodyFormat有效.