原创]email发送系统(c#+基于smtp认证)
http://www.lionsky.net/MyWebsite/article/list.aspx?id=430
http://www.lionsky.net/MyWebsite/article/list.aspx?id=430
的改版
在为公司写通知服务时,从网上找到了以上地址,非常感谢原作者创造性的劳动。改写的目的是为了适应作为服务运行的要求:
1、适应多线程的要求,发送邮件服务可在后台运行,将与SMTP服务器的连接视为独占资源。
2、适应稳定性的要求,不再以简单地抛出异常来处理错误,在出现异常后等待一定时间间隔后重试,重试一段时间间隔后若还时发不出去,则认为是SMTP出错,返回发送邮件不成功的标识。
3、精简属性、方法,与邮件相关的信息不再作为属性,而是作为send的参数传入;只公布了一个无重载的send方法。以此类为基类,另写通知服务要求的接口方法。
以下是改写后的代码:
using System;
using System.Text;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Collections;
using System.Threading; namespace Deep.SendEmail
{
#region AspNetPager Server Control
/// 邮件可以通过 Microsoft Windows 2000 中内置的 SMTP 邮件服务或任意 SMTP 服务器来传送
/// </summary>
public class SmtpMail
{
/// 设定语言代码,默认设定为GB2312,如不需要可设置为""
/// </summary>
private string m_charset="GB2312";
/// <summary>
/// 服务器交互记录
/// </summary>
private StringBuilder m_logs = new StringBuilder();
/// SMTP错误代码哈希表
/// </summary>
private Hashtable m_ErrCodeHT = new Hashtable();
/// SMTP正确代码哈希表
/// </summary>
private Hashtable m_RightCodeHT = new Hashtable();
/// 最多收件人数量
/// </summary>
private int m_recipientMaxnum = 2;
/// 重复时间,以秒为单位
/// </summary>
private int m_RepeatTime = 120;
/// 服务器出错或拒绝后的等待时间,以毫秒为单位
/// </summary>
private int m_WaitTime = 20000;
/// 初始化 <see cref="Lion.Web.Mail.SmtpMail"/> 类的新实例
/// </summary>
public SmtpMail()
{
SMTPCodeAdd();
}
/// <summary>
/// 服务器交互记录,如发现本组件不能使用的SMTP服务器,请将出错时的Logs发给我(lion-a@sohu.com),我将尽快查明原因。
/// </summary>
public string Logs
{
get
{
return m_logs.ToString();
}
}
/// 最多收件人数量
/// </summary>
public int RecipientMaxNum
{
set
{
m_recipientMaxnum = value;
}
get
{
return m_recipientMaxnum;
}
}
/// 设定语言代码,默认设定为GB2312,如不需要可设置为""
/// </summary>
public string Charset
{
get
{
return this.m_charset;
}
set
{
this.m_charset = value;
}
}
/// 重复时间,以秒为单位
/// </summary>
public int RepeatTime
{
get {return m_RepeatTime;}
set {m_RepeatTime = value;}
/// 服务器出错或拒绝后的等待时间,以毫秒为单位
/// </summary>
public int WaitTime
{
get {return m_WaitTime;}
set {m_WaitTime = value > 10000?value:10000;}
}
/// 邮件服务器域名和验证信息
/// 形如:"user:pass@www.server.com:25",也可省略次要信息。如"user:pass@www.server.com"或"www.server.com"
/// </summary>
/// <param name="mailDomain">输入用户名、密码、邮件服务器域名、端口号</param>
/// <param name="mailServer">返回邮件服务器域名</param>
/// <param name="mailServerUserName">返回用户名</param>
/// <param name="password">返回密码</param>
/// <param name="mailserverport">返回端口号</param>
/// <param name="needSmtp">返回是否需要SMTP验证</param>
/// <returns></returns>
private bool SetMailDomain(string mailDomain,out string mailServer,out string mailServerUserName,out string password,
out int mailserverport,out bool needSmtp)
{
bool isRight = false;
//为输出变量赋初值
mailServer = string.Empty;
mailServerUserName = String.Empty;
password = String.Empty;
mailserverport = 25;
needSmtp = false;
int tempint;
if( mailServer != "" )
{
tempint = mailServer.IndexOf("@");
isRight = true;
if(tempint!=-1)
{
string str = mailServer.Substring(0,tempint);
mailServerUserName = str.Substring(0,str.IndexOf(":"));
password = str.Substring(str.IndexOf(":")+1,str.Length-str.IndexOf(":")-1);
needSmtp = !(password==string.Empty);
mailServer = mailDomain.Substring(tempint+1,mailDomain.Length-tempint-1);
}
if(tempint != -1)
{
mailserverport = System.Convert.ToInt32(mailServer.Substring(tempint+1,mailServer.Length-tempint-1));
mailServer = mailServer.Substring(0,tempint);
}
}
}
/// 添加邮件附件
/// </summary>
/// <param name="filePath">附件绝对路径</param>
private IList AddAttachment(params string[] filePath)
{
if(filePath == null || filePath.Length == 0)
{
return null;
}
IList m_Attachments = new System.Collections.ArrayList();// 邮件附件列表
for(int i=0;i<filePath.Length;i++)
{
if(File.Exists(filePath[i]))
{
m_Attachments.Add(filePath[i]);
}
else
{
m_logs.Append("错误:没找到文件名为"+filePath[i]+"的附件文件!"+ENTER);
}
}
return m_Attachments;
}
/// <summary>
/// 添加一组收件人(不超过m_recipientMaxnum个),参数为字符串数组
/// </summary>
/// <param name="recipients">保存有收件人地址的字符串数组(不超过m_recipientMaxnum个)</param>
private Hashtable AddRecipient(params string[] recipients)
{
if(recipients==null || recipients.Length == 0)
{
return null;
}
Hashtable recipientList=new Hashtable();// 收件人列表
for(int i=0;i<recipients.Length;i++)
{
string recipient = recipients[i].Trim();
if(recipient !=String.Empty && recipient.IndexOf("@") != -1)
{
recipientList.Add(recipientList.Count,recipients[i]);
}
}
return recipientList;
}
/// 发送邮件方法
/// </summary>
/// <param name="smtpServer">smtp服务器信息,如"username:password@www.smtpServer.com:25",也可去掉部分次要信息,如"www.smtpServer.com"</param>
/// <param name="from">发件人mail地址</param>
/// <param name="fromname">发件人姓名</param>
/// <param name="to">收件人地址列表</param>
/// <param name="toname">收件人姓名</param>
/// <param name="html">是否HTML邮件</param>
/// <param name="subject">邮件主题</param>
/// <param name="body">邮件正文</param>
/// <param name="filePath">邮件附件列表</param>
public bool Send(string smtpServer,string from,string fromName,string[] recipientADD,string recipientName,bool isHtml,string subject,Priority priority, string body,string[] filePath)
{
//如果收件人多于服务器可同时发送的最大值,则分多次发送
if(recipientADD.Length > RecipientMaxNum)
{
string[] recipientADD1 = new string[RecipientMaxNum];
string[] recipientADD2 = new string[recipientADD.Length - RecipientMaxNum];
for(int i = 0;i < recipientADD.Length; i++)
{
if(i < RecipientMaxNum)
{
recipientADD1[i] = recipientADD[i];
}
else
{
recipientADD2[i - RecipientMaxNum] = recipientADD[i];
}
}
return Send(smtpServer,from,fromName,recipientADD1,recipientName,isHtml,subject,priority, body,filePath)
&&
Send(smtpServer,from,fromName,recipientADD2,recipientName,isHtml,subject,priority, body,filePath);
}
{
m_logs.Remove(0,m_logs.Length);
}
int mailserverport=25;// 邮件服务器端口号
string userName="";// SMTP认证时使用的用户名
string password="";// SMTP认证时使用的密码
bool needSmtp=false;// 是否需要SMTP验证
SetMailDomain(smtpServer,out mailServer,out userName,out password,
out mailserverport,out needSmtp);
if(mailServer.Trim()=="")
{
m_logs.Append("必须指定SMTP服务器"+ENTER);
return false;
}
IList attachments = AddAttachment(filePath);// 邮件附件列表
Hashtable recipients = AddRecipient(recipientADD);// 收件人列表
if(recipients == null || recipients.Count == 0 )
{
m_logs.Append("必须指定收件人"+ENTER);
return false;
}
if(recipients.Count > RecipientMaxNum)
{
m_logs.Append("一次发送的收件人太多"+ENTER);
return false;
}
bool isSuccessful = false;
lock(this)
{
TcpClient tcpClientObject = null;// TcpClient对象,用于连接服务器
NetworkStream networkStreamObject = null;// NetworkStream对象
DateTime dateTimeBegin = DateTime.Now;
int useTime = 0;
while(! ( useTime > RepeatTime || isSuccessful))
{
try
{
tcpClientObject=new TcpClient(mailServer,mailserverport);
networkStreamObject = tcpClientObject.GetStream();
isSuccessful =SendEmail(networkStreamObject,needSmtp,mailServer,userName,password,recipients,from,
fromName,recipientName,subject,priority.ToString(),attachments, isHtml, body);
}
catch(Exception e)
{
m_logs.Append("错误:"+e.Message+ENTER);
}
finally
{
if(networkStreamObject!=null)networkStreamObject.Close();
if(tcpClientObject!=null)tcpClientObject.Close();
if(!isSuccessful)
{
string n = Thread.CurrentThread.Name;
Thread.Sleep(WaitTime);
}
useTime = ((TimeSpan)(DateTime.Now - dateTimeBegin)).Seconds;
}
}
}
return isSuccessful;
}
/// SMTP回应代码哈希表
/// </summary>
private void SMTPCodeAdd()
{
m_ErrCodeHT.Add("500","邮箱地址错误");
m_ErrCodeHT.Add("501","参数格式错误");
m_ErrCodeHT.Add("502","命令不可实现");
m_ErrCodeHT.Add("503","服务器需要SMTP验证");
m_ErrCodeHT.Add("504","命令参数不可实现");
m_ErrCodeHT.Add("421","服务未就绪,关闭传输信道");
m_ErrCodeHT.Add("450","要求的邮件操作未完成,邮箱不可用(例如,邮箱忙)");
m_ErrCodeHT.Add("550","要求的邮件操作未完成,邮箱不可用(例如,邮箱未找到,或不可访问)");
m_ErrCodeHT.Add("451","放弃要求的操作;处理过程中出错");
m_ErrCodeHT.Add("551","用户非本地,请尝试<forward-path>");
m_ErrCodeHT.Add("452","系统存储不足,要求的操作未执行");
m_ErrCodeHT.Add("552","过量的存储分配,要求的操作未执行");
m_ErrCodeHT.Add("553","邮箱名不可用,要求的操作未执行(例如邮箱格式错误)");
m_ErrCodeHT.Add("432","需要一个密码转换");
m_ErrCodeHT.Add("534","认证机制过于简单");
m_ErrCodeHT.Add("538","当前请求的认证机制需要加密");
m_ErrCodeHT.Add("454","临时认证失败");
m_ErrCodeHT.Add("530","需要认证");
m_RightCodeHT.Add("250","要求的邮件操作完成");
m_RightCodeHT.Add("251","用户非本地,将转发向<forward-path>");
m_RightCodeHT.Add("354","开始邮件输入,以<enter>.<enter>结束");
m_RightCodeHT.Add("221","服务关闭传输信道");
m_RightCodeHT.Add("334","服务器响应验证Base64字符串");
m_RightCodeHT.Add("235","验证成功");
}
/// 将字符串编码为Base64字符串
/// </summary>
/// <param name="str">要编码的字符串</param>
private string Base64Encode(string str)
{
byte[] barray;
barray=Encoding.Default.GetBytes(str);
return Convert.ToBase64String(barray);
}
/// 将Base64字符串解码为普通字符串
/// </summary>
/// <param name="str">要解码的字符串</param>
private string Base64Decode(string str)
{
byte[] barray;
barray=Convert.FromBase64String(str);
return Encoding.Default.GetString(barray);
}
/// 得到上传附件的文件流
/// </summary>
/// <param name="filePath">附件的绝对路径</param>
private string GetStream(string filePath)
{
byte[] by = null;
System.IO.FileStream FileStr = null;
string streamString = "";
try
{
//建立文件流对象
FileStr=new System.IO.FileStream(filePath,Syste
using System.Text;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Collections;
using System.Threading; namespace Deep.SendEmail
{
#region AspNetPager Server Control
/// <summary>
/// 邮件可以通过 Microsoft Windows 2000 中内置的 SMTP 邮件服务或任意 SMTP 服务器来传送
/// </summary>
public class SmtpMail
{
private const string ENTER="\r\n";
/// <summary>
/// 设定语言代码,默认设定为GB2312,如不需要可设置为""
/// </summary>
private string m_charset="GB2312";
/// <summary>
/// 服务器交互记录
/// </summary>
private StringBuilder m_logs = new StringBuilder();
private string m_ErrCode;
/// <summary>
/// SMTP错误代码哈希表
/// </summary>
private Hashtable m_ErrCodeHT = new Hashtable();
/// <summary>
/// SMTP正确代码哈希表
/// </summary>
private Hashtable m_RightCodeHT = new Hashtable();
/// 最多收件人数量
/// </summary>
private int m_recipientMaxnum = 2;
/// <summary>
/// 重复时间,以秒为单位
/// </summary>
private int m_RepeatTime = 120;
/// <summary>
/// 服务器出错或拒绝后的等待时间,以毫秒为单位
/// </summary>
private int m_WaitTime = 20000;
/// <summary>
/// 初始化 <see cref="Lion.Web.Mail.SmtpMail"/> 类的新实例
/// </summary>
public SmtpMail()
{
SMTPCodeAdd();
}
#region Properties 定义属性
/// <summary>
/// 服务器交互记录,如发现本组件不能使用的SMTP服务器,请将出错时的Logs发给我(lion-a@sohu.com),我将尽快查明原因。
/// </summary>
public string Logs
{
get
{
return m_logs.ToString();
}
}
/// <summary>
/// 最多收件人数量
/// </summary>
public int RecipientMaxNum
{
set
{
m_recipientMaxnum = value;
}
get
{
return m_recipientMaxnum;
}
}
/// <summary>
/// 设定语言代码,默认设定为GB2312,如不需要可设置为""
/// </summary>
public string Charset
{
get
{
return this.m_charset;
}
set
{
this.m_charset = value;
}
}
/// 重复时间,以秒为单位
/// </summary>
public int RepeatTime
{
get {return m_RepeatTime;}
set {m_RepeatTime = value;}
}
/// <summary>
/// 服务器出错或拒绝后的等待时间,以毫秒为单位
/// </summary>
public int WaitTime
{
get {return m_WaitTime;}
set {m_WaitTime = value > 10000?value:10000;}
}
#endregion
#region Methods 定义方法
/// <summary>
/// 邮件服务器域名和验证信息
/// 形如:"user:pass@www.server.com:25",也可省略次要信息。如"user:pass@www.server.com"或"www.server.com"
/// </summary>
/// <param name="mailDomain">输入用户名、密码、邮件服务器域名、端口号</param>
/// <param name="mailServer">返回邮件服务器域名</param>
/// <param name="mailServerUserName">返回用户名</param>
/// <param name="password">返回密码</param>
/// <param name="mailserverport">返回端口号</param>
/// <param name="needSmtp">返回是否需要SMTP验证</param>
/// <returns></returns>
private bool SetMailDomain(string mailDomain,out string mailServer,out string mailServerUserName,out string password,
out int mailserverport,out bool needSmtp)
{
bool isRight = false;
//为输出变量赋初值
mailServer = string.Empty;
mailServerUserName = String.Empty;
password = String.Empty;
mailserverport = 25;
needSmtp = false;
mailserver
= mailDomain.Trim();int tempint;
if( mailServer != "" )
{
tempint = mailServer.IndexOf("@");
isRight = true;
if(tempint!=-1)
{
string str = mailServer.Substring(0,tempint);
mailServerUserName = str.Substring(0,str.IndexOf(":"));
password = str.Substring(str.IndexOf(":")+1,str.Length-str.IndexOf(":")-1);
needSmtp = !(password==string.Empty);
mailServer = mailDomain.Substring(tempint+1,mailDomain.Length-tempint-1);
}
tempint
= mailServer.IndexOf(":");if(tempint != -1)
{
mailserverport = System.Convert.ToInt32(mailServer.Substring(tempint+1,mailServer.Length-tempint-1));
mailServer = mailServer.Substring(0,tempint);
}
}
return isRight;
}
/// <summary>
/// 添加邮件附件
/// </summary>
/// <param name="filePath">附件绝对路径</param>
private IList AddAttachment(params string[] filePath)
{
if(filePath == null || filePath.Length == 0)
{
return null;
}
IList m_Attachments = new System.Collections.ArrayList();// 邮件附件列表
for(int i=0;i<filePath.Length;i++)
{
if(File.Exists(filePath[i]))
{
m_Attachments.Add(filePath[i]);
}
else
{
m_logs.Append("错误:没找到文件名为"+filePath[i]+"的附件文件!"+ENTER);
}
}
return m_Attachments;
}
/// <summary>
/// 添加一组收件人(不超过m_recipientMaxnum个),参数为字符串数组
/// </summary>
/// <param name="recipients">保存有收件人地址的字符串数组(不超过m_recipientMaxnum个)</param>
private Hashtable AddRecipient(params string[] recipients)
{
if(recipients==null || recipients.Length == 0)
{
return null;
}
Hashtable recipientList=new Hashtable();// 收件人列表
for(int i=0;i<recipients.Length;i++)
{
string recipient = recipients[i].Trim();
if(recipient !=String.Empty && recipient.IndexOf("@") != -1)
{
recipientList.Add(recipientList.Count,recipients[i]);
}
}
return recipientList;
}
/// <summary>
/// 发送邮件方法
/// </summary>
/// <param name="smtpServer">smtp服务器信息,如"username:password@www.smtpServer.com:25",也可去掉部分次要信息,如"www.smtpServer.com"</param>
/// <param name="from">发件人mail地址</param>
/// <param name="fromname">发件人姓名</param>
/// <param name="to">收件人地址列表</param>
/// <param name="toname">收件人姓名</param>
/// <param name="html">是否HTML邮件</param>
/// <param name="subject">邮件主题</param>
/// <param name="body">邮件正文</param>
/// <param name="filePath">邮件附件列表</param>
public bool Send(string smtpServer,string from,string fromName,string[] recipientADD,string recipientName,bool isHtml,string subject,Priority priority, string body,string[] filePath)
{
//如果收件人多于服务器可同时发送的最大值,则分多次发送
if(recipientADD.Length > RecipientMaxNum)
{
string[] recipientADD1 = new string[RecipientMaxNum];
string[] recipientADD2 = new string[recipientADD.Length - RecipientMaxNum];
for(int i = 0;i < recipientADD.Length; i++)
{
if(i < RecipientMaxNum)
{
recipientADD1[i] = recipientADD[i];
}
else
{
recipientADD2[i - RecipientMaxNum] = recipientADD[i];
}
}
return Send(smtpServer,from,fromName,recipientADD1,recipientName,isHtml,subject,priority, body,filePath)
&&
Send(smtpServer,from,fromName,recipientADD2,recipientName,isHtml,subject,priority, body,filePath);
}
if(m_logs.length > 2048)
{
m_logs.Remove(0,m_logs.Length);
}
string mailServer="";// 邮件服务器域名
int mailserverport=25;// 邮件服务器端口号
string userName="";// SMTP认证时使用的用户名
string password="";// SMTP认证时使用的密码
bool needSmtp=false;// 是否需要SMTP验证
SetMailDomain(smtpServer,out mailServer,out userName,out password,
out mailserverport,out needSmtp);
if(mailServer.Trim()=="")
{
m_logs.Append("必须指定SMTP服务器"+ENTER);
return false;
}
IList attachments = AddAttachment(filePath);// 邮件附件列表
Hashtable recipients = AddRecipient(recipientADD);// 收件人列表
if(recipients == null || recipients.Count == 0 )
{
m_logs.Append("必须指定收件人"+ENTER);
return false;
}
if(recipients.Count > RecipientMaxNum)
{
m_logs.Append("一次发送的收件人太多"+ENTER);
return false;
}
bool isSuccessful = false;
lock(this)
{
TcpClient tcpClientObject = null;// TcpClient对象,用于连接服务器
NetworkStream networkStreamObject = null;// NetworkStream对象
DateTime dateTimeBegin = DateTime.Now;
int useTime = 0;
while(! ( useTime > RepeatTime || isSuccessful))
{
try
{
tcpClientObject=new TcpClient(mailServer,mailserverport);
networkStreamObject = tcpClientObject.GetStream();
isSuccessful =SendEmail(networkStreamObject,needSmtp,mailServer,userName,password,recipients,from,
fromName,recipientName,subject,priority.ToString(),attachments, isHtml, body);
}
catch(Exception e)
{
m_logs.Append("错误:"+e.Message+ENTER);
}
finally
{
if(networkStreamObject!=null)networkStreamObject.Close();
if(tcpClientObject!=null)tcpClientObject.Close();
if(!isSuccessful)
{
string n = Thread.CurrentThread.Name;
Thread.Sleep(WaitTime);
}
useTime = ((TimeSpan)(DateTime.Now - dateTimeBegin)).Seconds;
}
}
}
return isSuccessful;
}
#endregion
#region Private Helper Functions
/// <summary>
/// SMTP回应代码哈希表
/// </summary>
private void SMTPCodeAdd()
{
m_ErrCodeHT.Add("500","邮箱地址错误");
m_ErrCodeHT.Add("501","参数格式错误");
m_ErrCodeHT.Add("502","命令不可实现");
m_ErrCodeHT.Add("503","服务器需要SMTP验证");
m_ErrCodeHT.Add("504","命令参数不可实现");
m_ErrCodeHT.Add("421","服务未就绪,关闭传输信道");
m_ErrCodeHT.Add("450","要求的邮件操作未完成,邮箱不可用(例如,邮箱忙)");
m_ErrCodeHT.Add("550","要求的邮件操作未完成,邮箱不可用(例如,邮箱未找到,或不可访问)");
m_ErrCodeHT.Add("451","放弃要求的操作;处理过程中出错");
m_ErrCodeHT.Add("551","用户非本地,请尝试<forward-path>");
m_ErrCodeHT.Add("452","系统存储不足,要求的操作未执行");
m_ErrCodeHT.Add("552","过量的存储分配,要求的操作未执行");
m_ErrCodeHT.Add("553","邮箱名不可用,要求的操作未执行(例如邮箱格式错误)");
m_ErrCodeHT.Add("432","需要一个密码转换");
m_ErrCodeHT.Add("534","认证机制过于简单");
m_ErrCodeHT.Add("538","当前请求的认证机制需要加密");
m_ErrCodeHT.Add("454","临时认证失败");
m_ErrCodeHT.Add("530","需要认证");
m_rightcodeht.add(
"220","服务就绪");m_RightCodeHT.Add("250","要求的邮件操作完成");
m_RightCodeHT.Add("251","用户非本地,将转发向<forward-path>");
m_RightCodeHT.Add("354","开始邮件输入,以<enter>.<enter>结束");
m_RightCodeHT.Add("221","服务关闭传输信道");
m_RightCodeHT.Add("334","服务器响应验证Base64字符串");
m_RightCodeHT.Add("235","验证成功");
}
/// <summary>
/// 将字符串编码为Base64字符串
/// </summary>
/// <param name="str">要编码的字符串</param>
private string Base64Encode(string str)
{
byte[] barray;
barray=Encoding.Default.GetBytes(str);
return Convert.ToBase64String(barray);
}
/// <summary>
/// 将Base64字符串解码为普通字符串
/// </summary>
/// <param name="str">要解码的字符串</param>
private string Base64Decode(string str)
{
byte[] barray;
barray=Convert.FromBase64String(str);
return Encoding.Default.GetString(barray);
}
/// 得到上传附件的文件流
/// </summary>
/// <param name="filePath">附件的绝对路径</param>
private string GetStream(string filePath)
{
byte[] by = null;
System.IO.FileStream FileStr = null;
string streamString = "";
try
{
//建立文件流对象
FileStr=new System.IO.FileStream(filePath,Syste