How can I set the MailMessage's body with a HTML file ?
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.
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.
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... //... }
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With