2011-03-30 17 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