📄 smtp_.cs
字号:
//开始编写邮件内容
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
}
#region 邮件内容
/// <summary>
/// 邮件内容
/// </summary>
public class MailMessage {
private string sender=null;
private StringCollection receivers=new StringCollection();
private string subject="";
private string xMailer="Foxmail 6, 01, 102, 12 [cn]";
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;}
}
}
#endregion
#region 一些枚举
/// <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
}
#endregion
#region 加密认证类
public class KSN_MACTripleDES {
private MACTripleDES mact;
private string __key="HBJ.Sender";
private byte[] __data=null;
public KSN_MACTripleDES() {
mact=new MACTripleDES();
}
/// <summary>
/// 获取或设置用于数字签名的密钥
/// </summary>
public string Key {
get{return this.__key;}
set {
int keyLength=value.Length;
int[] keyAllowLengths=new int[]{8,16,24};
bool isRight=false;
foreach(int i in keyAllowLengths) {
if(keyLength==keyAllowLengths[i]) {
isRight=true;
break;
}
}
if(!isRight)
throw new CryptographicException("用于数字签名的密钥长度必须是8,16,24值之一");
else
this.__key=value;
}
}
/// <summary>
/// 获取或设置用于数字签名的用户数据
/// </summary>
public byte[] Data {
get{return this.__data;}
set{this.__data=value;}
}
/// <summary>
/// 得到签名后的hash值
/// </summary>
/// <returns></returns>
public string GetHashValue() {
if(this.Data==null)
throw new Exception("没有设置要进行数字签名的用户数据(property:Data)");
byte[] key=Encoding.ASCII.GetBytes(this.Key);
this.mact.Key=key;
byte[] hash_b=this.mact.ComputeHash(this.mact.ComputeHash(this.Data));
return Encoding.ASCII.GetString(hash_b);
}
}
#endregion
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -