我有以下代码来创建和发送电子邮件:
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?
答案 0 :(得分:9)
MailMessage.IsBodyHtml
(来自MSDN):
获取或设置一个值,该值指示邮件消息正文是否在 HTML。
using (var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body,
IsBodyHtml = true // this property
})
答案 1 :(得分: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...
//...
}
答案 2 :(得分:-1)
将mailMessage.IsBodyHtml
设为true。然后您的邮件将以HTML格式呈现。