2011-03-30 15 views
14

Deseo enviar mensajes MIME de varias partes con un componente HTML más un componente de texto sin formato para personas cuyos clientes de correo electrónico no pueden manejar HTML. La clase System.Net.Mail.MailMessage no parece ser compatible con esto. ¿Cómo lo haces?¿Cómo enviar mensajes MIME multiparte en C#?

Respuesta

32

D'oh, esto es muy simple ... pero voy a dejar aquí la respuesta para cualquier persona que, como yo, fue a buscar el SO de la respuesta antes de buscar en Google ... :)

de crédito a this article.

Uso AlternateViews, así:

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

Si quieres un sistema un poco más fuertemente tipado puede utilizar MediaTypeNames.Text.Plain o MediaTypeNames.Text.Html en lugar de "/ plain text" y "text/html " – Talon

+1

Busqué en Google y me envió a SO: / – Beta033

Cuestiones relacionadas