How can I set the MailMessage’s body with a HTML file ?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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 = "<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c5a3b7aaa885a8aca6b7aab6aaa3b1eba6aaa8">[email protected]</a>";
myMail.To = "<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a0d4cfe0cdc9c3d2cfd3cfc6d48ec3cfcd">[email protected]</a>";
myMail.Subject = "HTML Message";
myMail.BodyFormat = MailFormat.Html;
myMail.Body = reader.ReadToEnd(); // Load the content from your file...
//...
}
Method 2
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.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0