简体   繁体   English

发送HTML电子邮件asp.net

[英]send html email asp.net

I am trying to send html email that i created from gmail 3rd party plugin. 我正在尝试发送从gmail 3rd party插件创建的html电子邮件。 I had created my own webpage for sending email. 我已经创建了自己的网页来发送电子邮件。 I am using gmail smtp . 我正在使用gmail smtp I input the following code in message body but email doesn't send as html template, simple code has been sent. 我在邮件正文中输入了以下代码,但电子邮件未作为html模板发送,已经发送了简单代码。 Kindly help me to send html email. 请帮助我发送HTML电子邮件。 Thanks 谢谢

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);

    }
}

Following html email template i am trying to send : 我尝试发送以下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>

I have replicate this scenario on my local machine. 我已经在本地计算机上复制了这种情况。 The reason it is not working on your side, that you have enclosed the code between tags, so that is unnecessary. 之所以对您不利,是因为您将代码括在了标签之间,所以这是不必要的。 Remove the html tag, and directly assign html from Text Control to mail.body 删除html标记,然后将Text Control中的html直接分配给mail.body

message.body = body;

that will do it.. 会做到的..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM