簡體   English   中英

如何在c#中發送多部分MIME消息?

[英]How to send multi-part MIME messages in c#?

我想發送多部分MIME消息,其中包含HTML組件和純文本組件,供電子郵件客戶端無法處理HTML的人使用。 System.Net.Mail.MailMessage類似乎不支持此功能。 你怎么做呢?

噢,這真的很簡單......但是我會在這里留下答案給那些像我一樣在Googling之前來尋找答案的人...... :)

相信這篇文章

使用AlternateViews ,如下所示:

//create the mail message
var mail = new MailMessage();

//set the addresses
mail.From = new MailAddress("me@mycompany.com");
mail.To.Add("you@yourcompany.com");

//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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM