在为公司写通知服务时,从网上找到了以上地址,非常感谢原作者创造性的劳动。改写的目的是为了适应作为服务运行的要求:
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 服务器来传送
///
public class SmtpMail
{

private const string ENTER=”rn”;

///
/// 设定语言代码,默认设定为GB2312,如不需要可设置为””
///
private string m_charset=”GB2312″;

///
/// 服务器交互记录
///
private StringBuilder m_logs = new StringBuilder();

private string m_ErrCode;

///
/// SMTP错误代码哈希表
///
private Hashtable m_ErrCodeHT = new Hashtable();

///
/// SMTP正确代码哈希表
///
private Hashtable m_RightCodeHT = new Hashtable();

///
/// 最多收件人数量
///
private int m_recipientMaxnum = 2;

///
/// 重复时间,以秒为单位
///
private int m_RepeatTime = 120;

///
/// 服务器出错或拒绝后的等待时间,以毫秒为单位
///
private int m_WaitTime = 20000;

///
/// 初始化 类的新实例
///
public SmtpMail()
{
SMTPCodeAdd();
}

#region Properties 定义属性
///
///
服务器交互记录,如发现本组件不能使用的SMTP服务器,请将出错时的Logs发给我(lion-a@sohu.com),我将尽快查明原因。
///
public string Logs
{
get
{
return m_logs.ToString();
}
}

///
/// 最多收件人数量
///
public int RecipientMaxNum
{
set
{
m_recipientMaxnum = value;
}
get
{
return m_recipientMaxnum;
}
}

///
/// 设定语言代码,默认设定为GB2312,如不需要可设置为””
///
public string Charset
{
get
{
return this.m_charset;
}
set
{
this.m_charset = value;
}
}

///
/// 重复时间,以秒为单位
///
public int RepeatTime
{
get {return m_RepeatTime;}
set {m_RepeatTime = value;}

}

///
/// 服务器出错或拒绝后的等待时间,以毫秒为单位
///
public int WaitTime
{
get {return m_WaitTime;}
set {m_WaitTime = value > 10000?value:10000;}
}

#endregion

#region Methods 定义方法

///
/// 邮件服务器域名和验证信息
///
形如:”user:pass@www.server.com:25″,也可省略次要信息。如”user:pass@www.server.com”或”www.server.com”
///
/// 输入用户名、密码、邮件服务器域名、端口号
/// 返回邮件服务器域名
/// 返回用户名
/// 返回密码
/// 返回端口号
/// 返回是否需要SMTP验证
///
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;
}

///
/// 添加邮件附件
///
/// 附件绝对路径
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 {
if(File.Exists(filePath[i]))
{
m_Attachments.Add(filePath[i]);
}
else
{
m_logs.Append(“错误:没找到文件名为”+filePath[i]+”的附件文件!”+ENTER);
}
}
return m_Attachments;
}

///
/// 添加一组收件人(不超过m_recipientMaxnum个),参数为字符串数组
///
/// 保存有收件人地址的字符串数组(不超过m_recipientMaxnum个)
private Hashtable AddRecipient(params string[] recipients)
{
if(recipients==null || recipients.Length == 0)
{
return null;
}
Hashtable recipientList=new Hashtable();// 收件人列表

for(int i=0;i {
string recipient = recipients[i].Trim();
if(recipient !=String.Empty && recipient.IndexOf(“@”) != -1)
{
recipientList.Add(recipientList.Count,recipients[i]);
}
}
return recipientList;
}

///
/// 发送邮件方法
///
///
smtp服务器信息,如”username:password@www.smtpServer.com:25″,也可去掉部分次要信息,如”www.smtpServer.com”
/// 发件人mail地址
/// 发件人姓名
/// 收件人地址列表
/// 收件人姓名
/// 是否HTML邮件
/// 邮件主题
/// 邮件正文
/// 邮件附件列表
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;
}

#韦德国际bv1946韦德国际1946手机版,endregion

#region Private Helper Functions

///
/// SMTP回应代码哈希表
///
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″,”用户非本地,请尝试”);
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″,”用户非本地,将转发向”);
m_RightCodeHT.Add(“354″,”开始邮件输入,以.结束”);
m_RightCodeHT.Add(“221″,”服务关闭传输信道”);
m_RightCodeHT.Add(“334″,”服务器响应验证Base64字符串”);
m_RightCodeHT.Add(“235″,”验证成功”);
}

///
/// 将字符串编码为Base64字符串
///
/// 要编码的字符串
private string Base64Encode(string str)
{
byte[] barray;
barray=Encoding.Default.GetBytes(str);
return Convert.ToBase64String(barray);
}

///
/// 将Base64字符串解码为普通字符串
///
/// 要解码的字符串
private string Base64Decode(string str)
{
byte[] barray;
barray=Convert.FromBase64String(str);
return Encoding.Default.GetString(barray);
}

///
/// 得到上传附件的文件流
///
/// 附件的绝对路径
private string GetStream(string filePath)
{
byte[] by = null;
System.IO.FileStream FileStr = null;
string streamString = “”;
try
{
//建立文件流对象
FileStr=new System.IO.FileStream(filePath,System.IO.FileMode.Open);
by=new byte[System.Convert.ToInt32(FileStr.Length)];
FileStr.Read(by,0,by.Length);
streamString = System.Convert.ToBase64String(by);
}
catch(Exception ex)
{
//写错误日志
m_logs.Append(“错误:”+ex.Message+ENTER);
}
finally
{
if(FileStr != null)
{
FileStr.Close();
}
}

return streamString;
}

///
/// 发送SMTP命令
///
private bool SendCommand(string str,NetworkStream
_NetworkStreamObject)
{
byte[] WriteBuffer;
if(str==null||str.Trim()==String.Empty)
{
return true;
}
m_logs.Append(str+ENTER);
WriteBuffer = Encoding.Default.GetBytes(str);
try
{
_NetworkStreamObject.Write(WriteBuffer,0,WriteBuffer.Length);
}
catch(Exception ex)
{
//写日志
m_logs.Append(“错误:”+ex.Message+ENTER);
return false;
}
return true;
}

///
/// 接收SMTP服务器回应
///
private string RecvResponse(NetworkStream _NetworkStreamObject)
{
int StreamSize = 0;
string ReturnValue = String.Empty;
byte[] ReadBuffer = new byte[1024] ;
try
{
StreamSize =
_NetworkStreamObject.Read(ReadBuffer,0,ReadBuffer.Length);
}
catch(Exception ex)
{
//写日志
m_logs.Append(“错误:”+ex.Message+ENTER);
m_ErrCode = ex.Message;
return “false”;
}

if (StreamSize==0)
{
return ReturnValue ;
}
else
{
ReturnValue =
Encoding.Default.GetString(ReadBuffer).Substring(0,StreamSize);
m_logs.Append(ReturnValue+ENTER);
return ReturnValue;
}
}

///
/// 与服务器交互,发送一条命令并接收回应。
///
/// 一个要发送的命令
private bool Dialog(string str,NetworkStream _NetworkStream)
{
if(str==null||str.Trim()==””)
{
return true;
}
if(SendCommand(str,_NetworkStream))
{
string RR=RecvResponse(_NetworkStream);
if(RR==”false”)
{
return false;
}
string RRCode=RR.Substring(0,3);
if(m_RightCodeHT[RRCode]!=null)
{
return true;
}
else
{
m_ErrCode = RRCode;
return false;
}
}
else
{
return false;
}

}

///
/// 与服务器交互,发送一组命令并接收回应。
///

private bool Dialog(string[] str,NetworkStream _NetworkStream)
{
for(int i=0;i {
if(!Dialog(str[i],_NetworkStream))
{
return false;
}
}

return true;
}

///
/// SendEmail
///
///
private bool SendEmail(NetworkStream _NetworkStream,bool
needSmtp,string mailServer,string userName,string password,Hashtable
recipients,string from,
string fromName,string recipientName,string subject,string
priority,IList attachments,bool isHtml,
string body)
{
//验证网络连接是否正确
if(m_RightCodeHT[RecvResponse(_NetworkStream).Substring(0,3)]==null)
{
return false;
}

string[] SendBuffer;
string SendBufferstr;
StringBuilder SendBufferstrBuilder = new StringBuilder();

//进行SMTP验证
if(needSmtp)
{
SendBuffer=new String[4];
SendBuffer[0]=”EHLO ” + mailServer + ENTER;
SendBuffer[1]=”AUTH LOGIN” + ENTER;
SendBuffer[2]=Base64Encode(userName) + ENTER;
SendBuffer[3]=Base64Encode(password) + ENTER;
if(!Dialog(SendBuffer,_NetworkStream))
{
return false;
}
}
else
{
SendBufferstr=”HELO ” + mailServer + ENTER;
if(!Dialog(SendBufferstr,_NetworkStream))
return false;
}

//
SendBufferstr=”MAIL FROM:” + ENTER;
if(!Dialog(SendBufferstr,_NetworkStream))
return false;

//
SendBuffer=new string[m_recipientMaxnum];
for(int i=0;i {
SendBuffer[i]=”RCPT TO:” + ENTER;

}

if(!Dialog(SendBuffer,_NetworkStream))
return false;
SendBufferstr=”DATA” + ENTER;
if(!Dialog(SendBufferstr,_NetworkStream))
return false;

SendBufferstrBuilder.Append(“From:” + fromName + “” +ENTER);

SendBufferstrBuilder.Append(“To:=?”+Charset.ToUpper()+”?B?”+Base64Encode(recipientName)+”?=”+””+ENTER);

SendBufferstrBuilder.Append(“CC:”);
for(int i=0;i {
SendBufferstrBuilder.Append(recipients[i].ToString() + “,”);
}
SendBufferstrBuilder.Append(ENTER);

SendBufferstrBuilder.Append(((subject==String.Empty ||
subject==null)?”Subject:”:((Charset==””)?(“Subject:” +
subject):(“Subject:” + “=?” + Charset.ToUpper() + “?B?” +
Base64Encode(subject) +”?=”))) + ENTER);
SendBufferstrBuilder.Append(“X-Priority:” + priority + ENTER);
SendBufferstrBuilder.Append(“X-MSMail-Priority:” + priority + ENTER);
SendBufferstrBuilder.Append(“Importance:” + priority + ENTER);
SendBufferstrBuilder.Append(“X-Mailer: Lion.Web.Mail.SmtpMail Pubclass
[cn]” + ENTER);
SendBufferstrBuilder.Append(“MIME-Version: 1.0” + ENTER);

if(attachments != null && attachments.Count!=0)
{
SendBufferstrBuilder.Append(“Content-Type: multipart/mixed;” + ENTER);
SendBufferstrBuilder.Append(”
boundary=”=====”+(isHtml?”001_Dragon520636771063_”:”001_Dragon303406132050_”)+”=====””+ENTER+ENTER);
}

if(isHtml)
{
if(attachments != null && attachments.Count==0)
{
SendBufferstrBuilder.Append(“Content-Type:
multipart/alternative;”+ENTER);//内容格式和分隔符
SendBufferstrBuilder.Append(”
boundary=”=====003_Dragon520636771063_=====””+ENTER+ENTER);

SendBufferstrBuilder.Append(“This is a multi-part message in MIME
format.”+ENTER+ENTER);
}
else
{
SendBufferstrBuilder.Append(“This is a multi-part message in MIME
format.”+ENTER+ENTER);
SendBufferstrBuilder.Append(“–=====001_Dragon520636771063_=====”+ENTER);
SendBufferstrBuilder.Append(“Content-Type:
multipart/alternative;”+ENTER);//内容格式和分隔符
SendBufferstrBuilder.Append(”
boundary=”=====003_Dragon520636771063_=====””+ENTER+ENTER);
}
SendBufferstrBuilder.Append(“–=====003_Dragon520636771063_=====”+ENTER);
SendBufferstrBuilder.Append(“Content-Type: text/plain;”+ ENTER);
SendBufferstrBuilder.Append(((Charset==””)?(”
charset=”iso-8859-1″”):(” charset=”” + Charset.ToLower() + “””))

  • ENTER);
    SendBufferstrBuilder.Append(“Content-Transfer-Encoding: base64” + ENTER
  • ENTER);
    SendBufferstrBuilder.Append(Base64Encode(“邮件内容为HTML格式,请选择HTML方式查看”)
  • ENTER + ENTER);

SendBufferstrBuilder.Append(“–=====003_Dragon520636771063_=====”+ENTER);

SendBufferstrBuilder.Append(“Content-Type: text/html;” + ENTER);
SendBufferstrBuilder.Append(((Charset==””)?(”
charset=”iso-8859-1″”):(” charset=”” + Charset.ToLower() + “””))

  • ENTER);
    SendBufferstrBuilder.Append(“Content-Transfer-Encoding: base64” + ENTER
  • ENTER);
    SendBufferstrBuilder.Append(Base64Encode(body) + ENTER + ENTER);
    SendBufferstrBuilder.Append(“–=====003_Dragon520636771063_=====–“+ENTER);
    }
    else
    {
    if(attachments != null && attachments.Count!=0)
    {
    SendBufferstrBuilder.Append(“–=====001_Dragon303406132050_=====”+ENTER);
    }
    SendBufferstrBuilder.Append(“Content-Type: text/plain;” + ENTER);
    SendBufferstrBuilder.Append(((Charset==””)?(”
    charset=”iso-8859-1″”):(” charset=”” + Charset.ToLower() + “””))
  • ENTER);
    SendBufferstrBuilder.Append(“Content-Transfer-Encoding: base64” + ENTER
  • ENTER);
    SendBufferstrBuilder.Append(Base64Encode(body) + ENTER);
    }

//SendBufferstr += “Content-Transfer-Encoding: base64″+ENTER;
if(attachments != null && attachments.Count!=0)
{
for(int i=0;i {
string filepath = (string)attachments[i];
SendBufferstrBuilder.Append(“–=====”+
(isHtml?”001_Dragon520636771063_”:”001_Dragon303406132050_”)
+”=====”+ENTER);
//SendBufferstr += “Content-Type: application/octet-stream”+ENTER;
SendBufferstrBuilder.Append(“Content-Type: text/plain;”+ENTER);
SendBufferstrBuilder.Append(”
name=”=?”+Charset.ToUpper()+”?B?”+Base64Encode(filepath.Substring(filepath.LastIndexOf(“\”)+1))+”?=””+ENTER);
SendBufferstrBuilder.Append(“Content-Transfer-Encoding:
base64″+ENTER);
SendBufferstrBuilder.Append(“Content-Disposition: attachment;”+ENTER);
SendBufferstrBuilder.Append(”
filename=”=?”+Charset.ToUpper()+”?B?”+Base64Encode(filepath.Substring(filepath.LastIndexOf(“\”)+1))+”?=””+ENTER+ENTER);
SendBufferstrBuilder.Append(GetStream(filepath)+ENTER+ENTER);
}
SendBufferstrBuilder.Append(“–=====”+
(isHtml?”001_Dragon520636771063_”:”001_Dragon303406132050_”)
+”=====–“+ENTER+ENTER);
}
SendBufferstrBuilder.Append(ENTER + “.” + ENTER);
SendBufferstr = SendBufferstrBuilder.ToString();

if(!Dialog(SendBufferstr,_NetworkStream))
return false;

SendBufferstr=”QUIT” + ENTER;
if(!Dialog(SendBufferstr,_NetworkStream))
return false;

return true;
}

#endregion

#region
/*
///
/// 添加一个密件收件人
///
/// 收件人地址
public bool AddRecipientBCC(string str)
{
if(str==null||str.Trim()==””)
return true;
if(RecipientBCCNum<10)
{
RecipientBCC.Add(RecipientBCCNum,str);
RecipientBCCNum++;
return true;
}
else
{
m_logs.Append(“错误:收件人过多”);
return false;
}
}

///
/// 添加一组密件收件人(不超过10个),参数为字符串数组
///
/// 保存有收件人地址的字符串数组(不超过10个)
public bool AddRecipientBCC(string[] str)
{
for(int i=0;i {
if(!AddRecipientBCC(str[i]))
{
return false;
}
}
return true;
}

*/
#endregion
}

///
/// 邮件发送优先级
///
public enum Priority
{
High,
Normal,
Low
}

#endregion

}

相关文章