使用C#實現發送自訂的html格式郵件的代碼案例

來源:互聯網
上載者:User
本篇文章主要介紹了通過C#實現發送自訂的html格式郵件,詳細的介紹了發送HTML格式郵件的方法,有興趣的可以瞭解一下。

要發送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)

頁面:

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>  <p style="background-color:green;width:200px;height:100px;color:white">HoverTreeTop</p></body></html>
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.