如果你的ASP.NET程序学得不错了,想用ASP.NET来发送邮件,在网上有好多文章,本站不管网上说是如何实现的,下面整理两种.NET发送邮件的方法给你参考一下,很简单的哦。
第一种:
1、配置文件(Web.config) + 少量C#代码
在 configuration 节点下添加
<system.net> <mailSettings> <smtp from="[email protected]"> <network host="smtp.163.com" password="baidu" port="25" userName="baidu" defaultCredentials="false"/> </smtp> </mailSettings> </system.net>
/// <summary>
/// 发送邮件/// </summary>private void SendMail(){ System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(); message.From = new System.Net.Mail.MailAddress("[email protected]");//发送人邮箱地址,与smtp节点中的from值一致 message.To.Add(new System.Net.Mail.MailAddress("[email protected]"));//接收人邮箱地址 message.Subject = "hello"; message.Body = "<b>taobao</b>"; message.IsBodyHtml = true; System.Net.Mail.SmtpClient smtpclient = new System.Net.Mail.SmtpClient(); smtpclient.Send(message);}
好,介绍完第一种发送邮件的方法,下面看第二种。
/// <summary>
/// 发送邮件/// </summary>private void SendMail(){ System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(); message.From = new System.Net.Mail.MailAddress("[email protected]");//发送人邮箱地址,与smtp节点中的from值一致 message.To.Add(new System.Net.Mail.MailAddress("[email protected]"));//接收人邮箱地址 message.Subject = "hello"; message.Body = "<b>taobao</b>"; message.IsBodyHtml = true; System.Net.Mail.SmtpClient smtpclient = new System.Net.Mail.SmtpClient("smtp.163.com", 25); smtpclient.Credentials = new System.Net.NetworkCredential("baidu", "baidu");//参数分别是邮箱用户名和密码 smtpclient.Send(message);}
怎样,上面两种发送邮件的方法都很简单吧。