同时发送 HTML 和文本电子邮件是最佳做法吗?
如果我只发送 HTML 有什么危险?
我在想下面这样的事情,来自http://johnnycoder.com/blog/2009/04/15/net-mailmessage-linkedresources-alternateviews-and-exceptions/ http://johnnycoder.com/blog/2009/04/15/net-mailmessage-linkedresources-alternateviews-and-exceptions/.
try
{
// Assign a sender, recipient and subject to new mail message
MailAddress sender =
new MailAddress("[email protected] /cdn-cgi/l/email-protection", "Sender");
MailAddress recipient =
new MailAddress("[email protected] /cdn-cgi/l/email-protection", "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);
}
我想说,在当今世界,“最佳实践”方法是确保您以以下方式发送消息:both纯文本和 HTML(如果您确实想发送 HTML 电子邮件)。
哦,并确保您确实发送了content在纯文本视图中,而不是一句“您必须使用支持 HTML 消息的电子邮件客户端”。 Google Mail 采用了这种方法,而且似乎工作得很好,允许在成熟的 PC 客户端上查看“丰富”视图,同时还允许在更受限制的设备(即移动/手机)上查看“最少”视图。
如果您想采取纯粹主义者的观点,您根本不会发送 HTML 电子邮件,也不会将二进制文件“附加”到电子邮件中。原始电子邮件标准的两个损坏,该标准仅起初用于纯文本。
(看看一些人对此的看法here http://www.georgedillon.com/web/html_email_is_evil.shtml and here http://birdhouse.org/etc/evilmail.html)
然而,在务实的现代现实世界中,HTML 电子邮件是非常真实的,并且非常容易被接受。发送 HTML 电子邮件的主要缺点是收件人是否会按照您希望的方式看到电子邮件。这与网页设计师多年来一直在努力解决的问题大致相同。让他们的网站在所有可能的浏览器中看起来“恰到好处”(尽管今天比许多年前要容易得多)。
类似于确保网站正常运行无需Javascript,通过以 HTML 和纯文本形式发送电子邮件,您将确保您的电子邮件优雅地降级这样人们在(例如)小型移动设备(如今这种设备变得越来越流行,并且可能能够也可能无法呈现完整的 HTML 电子邮件)上阅读电子邮件时仍然可以毫无问题地阅读您的电子邮件内容。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)