1

我有以下代码来创建和发送电子邮件:

var fromAddress = new MailAddress("email@address.com", "Summary");
var toAddress = new MailAddress(dReader["Email"].ToString(), dReader["FirstName"].ToString());
const string fromPassword = "####";
const string subject = "Summary";
string body = bodyText;

//Sets the smpt server of the hosting account to send
var smtp = new SmtpClient
{
    Host = "smpt@smpt.com",
    Port = 587,
    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);
}

如何将邮件正文设置为 HTML?

4

3 回答 3

9

MailMessage.IsBodyHtml(来自 MSDN):

获取或设置一个值,该值指示邮件正文是否为 Html。

using (var message = new MailMessage(fromAddress, toAddress)
{                       
    Subject = subject,
    Body = body,
    IsBodyHtml = true // this property
})
于 2013-04-01T18:35:55.117 回答
0

只需将 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...
    //...
}
于 2013-04-01T18:36:58.487 回答
-1

设置mailMessage.IsBodyHtml为真。然后您的邮件消息将以 HTML 格式呈现。

于 2014-03-10T13:01:20.943 回答