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

后端 未结 2 1890
轮回少年
轮回少年 2020-12-03 01:20

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

相关标签:
2条回答
  • 2020-12-03 01:56

    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.

    0 讨论(0)
  • 2020-12-03 02:17

    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 = "from@microsoft.com";
        myMail.To = "to@microsoft.com";
        myMail.Subject = "HTML Message";
        myMail.BodyFormat = MailFormat.Html;
    
        myMail.Body = reader.ReadToEnd();  // Load the content from your file...
        //...
    }
    
    0 讨论(0)
提交回复
热议问题