ASP.NET2.0发送电子邮件示例代码
一个国外的英文网站上有非常详细的代码说明:
相关的资料地址:http://www.systemnetmail.com/
下面的代码是我参考资料写出的一个示例:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net;
using System.Net.Mail;
namespace Email_Test.aspx
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string emailTitle = "测试email";
string toEmail = "seagreen7@yeah.net";
string mycontent = "这是测试邮件内容";
string myResult = SendHtmlEmail(emailTitle, toEmail, mycontent);
if (myResult == "ok")
{ this.Label1.Text = "恭喜,邮件已经成功发送给" + toEmail; }
else
{ this.Label1.Text = "抱歉,邮件发送失败,请检查web.config文件的配置信息 system.net 节点。" ; }
}
public static string SendHtmlEmail(string EmailTitle, string destEmail, string EmailContent)
{
try
{
// 读取web.config中的邮件发送的配置信息
//在这里的代码中,我们不需要设置SmtpClient类的任何属性,因为它们已经在Web.config文件中指定了
//create the mail message
MailMessage mail = new MailMessage();
//set the addresses
mail.To.Add(destEmail);
//set the content
mail.Subject = EmailTitle;
//screen scrape the html
string html = EmailContent;
mail.Body = html;
mail.IsBodyHtml = true;
//send the message
SmtpClient smtp = new SmtpClient();
smtp.Send(mail);
}
catch (Exception e)
{
return "fail<br>" + e.ToString(); //发送失败,返回fail
}
return "ok"; //发送成功,返回 ok
}
}
}
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net;
using System.Net.Mail;
namespace Email_Test.aspx
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string emailTitle = "测试email";
string toEmail = "seagreen7@yeah.net";
string mycontent = "这是测试邮件内容";
string myResult = SendHtmlEmail(emailTitle, toEmail, mycontent);
if (myResult == "ok")
{ this.Label1.Text = "恭喜,邮件已经成功发送给" + toEmail; }
else
{ this.Label1.Text = "抱歉,邮件发送失败,请检查web.config文件的配置信息 system.net 节点。" ; }
}
public static string SendHtmlEmail(string EmailTitle, string destEmail, string EmailContent)
{
try
{
// 读取web.config中的邮件发送的配置信息
//在这里的代码中,我们不需要设置SmtpClient类的任何属性,因为它们已经在Web.config文件中指定了
//create the mail message
MailMessage mail = new MailMessage();
//set the addresses
mail.To.Add(destEmail);
//set the content
mail.Subject = EmailTitle;
//screen scrape the html
string html = EmailContent;
mail.Body = html;
mail.IsBodyHtml = true;
//send the message
SmtpClient smtp = new SmtpClient();
smtp.Send(mail);
}
catch (Exception e)
{
return "fail<br>" + e.ToString(); //发送失败,返回fail
}
return "ok"; //发送成功,返回 ok
}
}
}
web.config的内容如下:
<system.net>
<mailSettings>
<!-- 发送邮件设置,把这里的邮箱地址和密码设置成你自己的就ok了 -->
<smtp from="yours@126.com">
<network host="smtp.126.com" port="25" userName="yours@126.com" password="123456" defaultCredentials="false"/>
</smtp>
</mailSettings>
</system.net>
<mailSettings>
<!-- 发送邮件设置,把这里的邮箱地址和密码设置成你自己的就ok了 -->
<smtp from="yours@126.com">
<network host="smtp.126.com" port="25" userName="yours@126.com" password="123456" defaultCredentials="false"/>
</smtp>
</mailSettings>
</system.net>