发送HTML和Text电子邮件是最佳做法吗?
如果我只发送HTML有什么危险?
我正在考虑下面这样的事情
try
{
// Assign a sender, recipient and subject to new mail message
MailAddress sender =
new MailAddress("sender@johnnycoder.com", "Sender");
MailAddress recipient =
new MailAddress("recipient@johnnycoder.com", "Recipient");
MailMessage m = new MailMessage(sender, recipient);
m.Subject = "Test Message";
// Define the plain text alternate view and add to message
string plainTextBody =
"You must use an email client that supports HTML messages";
AlternateView plainTextView =
AlternateView.CreateAlternateViewFromString(
plainTextBody, null, MediaTypeNames.Text.Plain);
m.AlternateViews.Add(plainTextView);
// Define the html alternate view with embedded image and
// add to message. To reference images attached as linked
// resources from your HTML message body, use "cid:contentID"
// in the <img> tag...
string htmlBody =
"<html><body><h1>Picture</h1><br>" +
"<img src=\"cid:SampleImage\"></body></html>";
AlternateView htmlView =
AlternateView.CreateAlternateViewFromString(
htmlBody, null, MediaTypeNames.Text.Html);
// ...and then define the actual LinkedResource matching the
// ContentID property as found in the image tag. In this case,
// the HTML message includes the tag
// <img src=\"cid:SampleImage\"> and the following
// LinkedResource.ContentId is set to "SampleImage"
LinkedResource sampleImage =
new LinkedResource("sample.jpg",
MediaTypeNames.Image.Jpeg);
sampleImage.ContentId = "SampleImage";
htmlView.LinkedResources.Add(sampleImage);
m.AlternateViews.Add(htmlView);
// Finally, configure smtp or alternatively use the
// system.net mailSettings
SmtpClient smtp = new SmtpClient
{
Host = "smtp.example.com",
UseDefaultCredentials = false,
Credentials =
new NetworkCredential("username", "password")
};
//<system.net>
// <mailSettings>
// <smtp deliveryMethod="Network">
// <network host="smtp.example.com"
// port="25" defaultCredentials="true"/>
// </smtp>
// </mailSettings>
//</system.net>
smtp.Send(m);
}
catch (ArgumentException)
{
throw new
ArgumentException("Undefined sender and/or recipient.");
}
catch (FormatException)
{
throw new
FormatException("Invalid sender and/or recipient.");
}
catch (InvalidOperationException)
{
throw new
InvalidOperationException("Undefined SMTP server.");
}
catch (SmtpFailedRecipientException)
{
throw new SmtpFailedRecipientException(
"The mail server says that there is no mailbox for recipient");
}
catch (SmtpException ex)
{
// Invalid hostnames result in a WebException InnerException that
// provides a more descriptive error, so get the base exception
Exception inner = ex.GetBaseException();
throw new SmtpException("Could not send message: " + inner.Message);
}
Run Code Online (Sandbox Code Playgroud)
Cra*_*gTP 18
我要说的是,在今天的世界里,"最佳实践"的方法是,以确保您发送邮件作为两个纯文本和HTML(如果你真的想发送HTML电子邮件).
哦,并确保你实际上在纯文本视图中发送内容,而不是一句话说"你必须使用支持HTML消息的电子邮件客户端".谷歌邮件采用这种方式,它似乎完美地工作,允许在成熟的PC客户端上"丰富"的视图,同时还允许在更受限制的设备(即移动/手机)上"最小"的视图.
如果您想采用纯粹主义者的观点,您根本不会发送HTML电子邮件,也不会将二进制文件"附加"到电子邮件中.原始电子邮件标准的两个腐败,这是永远只能原本打算用于纯文本.(在这里和这里看到一些人对此的看法)
然而,在务实的现代现实世界中,HTML电子邮件非常真实,而且非常可接受.发送HTML电子邮件的主要缺点是收件人是否会以您希望他们看到的方式看到该电子邮件.这与网页设计师多年来一直在争论的问题大致相同; 让他们的网站在所有可能的浏览器中看起来"恰到好处"(尽管今天它比很多年前容易得多).
与确保网站正常运行而不需要 Javascript 类似,通过将您的电子邮件作为HTML和纯文本发送,您将确保您的电子邮件优雅地降级,以便人们在(例如)小型移动设备上阅读他们的电子邮件(这些东西正在成为如今越来越流行 - 可能会或可能不会呈现完整的HTML电子邮件仍然可以毫无问题地阅读您的电子邮件内容.