Compiling HTML body for mail in c#

2019-02-18 16:39发布

问题:

I am using the following codes for users to send me a quote to my email.

    using System.Net.Mail;

    MailMessage mail = new MailMessage();
    mail.From = new MailAddress(Email.Text);
    mail.To.Add("my yahoo email");
    mail.Subject = "Requested Quote From my site";
    mail.Body = "<br /><b>Primary project type:</b> " + ProjectTypeRadio.SelectedItem.Text + "<br /><b>Interested In:</b> " + InterestedCheck + "<br /><br /><br /><br /><b>PRODUCT DETAILS:</b><hr />" + Electric + HotWater + PoolHeating + SpaceHeating + "<br /> + "<br /><b>Message:</b><br /><hr /><br />" + txtMsg.Text + "<br /><br />";

    SmtpClient smtp = new SmtpClient("smtp.mail.yahoo.com");
    smtp.Send(mail);
    Response.Redirect("Thanks.aspx");

Web.Config

<system.net>
<mailSettings>
  <smtp>
    <network host="smtp.mail.yahoo.com" port="995" userName="my yahoo email" password="Password" />
  </smtp>
</mailSettings>

PROBLEM: I am recieving the mail in my yahoo account but the message is not formated instead its showing all the HTML tags i.e.

Can anyone please check my code and point my mistake or guide me a solution, it will be a big favor, thanks.

回答1:

You can set mail.IsBodyHTML = true

Also, depending on what you're sending you may want to use AlternateViews to send both plain text and text/html variations.



回答2:

you must set BodyFormat mail.BodyFormat = System.Web.Mail.MailFormat.Html;