Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send an email with a HTML file as body (C#)

Tags:

How can I set the MailMessage's body with a HTML file ?

like image 672
Paul Avatar asked Jul 20 '09 20:07

Paul


People also ask

How do I send an HTML file as an attachment?

To Insert text or HTML files into a message:Click the Attach File icon in Outlook 2007, 2010, and 2013. Select the file and expand the Insert button. Select Insert as text. If the file contains HTML tags, it will render in the message as HTML.

How do I save an email as HTML?

Click File > Save As to open the Save As dialog window. Select HTML from the Save as type drop-down menu. Enter a file title, and choose a folder to save the email to. Then press the Save button.


2 Answers

Just set the MailMessage.BodyFormat property to MailFormat.Html, and then dump the contents of your html file to the MailMessage.Body property:

using (StreamReader reader = File.OpenText(htmlFilePath)) // Path to your  {                                                         // HTML file     MailMessage myMail = new MailMessage();     myMail.From = "[email protected]";     myMail.To = "[email protected]";     myMail.Subject = "HTML Message";     myMail.BodyFormat = MailFormat.Html;      myMail.Body = reader.ReadToEnd();  // Load the content from your file...     //... } 
like image 60
Christian C. Salvadó Avatar answered Sep 20 '22 17:09

Christian C. Salvadó


I case you are using System.Net.Mail.MailMessage, you can use:

mail.IsBodyHtml = true; 

System.Web.Mail.MailMessage is obsoleted but if using it: mail.BodyFormat works.

like image 43
A-Sharabiani Avatar answered Sep 19 '22 17:09

A-Sharabiani