在 SMTP 邮件中编码 HTML
Encode HTML in SMTP Message
我是 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 Today at 5:45 PM
Tosusan.farrar@sbcglobal.net
Message body
< div id = 'emailMsg' style = 'margin-left: auto; margin-right: auto; width: 600px; margin-top: 15px; font-family: Tahoma; font-size: .9em;' >
Hello World
问题是 string HtmlContent
的内容不是格式正确的 HTML 文档。您缺少 <html>
、<head>
、<title>
、<body>
等标签。此外,< div
中附加的 space 不正确。
空白导致了这个问题。你应该改变这个:
< div id = 'emailMsg' ...
为此:
<div id = 'emailMsg' ...
我是 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 Today at 5:45 PM Tosusan.farrar@sbcglobal.net
Message body
< div id = 'emailMsg' style = 'margin-left: auto; margin-right: auto; width: 600px; margin-top: 15px; font-family: Tahoma; font-size: .9em;' >
Hello World
问题是 string HtmlContent
的内容不是格式正确的 HTML 文档。您缺少 <html>
、<head>
、<title>
、<body>
等标签。此外,< div
中附加的 space 不正确。
空白导致了这个问题。你应该改变这个:
< div id = 'emailMsg' ...
为此:
<div id = 'emailMsg' ...