【问题标题】:send html email asp.net发送 html 电子邮件 asp.net
【发布时间】:2018-02-20 06:47:24
【问题描述】:

我正在尝试发送 html 电子邮件,该电子邮件是我从 gmail 3rd 方插件创建的。我创建了自己的网页来发送电子邮件。我正在使用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 标签,或者它们是否被转义?换句话说,如果你在 'var body=' 行之后放置一个断点,并检查 body 变量,你会看到 <div>或
    ?

标签: c# html asp.net


【解决方案1】:

我已经在我的本地机器上复制了这个场景。它对您不起作用的原因是您在标签之间包含了代码,因此这是不必要的。去掉html标签,直接将Text Control中的html赋值给mail.body

message.body = body;

这样就可以了..

【讨论】:

  • 我有一个功能正常的 smtpClient MailMessage 发送,我将邮件正文包含在 和 标记中...
  • @DIe 我试过了,没有它对我来说效果很好,我也认为身体上的 tostring 方法也是导致问题的原因
猜你喜欢
  • 2013-09-08
  • 1970-01-01
  • 2012-12-11
  • 2016-09-15
  • 1970-01-01
  • 2011-03-30
  • 2012-07-27
  • 2012-11-17
  • 1970-01-01
相关资源
最近更新 更多