2011-03-30 6 views
14

मैं एक HTML घटक के साथ बहु-भाग एमआईएम संदेश भेजना चाहता हूं और उन लोगों के लिए सादा पाठ घटक भेजना चाहता हूं जिनके ईमेल क्लाइंट HTML को संभाल नहीं सकते हैं। System.Net.Mail.MailMessage कक्षा इसका समर्थन करने के लिए प्रकट नहीं होती है। आप इसे कैसे करते हो?सी # में बहु-भाग एमआईएम संदेश कैसे भेजें?

उत्तर

32

डी 'ओह, यह वास्तव में सरल है ... लेकिन मैं किसी को जो, मेरी तरह, Googling से पहले जवाब के लिए इतने पर देख आया था के लिए यहाँ जवाब छोड़ देंगे ... :)

क्रेडिट करने के लिए 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 का उपयोग कर सकते हैं/एचटीएमएल " – Talon

+1

मैं googled और यह मुझे SO: / – Beta033