我是尝试 SMTPClient 的新手,想向客户发送 HTML 格式的电子邮件。相反,电子邮件包含所有标签,而不是格式化的文档。这是我的代码:
public void SendMailMessage(string FromAddress, string FromName, string ToName, string ToAddress, string Subject)
{
MailAddress fromAdd = new MailAddress(FromAddress, FromName);
MailAddress toAdd = new MailAddress(ToAddress);
MailMessage msg = new MailMessage(fromAdd, toAdd);
msg.IsBodyHtml = true;
msg.BodyEncoding = System.Text.Encoding.UTF8;
msg.Subject = Subject;
string HtmlContent = "<div id='emailFormat' style='width: 100%; background-color: #efeac9;'>" +
"< div id = 'emailMsg' style = 'margin-left: auto; margin-right: auto; width: 600px; margin-top: 15px; font-family: Tahoma; font-size: .9em;' ><h2>Hello World</h2></div></div>");
msg.Body = HtmlContent;
SmtpClient client = new SmtpClient();
client.Send(msg);
}
}
电子邮件消息如下所示:
Susan Farrar 今天下午 5:45 Tosusan.farrar@sbcglobal.net
邮件正文
< div id = 'emailMsg' 样式 = 'margin-left: auto; 边距右:自动;宽度:600px;边距顶部:15px;字体系列:Tahoma;字体大小:.9em;' >
你好世界