2011-03-30 18 views

回答

32

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); 
+6

如果你想有一個稍微強類型的系統,您可以使用MediaTypeNames.Text.Plain或MediaTypeNames.Text.Html,而不是 「text/plain的」 和「文/ html「 – Talon 2013-10-07 13:19:14

+1

我GOOGLE了,它發送給我SO:/ – Beta033 2015-07-22 19:48:34