本篇文章主要介绍了通过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格式邮件的代码案例的详细内容。
