发送带有 HTML 文件作为正文的电子邮件 (C#)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1155797/
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
Send an email with a HTML file as body (C#)
提问by Paul
How can I set the MailMessage's body with a HTML file ?
如何使用 HTML 文件设置 MailMessage 的正文?
采纳答案by CMS
Just set the MailMessage.BodyFormatproperty to MailFormat.Html, and then dump the contents of your html file to the MailMessage.Bodyproperty:
只需将MailMessage.BodyFormat属性设置为MailFormat.Html,然后将 html 文件的内容转储到MailMessage.Body属性:
using (StreamReader reader = File.OpenText(htmlFilePath)) // Path to your
{ // HTML file
MailMessage myMail = new MailMessage();
myMail.From = "[email protected]";
myMail.To = "[email protected]";
myMail.Subject = "HTML Message";
myMail.BodyFormat = MailFormat.Html;
myMail.Body = reader.ReadToEnd(); // Load the content from your file...
//...
}
回答by A-Sharabiani
I case you are using System.Net.Mail.MailMessage
, you can use:
如果你正在使用System.Net.Mail.MailMessage
,你可以使用:
mail.IsBodyHtml = true;
System.Web.Mail.MailMessage
is obsoleted but if using it: mail.BodyFormat
works.
System.Web.Mail.MailMessage
已过时,但如果使用它:mail.BodyFormat
有效。