C# 在 .net 中以 HTML 和纯文本形式发送邮件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44777/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sending a mail as both HTML and Plain Text in .net
提问by Michael Stum
I'm sending mail from my C# Application, using the SmtpClient. Works great, but I have to decide if I want to send the mail as Plain Text or HTML. I wonder, is there a way to send both? I think that's called multipart.
我正在使用 SmtpClient 从我的 C# 应用程序发送邮件。效果很好,但我必须决定是否要以纯文本或 HTML 格式发送邮件。我想知道,有没有办法同时发送?我认为这叫做多部分。
I googled a bit, but most examples essentially did not use SmtpClient but composed the whole SMTP-Body themselves, which is a bit "scary", so I wonder if something is built in the .net Framework 3.0?
我用谷歌搜索了一下,但大多数示例基本上没有使用 SmtpClient 而是自己组成了整个 SMTP-Body,这有点“可怕”,所以我想知道 .net Framework 3.0 中是否内置了某些东西?
If not, is there any really well used/robust Third Party Library for sending e-Mails?
如果没有,是否有任何真正使用良好/强大的第三方库来发送电子邮件?
采纳答案by Nick Berardi
What you want to do is use the AlternateViews property on the MailMessage
您想要做的是使用 MailMessage 上的 AlternateViews 属性
http://msdn.microsoft.com/en-us/library/system.net.mail.mailmessage.alternateviews.aspx
http://msdn.microsoft.com/en-us/library/system.net.mail.mailmessage.alternateviews.aspx
回答by Michael Stum
The MSDN Documentation seems to miss one thing though, I had to set the content type manually, but otherwise, it works like a charm :-)
MSDN 文档似乎遗漏了一件事,我必须手动设置内容类型,否则,它就像一个魅力:-)
MailMessage msg = new MailMessage(username, nu.email, subject, body);
msg.BodyEncoding = Encoding.UTF8;
msg.SubjectEncoding = Encoding.UTF8;
AlternateView htmlView = AlternateView.CreateAlternateViewFromString(htmlContent);
htmlView.ContentType = new System.Net.Mime.ContentType("text/html");
msg.AlternateViews.Add(htmlView);
回答by Michael Stum
Just want to add that you can use defined constants MediaTypeNames.Text.Html
and MediaTypeNames.Text.Plain
instead of "text/html"
and "text/plain"
, which is always a preferable way. It's in System.Net.Mime
namespace.
只是想补充一点,您可以使用定义的常量MediaTypeNames.Text.Html
而MediaTypeNames.Text.Plain
不是"text/html"
and "text/plain"
,这始终是一种更可取的方式。它在System.Net.Mime
命名空间中。
So in the example above, it would be:
所以在上面的例子中,它将是:
AlternateView htmlView = AlternateView.CreateAlternateViewFromString(htmlContent, null, MediaTypeNames.Text.Html);
回答by Aric TenEyck
I'm just going to put a note here for anyone that's having problems and finds their way to this page - sometimes, Outlook SMTP servers will reconvert outgoing email. If you're seeing your plain-text body vanish entirely, and nothing but base64-encoded attachments, it might be because your server is reencoding the email. Google's SMTP server does not reencode email - try sending through there and see what happens.
我只是要在此为遇到问题并找到此页面的任何人添加注释 - 有时,Outlook SMTP 服务器会重新转换外发电子邮件。如果您看到纯文本正文完全消失,只有 base64 编码的附件,则可能是因为您的服务器正在重新编码电子邮件。Google 的 SMTP 服务器不会重新编码电子邮件 - 尝试通过那里发送,看看会发生什么。
回答by user1852503
On top of using AlternateViews views to add both the html and the plain text view, make sure you are not also setting the body of the Mail Messageobject.
除了使用 AlternateViews 视图添加 html 和纯文本视图之外,请确保您没有同时设置邮件消息对象的正文。
// do not do this:
var msg = new MailMessage(model.From, model.To);
msg.Body = compiledHtml;
As it will make your email contain the html content in both views, overriding the alternative views.
因为它会使您的电子邮件在两个视图中都包含 html 内容,从而覆盖替代视图。
回答by Pieter
For the people(like me) who've had the problem of gmail displaying the plaintext part instead of the html part.
对于那些遇到过 gmail 显示纯文本部分而不是 html 部分问题的人(像我一样)。
Gmail seems to always display the last part in your message.
Gmail 似乎总是显示邮件的最后一部分。
So if you've added the html part before your plain text part chances are gmail will always show the plain text variant.
因此,如果您在纯文本部分之前添加了 html 部分,则 gmail 将始终显示纯文本变体。
To fix this you can simply add the plain text part before your html part.
要解决此问题,您只需在 html 部分之前添加纯文本部分。