.Net : System.Net.Mail et le MultiPart

Après plusieurs recherches sur le net pour trouver le moyen d’envoyer des mails en .Net avec un body en MultiPart, j’ai trouvé cette solution :

 static void MultiPartMime()
{
//create the mail message
MailMessage 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
AlternateView 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
AlternateView 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
SmtpClient smtp = new SmtpClient("127.0.0.1"); //specify the mail server address
smtp.Send(mail);
}

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *