/// <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)
{
//创建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);
//设置邮件服务器
System.Web.Mail.SmtpMail.SmtpServer = smtpserver;
try
{
//发送电子邮件
System.Web.Mail.SmtpMail.Send(mailMsg);
return true;
}
catch (Exception e)
{
Response.Write(e.Message.ToString());
return false;
}
}
/// 功 能 : 电子邮件发送
/// 语言环境: 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)
{
//创建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);
//设置邮件服务器
System.Web.Mail.SmtpMail.SmtpServer = smtpserver;
try
{
//发送电子邮件
System.Web.Mail.SmtpMail.Send(mailMsg);
return true;
}
catch (Exception e)
{
Response.Write(e.Message.ToString());
return false;
}
}
自己封装的函数,发送的时候可以这样调用
SendEmail("smtpServer", "fromAddress", "fromPWD", "fromAddress", "toAddress", "Subject", "Body")
Asp.Net2.0(System.Net.Mail),只适用与2.0版本
/// <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)
{
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发是加密的
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
{
mail.Send(message);
return true;
}
catch (Exception ex)
{
return false;
}
}
/// 发送电子邮件函数
/// </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)
{
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发是加密的
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
{
mail.Send(message);
return true;
}
catch (Exception ex)
{
return false;
}
}