首页 > 开发 > 综合 > 正文

用c#写的smtp邮件发送类

2024-07-21 02:25:41
字体:
来源:转载
供稿:网友
//**********************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
{
/// <summary>
/// 邮件内容
/// </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;
/// <summary>
/// 获取或设置发件人
/// </summary>
public string sender
{
get{return this.sender;}
set{this.sender=value;}
}
/// <summary>
/// 获取收件人地址集合
/// </summary>
public stringcollection receivers
{
get{return this.receivers;}
}
/// <summary>
/// 获取或设置邮件主题
/// </summary>
public string subject
{
get{return this.subject;}
set{this.subject=value;}
}
/// <summary>
/// 获取或设置邮件传送者
/// </summary>
public string xmailer
{
get{return this.xmailer;}
set{this.xmailer=value;}
}
/// <summary>
/// 获取附件列表
/// </summary>
public stringcollection attachments
{
get{return this.attachments;}
}
/// <summary>
/// 获取或设置邮件的编码方式
/// </summary>
public mailencodings mailencoding
{
get{return this.mailencoding;}
set{this.mailencoding=value;}
}
/// <summary>
/// 获取或设置邮件格式
/// </summary>
public mailtypes mailtype
{
get{return this.mailtype;}
set{this.mailtype=value;}
}
/// <summary>
/// 获取或设置邮件正文
/// </summary>
public byte[] mailbody
{
get{return this.mailbody;}
set{this.mailbody=value;}
}
}
/// <summary>
/// 邮件编码
/// </summary>
public enum mailencodings
{
gb2312,
ascii,
unicode,
utf8
}
/// <summary>
/// 邮件格式
/// </summary>
public enum mailtypes
{
html,
text
}
/// <summary>
/// smtp服务器的验证方式
/// </summary>
public enum smtpvalidatetypes
{
/// <summary>
/// 不需要验证
/// </summary>
none,
/// <summary>
/// 通用的auth login验证
/// </summary>
login,
/// <summary>
/// 通用的auth plain验证
/// </summary>
plain,
/// <summary>
/// cram-md5验证
/// </summary>
crammd5
}
/// <summary>
/// 邮件发送类
/// </summary>
public class ksn_smtp
{
#region "member fields"
/// <summary>
/// 连接对象
/// </summary>
private tcpclient tc;
/// <summary>
/// 网络流
/// </summary>
private networkstream ns;
/// <summary>
/// 错误的代码字典
/// </summary>
private stringdictionary errorcodes=new stringdictionary();
/// <summary>
/// 操作执行成功后的响应代码字典
/// </summary>
private stringdictionary rightcodes=new stringdictionary();
/// <summary>
/// 执行过程中错误的消息
/// </summary>
private string errormessage="";
/// <summary>
/// 记录操作日志
/// </summary>
private string logs="";
/// <summary>
/// 主机登陆的验证方式
/// </summary>
private stringcollection validatetypes=new stringcollection();
/// <summary>
/// 换行常数
/// </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"
/// <summary>
/// 获取最后一此程序执行中的错误消息
/// </summary>
public string errormessage
{
get{return this.errormessage;}
}
/// <summary>
/// 获取或设置日志输出路径
/// </summary>
public string logpath
{
get
{
return this.logpath;
}
set{this.logpath=value;}
}
/// <summary>
/// 获取或设置登陆smtp服务器的帐号
/// </summary>
public string userid
{
get{return this.userid;}
set{this.userid=value;}
}
/// <summary>
/// 获取或设置登陆smtp服务器的密码
/// </summary>
public string password
{
get{return this.password;}
set{this.password=value;}
}
/// <summary>
/// 获取或设置要使用登陆smtp服务器的验证方式
/// </summary>
public smtpvalidatetypes smtpvalidatetype
{
get{return this.smtpvalidatetype;}
set{this.smtpvalidatetype=value;}
}
#endregion
#region "construct functions"
/// <summary>
/// 构造函数
/// </summary>
/// <param name="server">主机名</param>
/// <param name="port">端口</param>
public ksn_smtp(string server,int port)
{
tc=new tcpclient(server,port);
ns=tc.getstream();
this.servername=server;
this.initialfields();
}
/// <summary>
/// 构造函数
/// </summary>
/// <param name="ip">主机ip</param>
/// <param name="port">端口</param>
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","用户非本地,请尝试<forward-path>");
errorcodes.add("552","过量的存储分配,制定的操作未完成");
errorcodes.add("553","邮箱名不可用,如:邮箱地址的格式错误");
errorcodes.add("554","传送失败");
errorcodes.add("535","用户身份验证失败");
//****************************************************************
//操作执行成功后的状态码
//****************************************************************
rightcodes.add("220","服务就绪");
rightcodes.add("221","服务关闭传输通道");
rightcodes.add("235","验证成功");
rightcodes.add("250","要求的邮件操作完成");
rightcodes.add("251","非本地用户,将转发向<forward-path>");
rightcodes.add("334","服务器响应验证base64字符串");
rightcodes.add("354","开始邮件输入,以<crlf>.<crlf>结束");
//读取系统回应
streamreader reader=new streamreader(ns);
logs+=reader.readline()+crlf;
}
/// <summary>
/// 向smtp发送命令
/// </summary>
/// <param name="cmd"></param>
private string sendcommand(string cmd,bool ismaildata)
{
if(cmd!=null && cmd.trim()!=string.empty)
{
byte[] cmd_b=null;
if(!ismaildata)//不是邮件数据
cmd+=crlf;

logs+=cmd;
//开始写入邮件数据
if(!ismaildata)
{
cmd_b=encoding.ascii.getbytes(cmd);
ns.write(cmd_b,0,cmd_b.length);
}
else
{
cmd_b=encoding.getencoding(this.mailencodingname).getbytes(cmd);
ns.beginwrite(cmd_b,0,cmd_b.length,new asynccallback(this.asynccallback),null);
}
//读取服务器响应
streamreader reader=new streamreader(ns);
string response=reader.readline();
logs+=response+crlf;
//检查状态码
string statuscode=response.substring(0,3);
bool isexist=false;
bool isrightcode=true;
foreach(string err in this.errorcodes.keys)
{
if(statuscode==err)
{
isexist=true;
isrightcode=false;
break;
}
}
foreach(string right in this.rightcodes.keys)
{
if(statuscode==right)
{
isexist=true;
break;
}
}
//根据状态码来处理下一步的动作
if(!isexist) //不是合法的smtp主机
{
this.seterror("不是合法的smtp主机,或服务器拒绝服务");
}
else if(!isrightcode)//命令没能成功执行
{
this.seterror(statuscode+":"+this.errorcodes[statuscode]);
}
else //命令成功执行
{
this.errormessage="";
}
return response;
}
else
{
return null;
}
}
/// <summary>
/// 通过auth login方式登陆smtp服务器
/// </summary>
private void landingbylogin()
{
string base64userid=this.convertbase64string(this.userid,"ascii");
string base64pass=this.convertbase64string(this.password,"ascii");
//握手
this.sendcommand("helo "+this.servername,false);
//开始登陆
this.sendcommand("auth login",false);
this.sendcommand(base64userid,false);
this.sendcommand(base64pass,false);
}
/// <summary>
/// 通过auth plain方式登陆服务器
/// </summary>
private void landingbyplain()
{
string null=((char)0).tostring();
string loginstr=null+this.userid+null+this.password;
string base64loginstr=this.convertbase64string(loginstr,"ascii");
//握手
this.sendcommand("helo "+this.servername,false);
//登陆
this.sendcommand(base64loginstr,false);
}
/// <summary>
/// 通过auth cram-md5方式登陆
/// </summary>
private void landingbycrammd5()
{
//握手
this.sendcommand("helo "+this.servername,false);
//登陆
string response=this.sendcommand("auth cram-md5",false);
//得到服务器返回的标识
string identifier=response.remove(0,4);
//用md5加密标识
ksn_mactripledes mac=new ksn_mactripledes();
mac.key=this.password;
mac.data=encoding.ascii.getbytes(identifier);
string hash=mac.gethashvalue();
//发送用户帐号信息
this.sendcommand(this.userid+" "+hash,false);
}
/// <summary>
/// 发送邮件
/// </summary>
/// <returns></returns>
public bool sendmail(mailmessage mail)
{
bool issended=true;
try
{
//检测发送邮件的必要条件
if(mail.sender==null)
{
this.seterror("没有设置发信人");
}
if(mail.receivers.count==0)
{
this.seterror("至少要有一个收件人");
}
if(this.smtpvalidatetype!=smtpvalidatetypes.none)
{
if(this.userid==null || this.password==null)
{
this.seterror("当前设置需要smtp验证,但是没有给出登陆帐号");
}
}
//开始登陆
switch(this.smtpvalidatetype)
{
case smtpvalidatetypes.none:
this.sendcommand("helo "+this.servername,false);
break;
case smtpvalidatetypes.login:
this.landingbylogin();
break;
case smtpvalidatetypes.plain:
this.landingbyplain();
break;
case smtpvalidatetypes.crammd5:
this.landingbycrammd5();
break;
default:
break;
}
//初始化邮件会话(对应smtp命令mail)
this.sendcommand("mail from:<"+mail.sender+">",false);
//标识收件人(对应smtp命令rcpt)
foreach(string receive in mail.receivers)
{
this.sendcommand("rcpt to:<"+receive+">",false);
}
//标识开始输入邮件内容(data)
this.sendcommand("data",false);
//开始编写邮件内容
string message="subject:"+mail.subject+crlf;
message+="x-mailer:"+mail.xmailer+crlf;
message+="mime-version:1.0"+crlf;
if(mail.attachments.count==0)//没有附件
{
if(mail.mailtype==mailtypes.text) //文本格式
{
message+="content-type:text/plain;"+crlf+" ".padright(8,' ')+"charset=/""+
mail.mailencoding.tostring()+"/""+crlf;
message+="content-transfer-encoding:base64"+crlf+crlf;
if(mail.mailbody!=null)
message+=convert.tobase64string(mail.mailbody,0,mail.mailbody.length)+crlf+crlf+crlf+"."+crlf;
}
else//html格式
{
message+="content-type:multipart/alertnative;"+crlf+" ".padright(8,' ')+"boundary"
+"=/"=====003_dragon310083331177_=====/""+crlf+crlf+crlf;
message+="this is a multi-part message in mime format"+crlf+crlf;
message+="--=====003_dragon310083331177_====="+crlf;
message+="content-type:text/html;"+crlf+" ".padright(8,' ')+"charset=/""+
mail.mailencoding.tostring().tolower()+"/""+crlf;
message+="content-transfer-encoding:base64"+crlf+crlf;
if(mail.mailbody!=null)
message+=convert.tobase64string(mail.mailbody,0,mail.mailbody.length)+crlf+crlf;
message+="--=====003_dragon310083331177_=====--"+crlf+crlf+crlf+"."+crlf;
}
}
else//有附件
{
//处理要在邮件中显示的每个附件的数据
stringcollection attatchmentdatas=new stringcollection();
foreach(string path in mail.attachments)
{
if(!file.exists(path))
{
this.seterror("指定的附件没有找到"+path);
}
else
{
//得到附件的字节流
fileinfo file=new fileinfo(path);
filestream fs=new filestream(path,filemode.open,fileaccess.read);
if(fs.length>(long)int.maxvalue)
{
this.seterror("附件的大小超出了最大限制");
}
byte[] file_b=new byte[(int)fs.length];
fs.read(file_b,0,file_b.length);
fs.close();
string attatchmentmailstr="content-type:application/octet-stream;"+crlf+" ".padright(8,' ')+"name="+
"/""+file.name+"/""+crlf;
attatchmentmailstr+="content-transfer-encoding:base64"+crlf;
attatchmentmailstr+="content-disposition:attachment;"+crlf+" ".padright(8,' ')+"filename="+
"/""+file.name+"/""+crlf+crlf;
attatchmentmailstr+=convert.tobase64string(file_b,0,file_b.length)+crlf+crlf;
attatchmentdatas.add(attatchmentmailstr);
}
}
//设置邮件信息
if(mail.mailtype==mailtypes.text) //文本格式
{
message+="content-type:multipart/mixed;"+crlf+" ".padright(8,' ')+"boundary=/"=====001_dragon320037612222_=====/""
+crlf+crlf;
message+="this is a multi-part message in mime format."+crlf+crlf;
message+="--=====001_dragon320037612222_====="+crlf;
message+="content-type:text/plain;"+crlf+" ".padright(8,' ')+"charset=/""+mail.mailencoding.tostring().tolower()+"/""+crlf;
message+="content-transfer-encoding:base64"+crlf+crlf;
if(mail.mailbody!=null)
message+=convert.tobase64string(mail.mailbody,0,mail.mailbody.length)+crlf;
foreach(string s in attatchmentdatas)
{
message+="--=====001_dragon320037612222_====="+crlf+s+crlf+crlf;
}
message+="--=====001_dragon320037612222_=====--"+crlf+crlf+crlf+"."+crlf;
}
else
{
message+="content-type:multipart/mixed;"+crlf+" ".padright(8,' ')+"boundary=/"=====001_dragon255511664284_=====/""
+crlf+crlf;
message+="this is a multi-part message in mime format."+crlf+crlf;
message+="--=====001_dragon255511664284_====="+crlf;
message+="content-type:text/html;"+crlf+" ".padright(8,' ')+"charset=/""+mail.mailencoding.tostring().tolower()+"/""+crlf;
message+="content-transfer-encoding:base64"+crlf+crlf;
if(mail.mailbody!=null)
message+=convert.tobase64string(mail.mailbody,0,mail.mailbody.length)+crlf+crlf;
for(int i=0;i<attatchmentdatas.count;i++)
{
message+="--=====001_dragon255511664284_====="+crlf+attatchmentdatas[i]+crlf+crlf;
}
message+="--=====001_dragon255511664284_=====--"+crlf+crlf+crlf+"."+crlf;
}
}
//发送邮件数据
this.mailencodingname=mail.mailencoding.tostring();
this.sendcommand(message,true);
if(this.sendiscomplete)
this.sendcommand("quit",false);
}
catch
{
issended=false;
}
finally
{
this.disconnect();
//输出日志文件
if(this.logpath!=null)
{
filestream fs=null;
if(file.exists(this.logpath))
{
fs=new filestream(this.logpath,filemode.append,fileaccess.write);
this.logs=crlf+crlf+this.logs;
}
else
fs=new filestream(this.logpath,filemode.create,fileaccess.write);
byte[] logpath_b=encoding.getencoding("gb2312").getbytes(this.logs);
fs.write(logpath_b,0,logpath_b.length);
fs.close();
}
}
return issended;
}
/// <summary>
/// 异步写入数据
/// </summary>
/// <param name="result"></param>
private void asynccallback(iasyncresult result)
{
if(result.iscompleted)
this.sendiscomplete=true;
}
/// <summary>
/// 关闭连接
/// </summary>
private void disconnect()
{
try
{
ns.close();
tc.close();
}
catch
{
;
}
}
/// <summary>
/// 设置出现错误时的动作
/// </summary>
/// <param name="errorstr"></param>
private void seterror(string errorstr)
{
this.errormessage=errorstr;
logs+=this.errormessage+crlf+"【邮件处理动作中止】"+crlf;
this.disconnect();
throw new applicationexception("");
}
/// <summary>
///将字符串转换为base64
/// </summary>
/// <param name="str"></param>
/// <param name="encodingname"></param>
/// <returns></returns>
private string convertbase64string(string str,string encodingname)
{
byte[] str_b=encoding.getencoding(encodingname).getbytes(str);
return convert.tobase64string(str_b,0,str_b.length);
}
#endregion
}
}


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表