Paul
Paul

Reputation: 12799

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

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

Upvotes: 25

Views: 90918

Answers (2)

A-Sharabiani
A-Sharabiani

Reputation: 19329

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.

Upvotes: 29

Christian C. Salvadó
Christian C. Salvadó

Reputation: 827178

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

Upvotes: 52

Related Questions