本篇文章主要介紹了透過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>
以上是使用C#實作發送自訂的html格式郵件的程式碼案例的詳細內容。更多資訊請關注PHP中文網其他相關文章!