发送html电子邮件asp.net

时间:2018-02-20 06:47:24

标签: c# html asp.net

我正在尝试发送我从gmail第三方插件创建的html电子邮件。我创建了自己的网页来发送电子邮件。我正在使用gmail smtp。我在邮件正文中输入以下代码,但是电子邮件没有作为html模板发送,简单代码已经发送。请帮我发送HTML电子邮件。感谢

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected bool Sendemail()
    {
        var smtph = smtphost.Text;
        var smtpp = smtpport.Text;
        int port = Int32.Parse(smtpp);

        var smtpemail=emailsmtp.Text;
        var smtppass = passwordsmtp.Text;
        //var fromaddress = TextBox1.Text;
        var toaddress = TextBox2.Text;
        var replyto = TextBox4.Text;
        var subject = TextBox3.Text;
        var body = TextArea1.InnerText.Replace(Environment.NewLine, "<br>");
       //var textBoxText = tbDeliver.Text.Replace(Environment.NewLine, "<br>");

        System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
        mail.IsBodyHtml = true;



        try
        {
            MailMessage message = new MailMessage();
            message.From = new MailAddress(smtpemail);
            message.To.Add(toaddress);

            message.Subject =subject;

            message.IsBodyHtml = true;  
             message.Body = "<html><body>" + body.ToString() + "</body></html>";
            //message.Body = body;
             message.ReplyToList.Add(new MailAddress(replyto));

             SmtpClient smtpClient = new SmtpClient(smtph, port);

             smtpClient.Credentials = new System.Net.NetworkCredential(smtpemail, smtppass);
             smtpClient.EnableSsl = true;
             smtpClient.Send(message);

        }
        catch (Exception exp)
        {
            throw exp;
        }
        return true;
    }
    protected void Button1_Click(object sender, EventArgs e)
    {

        Sendemail();
        ScriptManager.RegisterStartupScript(
            this,
            this.GetType(),
            "popup",
            "alert('Email Sent Succesfully');",
            true);

    }
}

我正在尝试发送以下html电子邮件模板:

<div class="gmail_quote gmail_extra gmail_quote">
  <blockquote class="gmail_quote" style=
  "margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
    <div dir="ltr">
      <div class="gmail_quote" dir="ltr">
        <div class="gmail_quote">
          <br>

          <div dir="ltr">
            <a href="https://google.com" rel="noopener" target=
            "_blank" data-saferedirecturl=
            "https://www.google.com/url?hl=en&amp;q=http://rebrand.ly//secureinfo&amp;source=gmail&amp;ust=1519194765300000&amp;usg=AFQjCNHuqoCNs38P52qaKaNtiJt0M0yw9g">
            <img src=
            "https://www.ief.org/_resources/files/pages/logos/logos/ief-logo-with-strap-72dpi.jpg"
            alt="" width="600" height="613"></a>
          </div>
        </div>
        <br>
      </div>
      <br>
    </div>
  </blockquote>
</div>
<br>

1 个答案:

答案 0 :(得分:0)

我已在本地计算机上复制此方案。它不适合你,你已经在代码之间包含代码,所以这是不必要的。删除html标记,并直接将Text Control中的html指定给mail.body

message.body = body;

会这样做..