通过C#发送自定义的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格式的邮件 by 何问起</h2> <a href="http://hovertree.com/h/bjag/swth6nfh.htm">http://hovertree.com/h/bjag/swth6nfh.htm </a> <div style="background-color:green;width:200px;height:100px;color:white">何问起 HoverTreeTop</div> </body> </html>
参考:http://hovertree.com/h/bjag/swth6nfh.htm
代码下载:http://hovertree.com/h/bjaf/hv6cqe5n.htm
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧