Asp.Net1.1(System.Web.Mail),同样适合Asp.Net2.0
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//// <summary>
/// 功 能 : 电子邮件发送
/// 语言环境: Asp.Net1.1 & Asp.Net2.0
/// 作 者 : puke
/// 完成日期: 2007-04-07
/// 版权所有: pukesys@tom.com
/// </summary>
/// <param name="smtpserver">邮件服务器名称</param>
/// <param name="userName">发送者邮件的用户名</param>
/// <param name="pwd">发送者邮件的密码</param>
/// <param name="from">发送者邮件的地址</param>
/// <param name="to">收件人的邮件地址</param>
/// <param name="subject">邮件主题</param>
/// <param name="body">邮件内容</param>
/// <returns>
/// 发送成功,返回 true
/// 发送失败,返回 false
/// </returns>
public bool SendEmail(string smtpserver, string userName, string pwd, string from, string to, string subject, string body)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
//创建MailMessage对象
System.Web.Mail.MailMessage mailMsg = new System.Web.Mail.MailMessage();
//设置发送者邮件的地址
mailMsg.From = from;
//设置收件人的邮件地址
mailMsg.To = to;
//设置邮件主题
mailMsg.Subject = subject;
//设置邮件内容
mailMsg.Body = body;
//设置支持服务器验证
mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
//设置发送方邮件的用户名
mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", userName);
//设置发送方邮件的密码
mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", pwd);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//设置邮件服务器
System.Web.Mail.SmtpMail.SmtpServer = smtpserver;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
try
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//发送电子邮件
System.Web.Mail.SmtpMail.Send(mailMsg);
return true;
}
catch (Exception e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Response.Write(e.Message.ToString());
return false;
}
}
自己封装的函数,发送的时候可以这样调用
SendEmail("smtpServer", "fromAddress", "fromPWD", "fromAddress", "toAddress", "Subject", "Body")
Asp.Net2.0(System.Net.Mail),只适用与2.0版本
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//// <summary>
/// 发送电子邮件函数
/// </summary>
/// <param name="host">邮件服务器</param>
/// <param name="userName">from用户名</param>
/// <param name="pwd">from密码</param>
/// <param name="ssl"></param>
/// <param name="from">电子邮件发件人的地址</param>
/// <param name="to">电子邮件收件人的地址</param>
/// <param name="subject">电子邮件主题</param>
/// <param name="body">电子邮件正文</param>
/// <returns></returns>
public bool sendMail(string host, string userName, string pwd, bool ssl, string from, string to, string subject, string body)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
System.Net.Mail.SmtpClient mail = new System.Net.Mail.SmtpClient();
mail.Host = host;//smtp
mail.Credentials = new System.Net.NetworkCredential(userName, pwd);
mail.EnableSsl = ssl;//发送连接套接层是否加密 例如用gmail发是加密的
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(from, to);
message.Body = body;
message.Subject = subject;
message.SubjectEncoding = System.Text.Encoding.GetEncoding("gb2312");
message.BodyEncoding = System.Text.Encoding.GetEncoding("gb2312");
message.IsBodyHtml = true;
try
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
mail.Send(message);
return true;
}
catch (Exception ex)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return false;
}
}