2009-07-20 86 views

回答

35

MailMessage.BodyFormat属性正好被设置为MailFormat.Html,然后倾倒你的HTML文件的内容到MailMessage.Body属性:

using (StreamReader reader = File.OpenText(htmlFilePath)) // Path to your 
{               // HTML file 
    MailMessage myMail = new MailMessage(); 
    myMail.From = "[email protected]"; 
    myMail.To = "[email protected]"; 
    myMail.Subject = "HTML Message"; 
    myMail.BodyFormat = MailFormat.Html; 

    myMail.Body = reader.ReadToEnd(); // Load the content from your file... 
    //... 
} 
+9

所以,只需指定,此答案对应于`System.Web.Mail.MailMessage`。试图设置BodyFormat时,我遇到了问题,因为Reflector告诉我它不在那里。原因是,我使用`System.Net.Mail.MailMessage`(在该类中,有一个名为`IsBodyHtml`的bool`标志,您可以改为设置)。此外,似乎如果所有的东西都是平等的,建议使用'System.Net.Mail`:http://stackoverflow.com/q/64599/324527 – Mattygabe 2012-07-11 15:37:08

8

我的情况下,你可以使用:

mail.IsBodyHtml = true; 

System.Web.Mail.MailMessage过时,但如果使用它:mail.BodyFormat作品。

相关问题