欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

通过C#实现发送自定义的html格式邮件

程序员文章站 2023-02-20 16:07:10
要发送html格式邮件,需要设置mailmessage对象的isbodyhtml属性,设置为true。 类mailmessage在命名空间system.net.mail下...

要发送html格式邮件,需要设置mailmessage对象的isbodyhtml属性,设置为true。

类mailmessage在命名空间system.net.mail下。

using system.net.mail;

发送html格式的邮件在hovertreetop项目中已经实现,并发送成功。

需依赖于hovertreeframe项目的hovertreeemail类。

方法为:

复制代码 代码如下:

public static string hovertreesendemail(string username, string password, smtpclient smtpclient, mailmessage mailmessage)

页面截图:

通过C#实现发送自定义的html格式邮件

emailsend.aspx页面:

<h2>发送邮件</h2>
  <br />收信人邮箱:<asp:textbox runat="server" id="textbox_mail" textmode="email" columns="53" />
  <br />标题:<asp:textbox runat="server" id="textbox_title" columns="60" />
  <br /><asp:checkbox runat="server" id="checkbox_ishtml" text="是否html格式" />
  <br />内容:
  <br /><asp:textbox runat="server" id="textbox_content" textmode="multiline" rows="10" columns="70" />
  <br /> <asp:button runat="server" id="button_send" text="发送邮件" onclick="button_send_click" />
    <br />
    <asp:literal runat="server" id="literal_tips" />

emailsend.aspx.cs代码:

using system;
using system.net.mail;
using hovertree.hovertreeframe.htnet;
using hovertreetop.htconfig.myconfig;

namespace hovertreetop.hovertree.hovertreepanel.htpanel.hemail
{
  public partial class emailsend : system.web.ui.page
  {
    protected void page_load(object sender, eventargs e)
    {

    }

    protected void button_send_click(object sender, eventargs e)
    {
      //使用smtp来发送邮件
      //literal_tips.text = hovertreeemail.hovertreesendemail("smtp.hovertree.com", "hello@.mail.hovertree.com", "hewenqi", "hello@mail.hovertree.com", "ht@mail.hovertree.com", "祝你生日快乐!", "生日快乐!天天开心! -- 何问起");
      // literal_tips.text = hovertreeemail.hovertreesendemail(htsmtpconfig.htsmtphost, htsmtpconfig.htsmtpusername, htsmtpconfig.htsmtppassword, htsmtpconfig.htsmtpfromemail, textbox_mail.text.trim(), textbox_title.text, textbox_content.text);

      smtpclient h_smtpclient = new smtpclient();
      h_smtpclient.host = htsmtpconfig.htsmtphost;
      mailmessage h_mailmessage = new mailmessage();
      h_mailmessage.from = new mailaddress(htsmtpconfig.htsmtpfromemail);
      h_mailmessage.to.add(textbox_mail.text.trim());
      h_mailmessage.subject = textbox_title.text.trim();
      h_mailmessage.body = textbox_content.text;
      h_mailmessage.isbodyhtml = checkbox_ishtml.checked;

      literal_tips.text = hovertreeemail.hovertreesendemail(htsmtpconfig.htsmtpusername, htsmtpconfig.htsmtppassword, h_smtpclient, h_mailmessage);

      if (literal_tips.text == "")
      {
        literal_tips.text = "发送成功!";
        textbox_content.text = "";
        textbox_title.text = "";
        textbox_mail.text = "";
      }
    }
  }
}

用于发送的示例内容:

<html>
<body>
  <h2>c#发送html格式的邮件 </h2>
  <div style="background-color:green;width:200px;height:100px;color:white">hovertreetop</div>
</body>
</html>

代码下载:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。