C#2.0中的System.Net.Mail中提供了系列的邮件收发的功能支持,很强大,小龙将自己在项目中用到的实现网站发送邮件的功能类贴出,供大家参考:
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Mail;
namespace BLL.Mail
{
/// <summary>
/// 名称:邮件发送管理类
/// 说明:该类用于提供对网站内部对用户进行邮件发送的控制
/// 模式:普通类
/// 编写:龙行九天
/// </summary>
public class MailManager
{
public const int SMTP_PORT = 23;
public const int POP_PORT = 110;
/// <summary>
/// 使用默认发送者发送无附件的文本邮件
/// </summary>
/// <param name="sTo">接收者地址(必须),多个地址用逗号(,)分隔</param>
/// <param name="sSubject">标题(必须)</param>
/// <param name="sBody">内容(必须)</param>
/// <returns></returns>
public bool SendMail(string sTo, string sSubject, string sBody)
{
return SendMail("", sTo, sSubject, sBody, false);
}
/// <summary>
/// 发送无附件的文本邮件
/// </summary>
/// <param name="sFrom">发送方,可以为空,将使用系统默认发送地址</param>
/// <param name="sTo">接收者地址(必须),多个地址用逗号(,)分隔</param>
/// <param name="sSubject">标题(必须)</param>
/// <param name="sBody">内容(必须)</param>
/// <returns></returns>
public bool SendMail(string sFrom, string sTo, string sSubject, string sBody)
{
return SendMail(sFrom, sTo, sSubject, sBody, false);
}
/// <summary>
/// 发送无附件邮件
/// </summary>
/// <param name="sFrom">发送方,可以为空,将使用系统默认发送地址</param>
/// <param name="sTo">接收者地址(必须),多个地址用逗号(,)分隔</param>
/// <param name="sSubject">标题(必须)</param>
/// <param name="sBody">内容(必须)</param>
/// <param name="IsHtml">是否发送HTML格式邮件</param>
/// <returns></returns>
public bool SendMail(string sFrom, string sTo, string sSubject, string sBody, bool IsHtml)
{
return SendMail(sFrom, sTo, sSubject, sBody, IsHtml, "");
}
/// <summary>
/// 发送邮件
/// </summary>
/// <param name="sFrom">发送方,可以为空,将使用系统默认发送地址</param>
/// <param name="sTo">接收者地址(必须),多个地址用逗号(,)分隔</param>
/// <param name="sSubject">标题(必须)</param>
/// <param name="sBody">内容(必须)</param>
/// <param name="IsHtml">是否发送HTML格式邮件</param>
/// <param name="sAttachs">附件,可以为空,多个附件用分号(;)分隔开</param>
/// <returns></returns>
public bool SendMail(string sFrom, string sTo, string sSubject, string sBody, bool IsHtml,string sAttachs)
{
BLL.Config.EnvironmentParam oParams = BLL.Config.EnvironmentParam.GetInstance();
//邮件内容
if (sFrom == "") sFrom = oParams.Mail_Address;
MailMessage oMsg = new MailMessage();
oMsg.From = new MailAddress(sFrom.Trim());
//添加收件方地址
sTo = sTo.Trim();
string[] sToArray = sTo.Split(',', ' ', ';', '|'); //实际支持逗号、空格、分号和|
for (int i = 0; i < sToArray.Length; i++)
{
string sAddress = sToArray[i].Trim();
if(sAddress != "")
oMsg.To.Add(sAddress);
}
oMsg.Subject = sSubject;
oMsg.IsBodyHtml = IsHtml;
oMsg.ReplyTo = oMsg.From;
oMsg.Priority = MailPriority.Normal;
oMsg.BodyEncoding = Encoding.UTF8;
oMsg.Body = sBody;
//邮件附件
if (sAttachs != "")
{
string[] attachFiles = sAttachs.Split(',', ' ', ';', '|');//实际支持逗号、空格、分号和|
for (int i = 0; i < attachFiles.Length; i++)
{
string sFile = attachFiles[i].Trim();
if (sFile != "")
oMsg.Attachments.Add(new Attachment(sFile));
}
}
//SMTP客户端发送邮件
try
{
SmtpClient oSmtp = new SmtpClient(oParams.SMTP_Server); //"smtp.163.com"
//if (oParams.SMTP_Port != SMTP_PORT ) oSmtp.Port = oParams.SMTP_Port;
System.Net.NetworkCredential SMTPUserInfo = new System.Net.NetworkCredential();
SMTPUserInfo.UserName = oParams.SMTP_User; //webadmin
SMTPUserInfo.Password = oParams.SMTP_Pass; //123456
oSmtp.Credentials = SMTPUserInfo;
oSmtp.Send(oMsg);
return true;
}
catch (Exception ex)
{
throw new Exception(ex.StackTrace);
}
}
}
}
备注说明:代码中出现的变量BLL.Config.EnvironmentParam oParams是小龙写的网站配置参数类,将发送邮件使用到的几个参数都配置到配置文件中,通过该类来读取:
oParams.SMTP_Server :SMTP服务器,如smtp.163.com
oParams.SMTP_Port : SMTP服务器的端口,默认是23
oParams.SMTP_User :使用的邮箱账号,如:webadmin
oParams.SMTP_Pass :使用的邮箱密码 如:1234
oParams.Mail_Address:使用的发送邮箱 如:webadmin@myweb.com
该类已经过测试。