发送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>

我已经在本地计算机上复制了此方案。它之所以不在您身边,是因为您已将代码封闭在标签之间,因此这是不必要的。删除HTML标签,然后直接将HTML从文本控件分配到邮件。Body

message.body = body;

那将做到..

最新更新