14
我想发送带有HTML组件的多部分MIME消息,以及其电子邮件客户端无法处理HTML的人的纯文本组件。 System.Net.Mail.MailMessage
类似乎不支持这一点。你怎么做呢?如何在c#中发送多部分MIME消息?
我想发送带有HTML组件的多部分MIME消息,以及其电子邮件客户端无法处理HTML的人的纯文本组件。 System.Net.Mail.MailMessage
类似乎不支持这一点。你怎么做呢?如何在c#中发送多部分MIME消息?
D'哦,这是非常简单的......但我会离开这里的答案的人谁像我一样,来的那么对于谷歌搜索之前的答案找上... :)
感谢this article。
使用AlternateViews
,就像这样:
//create the mail message
var mail = new MailMessage();
//set the addresses
mail.From = new MailAddress("[email protected]");
mail.To.Add("[email protected]");
//set the content
mail.Subject = "This is an email";
//first we create the Plain Text part
var plainView = AlternateView.CreateAlternateViewFromString("This is my plain text content, viewable by those clients that don't support html", null, "text/plain");
//then we create the Html part
var htmlView = AlternateView.CreateAlternateViewFromString("<b>this is bold text, and viewable by those mail clients that support html</b>", null, "text/html");
mail.AlternateViews.Add(plainView);
mail.AlternateViews.Add(htmlView);
//send the message
var smtp = new SmtpClient("127.0.0.1"); //specify the mail server address
smtp.Send(mail);
如果你想有一个稍微强类型的系统,您可以使用MediaTypeNames.Text.Plain或MediaTypeNames.Text.Html,而不是 “text/plain的” 和“文/ html“ – Talon 2013-10-07 13:19:14
我GOOGLE了,它发送给我SO:/ – Beta033 2015-07-22 19:48:34