//Created by Chen****
using System;
using System.IO;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Collections;
using System.Collections.Specialized;
using KSN.Exceptions;
using KSN.Validate;
namespace KSN.Web.Mail
{
///
1<summary>
2/// 邮件内容
3/// </summary>
public class MailMessage
{
private string sender=null;
private StringCollection receivers=new StringCollection();
private string subject="";
private string xMailer="";
private StringCollection attachments=new StringCollection();
private MailEncodings mailEncoding=MailEncodings.GB2312;
private MailTypes mailType=MailTypes.Html;
private byte[] mailBody=null;
///
1<summary>
2/// 获取或设置发件人
3/// </summary>
public string Sender
{
get{return this.sender;}
set{this.sender=value;}
}
///
1<summary>
2/// 获取收件人地址集合
3/// </summary>
public StringCollection Receivers
{
get{return this.receivers;}
}
///
1<summary>
2/// 获取或设置邮件主题
3/// </summary>
public string Subject
{
get{return this.subject;}
set{this.subject=value;}
}
///
1<summary>
2/// 获取或设置邮件传送者
3/// </summary>
public string XMailer
{
get{return this.xMailer;}
set{this.xMailer=value;}
}
///
1<summary>
2/// 获取附件列表
3/// </summary>
public StringCollection Attachments
{
get{return this.attachments;}
}
///
1<summary>
2/// 获取或设置邮件的编码方式
3/// </summary>
public MailEncodings MailEncoding
{
get{return this.mailEncoding;}
set{this.mailEncoding=value;}
}
///
1<summary>
2/// 获取或设置邮件格式
3/// </summary>
public MailTypes MailType
{
get{return this.mailType;}
set{this.mailType=value;}
}
///
1<summary>
2/// 获取或设置邮件正文
3/// </summary>
public byte[] MailBody
{
get{return this.mailBody;}
set{this.mailBody=value;}
}
}
///
1<summary>
2/// 邮件编码
3/// </summary>
public enum MailEncodings
{
GB2312,
ASCII,
Unicode,
UTF8
}
///
1<summary>
2/// 邮件格式
3/// </summary>
public enum MailTypes
{
Html,
Text
}
///
1<summary>
2/// smtp服务器的验证方式
3/// </summary>
public enum SmtpValidateTypes
{
///
1<summary>
2/// 不需要验证
3/// </summary>
None,
///
1<summary>
2/// 通用的auth login验证
3/// </summary>
Login,
///
1<summary>
2/// 通用的auth plain验证
3/// </summary>
Plain,
///
1<summary>
2/// CRAM-MD5验证
3/// </summary>
CRAMMD5
}
///
1<summary>
2/// 邮件发送类
3/// </summary>
public class KSN_Smtp
{
#region "member fields"
///
1<summary>
2/// 连接对象
3/// </summary>
private TcpClient tc;
///
1<summary>
2/// 网络流
3/// </summary>
private NetworkStream ns;
///
1<summary>
2/// 错误的代码字典
3/// </summary>
private StringDictionary errorCodes=new StringDictionary();
///
1<summary>
2/// 操作执行成功后的响应代码字典
3/// </summary>
private StringDictionary rightCodes=new StringDictionary();
///
1<summary>
2/// 执行过程中错误的消息
3/// </summary>
private string errorMessage="";
///
1<summary>
2/// 记录操作日志
3/// </summary>
private string logs="";
///
1<summary>
2/// 主机登陆的验证方式
3/// </summary>
private StringCollection validateTypes=new StringCollection();
///
1<summary>
2/// 换行常数
3/// </summary>
private const string CRLF="\r\n";
private string serverName="smtp";
private string logPath=null;
private string userid=null;
private string password=null;
private string mailEncodingName="GB2312";
private bool sendIsComplete=false;
private SmtpValidateTypes smtpValidateType=SmtpValidateTypes.Login;
#endregion
#region "propertys"
///
1<summary>
2/// 获取最后一此程序执行中的错误消息
3/// </summary>
public string ErrorMessage
{
get{return this.errorMessage;}
}
///
1<summary>
2/// 获取或设置日志输出路径
3/// </summary>
public string LogPath
{
get
{
return this.logPath;
}
set{this.logPath=value;}
}
///
1<summary>
2/// 获取或设置登陆smtp服务器的帐号
3/// </summary>
public string UserID
{
get{return this.userid;}
set{this.userid=value;}
}
///
1<summary>
2/// 获取或设置登陆smtp服务器的密码
3/// </summary>
public string Password
{
get{return this.password;}
set{this.password=value;}
}
///
1<summary>
2/// 获取或设置要使用登陆Smtp服务器的验证方式
3/// </summary>
public SmtpValidateTypes SmtpValidateType
{
get{return this.smtpValidateType;}
set{this.smtpValidateType=value;}
}
#endregion
#region "construct functions"
///
1<summary>
2/// 构造函数
3/// </summary>
///
1<param name="server"/>
主机名
///
1<param name="port"/>
端口
public KSN_Smtp(string server,int port)
{
tc=new TcpClient(server,port);
ns=tc.GetStream();
this.serverName=server;
this.initialFields();
}
///
1<summary>
2/// 构造函数
3/// </summary>
///
1<param name="ip"/>
主机ip
///
1<param name="port"/>
端口
public KSN_Smtp(IPAddress ip,int port)
{
IPEndPoint endPoint=new IPEndPoint(ip,port);
tc=new TcpClient(endPoint);
ns=tc.GetStream();
this.serverName=ip.ToString();
this.initialFields();
}
#endregion
#region "methods"
private void initialFields() //初始化连接
{
logs="================"+DateTime.Now.ToLongDateString()+" "+DateTime.Now.ToLongTimeString()+"==============="+CRLF;
//*****************************************************************
//错误的状态码
//*****************************************************************
errorCodes.Add("421","服务未就绪,关闭传输通道");
errorCodes.Add("432","需要一个密码转换");
errorCodes.Add("450","要求的邮件操作未完成,邮箱不可用(如:邮箱忙)");
errorCodes.Add("451","放弃要求的操作,要求的操作未执行");
errorCodes.Add("452","系统存储不足,要求的操作未完成");
errorCodes.Add("454","临时的认证失败");
errorCodes.Add("500","邮箱地址错误");
errorCodes.Add("501","参数格式错误");
errorCodes.Add("502","命令不可实现");
errorCodes.Add("503","命令的次序不正确");
errorCodes.Add("504","命令参数不可实现");
errorCodes.Add("530","需要认证");
errorCodes.Add("534","认证机制过于简单");
errorCodes.Add("538","当前请求的认证机制需要加密");
errorCodes.Add("550","当前的邮件操作未完成,邮箱不可用(如:邮箱未找到或邮箱不能用)");
errorCodes.Add("551","用户非本地,请尝试
1<forward-path>");
2errorCodes.Add("552","过量的存储分配,制定的操作未完成");
3errorCodes.Add("553","邮箱名不可用,如:邮箱地址的格式错误");
4errorCodes.Add("554","传送失败");
5errorCodes.Add("535","用户身份验证失败");
6//****************************************************************
7//操作执行成功后的状态码
8//****************************************************************
9rightCodes.Add("220","服务就绪");
10rightCodes.Add("221","服务关闭传输通道");
11rightCodes.Add("235","验证成功");
12rightCodes.Add("250","要求的邮件操作完成");
13rightCodes.Add("251","非本地用户,将转发向<forward-path>");
14rightCodes.Add("334","服务器响应验证Base64字符串");
15rightCodes.Add("354","开始邮件输入,以<crlf>.<crlf>结束");
16//读取系统回应
17StreamReader reader=new StreamReader(ns);
18logs+=reader.ReadLine()+CRLF;
19}
20/// <summary>
21/// 向SMTP发送命令
22/// </summary>
23/// <param name="cmd"/>
24private string sendCommand(string cmd,bool isMailData)
25{
26if(cmd!=null && cmd.Trim()!=string.Empty)
27{
28byte[] cmd_b=null;
29if(!isMailData)//不是邮件数据
30cmd+=CRLF;
31
32logs+=cmd;
33//开始写入邮件数据
34if(!isMailData)
35{
36cmd_b=Encoding.ASCII.GetBytes(cmd);
37ns.Write(cmd_b,0,cmd_b.Length);
38}
39else
40{
41cmd_b=Encoding.GetEncoding(this.mailEncodingName).GetBytes(cmd);
42ns.BeginWrite(cmd_b,0,cmd_b.Length,new AsyncCallback(this.asyncCallBack),null);
43}
44//读取服务器响应
45StreamReader reader=new StreamReader(ns);
46string response=reader.ReadLine();
47logs+=response+CRLF;
48//检查状态码
49string statusCode=response.Substring(0,3);
50bool isExist=false;
51bool isRightCode=true;
52foreach(string err in this.errorCodes.Keys)
53{
54if(statusCode==err)
55{
56isExist=true;
57isRightCode=false;
58break;
59}
60}
61foreach(string right in this.rightCodes.Keys)
62{
63if(statusCode==right)
64{
65isExist=true;
66break;
67}
68}
69//根据状态码来处理下一步的动作
70if(!isExist) //不是合法的SMTP主机
71{
72this.setError("不是合法的SMTP主机,或服务器拒绝服务");
73}
74else if(!isRightCode)//命令没能成功执行
75{
76this.setError(statusCode+":"+this.errorCodes[statusCode]);
77}
78else //命令成功执行
79{
80this.errorMessage="";
81}
82return response;
83}
84else
85{
86return null;
87}
88}
89/// <summary>
90/// 通过auth login方式登陆smtp服务器
91/// </summary>
92private void landingByLogin()
93{
94string base64UserId=this.convertBase64String(this.UserID,"ASCII");
95string base64Pass=this.convertBase64String(this.Password,"ASCII");
96//握手
97this.sendCommand("helo "+this.serverName,false);
98//开始登陆
99this.sendCommand("auth login",false);
100this.sendCommand(base64UserId,false);
101this.sendCommand(base64Pass,false);
102}
103/// <summary>
104/// 通过auth plain方式登陆服务器
105/// </summary>
106private void landingByPlain()
107{
108string NULL=((char)0).ToString();
109string loginStr=NULL+this.UserID+NULL+this.Password;
110string base64LoginStr=this.convertBase64String(loginStr,"ASCII");
111//握手
112this.sendCommand("helo "+this.serverName,false);
113//登陆
114this.sendCommand(base64LoginStr,false);
115}
116/// <summary>
117/// 通过auth CRAM-MD5方式登陆
118/// </summary>
119private void landingByCRAMMD5()
120{
121//握手
122this.sendCommand("helo "+this.serverName,false);
123//登陆
124string response=this.sendCommand("auth CRAM-MD5",false);
125//得到服务器返回的标识
126string identifier=response.Remove(0,4);
127//用MD5加密标识
128KSN_MACTripleDES mac=new KSN_MACTripleDES();
129mac.Key=this.Password;
130mac.Data=Encoding.ASCII.GetBytes(identifier);
131string hash=mac.GetHashValue();
132//发送用户帐号信息
133this.sendCommand(this.UserID+" "+hash,false);
134}
135/// <summary>
136/// 发送邮件
137/// </summary>
138/// <returns></returns>
139public bool SendMail(MailMessage mail)
140{
141bool isSended=true;
142try
143{
144//检测发送邮件的必要条件
145if(mail.Sender==null)
146{
147this.setError("没有设置发信人");
148}
149if(mail.Receivers.Count==0)
150{
151this.setError("至少要有一个收件人");
152}
153if(this.SmtpValidateType!=SmtpValidateTypes.None)
154{
155if(this.userid==null || this.password==null)
156{
157this.setError("当前设置需要smtp验证,但是没有给出登陆帐号");
158}
159}
160//开始登陆
161switch(this.SmtpValidateType)
162{
163case SmtpValidateTypes.None:
164this.sendCommand("helo "+this.serverName,false);
165break;
166case SmtpValidateTypes.Login:
167this.landingByLogin();
168break;
169case SmtpValidateTypes.Plain:
170this.landingByPlain();
171break;
172case SmtpValidateTypes.CRAMMD5:
173this.landingByCRAMMD5();
174break;
175default:
176break;
177}
178//初始化邮件会话(对应SMTP命令mail)
179this.sendCommand("mail from:<"+mail.Sender+">",false);
180//标识收件人(对应SMTP命令Rcpt)
181foreach(string receive in mail.Receivers)
182{
183this.sendCommand("rcpt to:<"+receive+">",false);
184}
185//标识开始输入邮件内容(Data)
186this.sendCommand("data",false);
187//开始编写邮件内容
188string message="Subject:"+mail.Subject+CRLF;
189message+="X-mailer:"+mail.XMailer+CRLF;
190message+="MIME-Version:1.0"+CRLF;
191if(mail.Attachments.Count==0)//没有附件
192{
193if(mail.MailType==MailTypes.Text) //文本格式
194{
195message+="Content-Type:text/plain;"+CRLF+" ".PadRight(8,' ')+"charset=\""+
196mail.MailEncoding.ToString()+"\""+CRLF;
197message+="Content-Transfer-Encoding:base64"+CRLF+CRLF;
198if(mail.MailBody!=null)
199message+=Convert.ToBase64String(mail.MailBody,0,mail.MailBody.Length)+CRLF+CRLF+CRLF+"."+CRLF;
200}
201else//Html格式
202{
203message+="Content-Type:multipart/alertnative;"+CRLF+" ".PadRight(8,' ')+"boundary"
204+"=\"=====003_Dragon310083331177_=====\""+CRLF+CRLF+CRLF;
205message+="This is a multi-part message in MIME format"+CRLF+CRLF;
206message+="--=====003_Dragon310083331177_====="+CRLF;
207message+="Content-Type:text/html;"+CRLF+" ".PadRight(8,' ')+"charset=\""+
208mail.MailEncoding.ToString().ToLower()+"\""+CRLF;
209message+="Content-Transfer-Encoding:base64"+CRLF+CRLF;
210if(mail.MailBody!=null)
211message+=Convert.ToBase64String(mail.MailBody,0,mail.MailBody.Length)+CRLF+CRLF;
212message+="--=====003_Dragon310083331177_=====--"+CRLF+CRLF+CRLF+"."+CRLF;
213}
214}
215else//有附件
216{
217//处理要在邮件中显示的每个附件的数据
218StringCollection attatchmentDatas=new StringCollection();
219foreach(string path in mail.Attachments)
220{
221if(!File.Exists(path))
222{
223this.setError("指定的附件没有找到"+path);
224}
225else
226{
227//得到附件的字节流
228FileInfo file=new FileInfo(path);
229FileStream fs=new FileStream(path,FileMode.Open,FileAccess.Read);
230if(fs.Length>(long)int.MaxValue)
231{
232this.setError("附件的大小超出了最大限制");
233}
234byte[] file_b=new byte[(int)fs.Length];
235fs.Read(file_b,0,file_b.Length);
236fs.Close();
237string attatchmentMailStr="Content-Type:application/octet-stream;"+CRLF+" ".PadRight(8,' ')+"name="+
238"\""+file.Name+"\""+CRLF;
239attatchmentMailStr+="Content-Transfer-Encoding:base64"+CRLF;
240attatchmentMailStr+="Content-Disposition:attachment;"+CRLF+" ".PadRight(8,' ')+"filename="+
241"\""+file.Name+"\""+CRLF+CRLF;
242attatchmentMailStr+=Convert.ToBase64String(file_b,0,file_b.Length)+CRLF+CRLF;
243attatchmentDatas.Add(attatchmentMailStr);
244}
245}
246//设置邮件信息
247if(mail.MailType==MailTypes.Text) //文本格式
248{
249message+="Content-Type:multipart/mixed;"+CRLF+" ".PadRight(8,' ')+"boundary=\"=====001_Dragon320037612222_=====\""
250+CRLF+CRLF;
251message+="This is a multi-part message in MIME format."+CRLF+CRLF;
252message+="--=====001_Dragon320037612222_====="+CRLF;
253message+="Content-Type:text/plain;"+CRLF+" ".PadRight(8,' ')+"charset=\""+mail.MailEncoding.ToString().ToLower()+"\""+CRLF;
254message+="Content-Transfer-Encoding:base64"+CRLF+CRLF;
255if(mail.MailBody!=null)
256message+=Convert.ToBase64String(mail.MailBody,0,mail.MailBody.Length)+CRLF;
257foreach(string s in attatchmentDatas)
258{
259message+="--=====001_Dragon320037612222_====="+CRLF+s+CRLF+CRLF;
260}
261message+="--=====001_Dragon320037612222_=====--"+CRLF+CRLF+CRLF+"."+CRLF;
262}
263else
264{
265message+="Content-Type:multipart/mixed;"+CRLF+" ".PadRight(8,' ')+"boundary=\"=====001_Dragon255511664284_=====\""
266+CRLF+CRLF;
267message+="This is a multi-part message in MIME format."+CRLF+CRLF;
268message+="--=====001_Dragon255511664284_====="+CRLF;
269message+="Content-Type:text/html;"+CRLF+" ".PadRight(8,' ')+"charset=\""+mail.MailEncoding.ToString().ToLower()+"\""+CRLF;
270message+="Content-Transfer-Encoding:base64"+CRLF+CRLF;
271if(mail.MailBody!=null)
272message+=Convert.ToBase64String(mail.MailBody,0,mail.MailBody.Length)+CRLF+CRLF;
273for(int i=0;i<attatchmentdatas.count;i++) +crlf+attatchmentdatas[i]+crlf+crlf;="" +crlf+crlf+crlf+"."+crlf;="" <summary="" byte[]="" catch="" else="" filestream="" filestream(this.logpath,filemode.append,fileaccess.write);="" filestream(this.logpath,filemode.create,fileaccess.write);="" finally="" fs="new" fs.close();="" fs.write(logpath_b,0,logpath_b.length);="" if(file.exists(this.logpath))="" if(this.logpath!="null)" if(this.sendiscomplete)="" issended="false;" issended;="" logpath_b='Encoding.GetEncoding("gb2312").GetBytes(this.logs);' message+="--=====001_Dragon255511664284_=====--" return="" this.disconnect();="" this.logs="CRLF+CRLF+this.logs;" this.mailencodingname="mail.MailEncoding.ToString();" this.sendcommand("quit",false);="" this.sendcommand(message,true);="" {="" }="" 发送邮件数据="" 输出日志文件="">
274/// 异步写入数据
275///
276/// <param name="result"/>
277private void asyncCallBack(IAsyncResult result)
278{
279if(result.IsCompleted)
280this.sendIsComplete=true;
281}
282/// <summary>
283/// 关闭连接
284/// </summary>
285private void disconnect()
286{
287try
288{
289ns.Close();
290tc.Close();
291}
292catch
293{
294;
295}
296}
297/// <summary>
298/// 设置出现错误时的动作
299/// </summary>
300/// <param name="errorStr"/>
301private void setError(string errorStr)
302{
303this.errorMessage=errorStr;
304logs+=this.errorMessage+CRLF+"【邮件处理动作中止】"+CRLF;
305this.disconnect();
306throw new ApplicationException("");
307}
308/// <summary>
309///将字符串转换为base64
310/// </summary>
311/// <param name="str"/>
312/// <param name="encodingName"/>
313/// <returns></returns>
314private string convertBase64String(string str,string encodingName)
315{
316byte[] str_b=Encoding.GetEncoding(encodingName).GetBytes(str);
317return Convert.ToBase64String(str_b,0,str_b.Length);
318}
319#endregion
320}
321}</attatchmentdatas.count;i++)></crlf></crlf></forward-path></forward-path>