using System;
using System.Text;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Collections;
namespace Huolx.Pubclass
{
/// <summary>
/// <para>支持SMTP服務(wù)器認(rèn)證的郵件發(fā)送類</para>
/// <para>屬性和方法設(shè)置類似于Jmail。</para>
/// <para>Huolx(flycloud@btamail.net.cn)版權(quán)所有</para>
/// </summary>
public class ESmtpMail
{
/// <summary>
/// 設(shè)定語言代碼,默認(rèn)設(shè)定為GB2312,如不需要可設(shè)置為""
/// </summary>
public string Charset="GB2312";
/// <summary>
/// 發(fā)件人地址
/// </summary>
public string From="";
/// <summary>
/// 發(fā)件人姓名
/// </summary>
public string FromName="";
/// <summary>
/// 回復(fù)郵件地址
/// </summary>
public string ReplyTo="";
/// <summary>
/// 郵件服務(wù)器域名
/// </summary>
private string mailserver="";
/// <summary>
/// 郵件服務(wù)器域名和驗(yàn)證信息
/// 形如:"user:pass@www.server.com:25",也可省略次要信息。如"user:pass@www.server.com"或"www.server.com"
/// </summary>
public string MailDomain
{
set
{
string maidomain=value.Trim();
int tempint;
if(maidomain!="")
{
tempint=maidomain.IndexOf("@");
if(tempint!=-1)
{
string up=maidomain.Substring(0,tempint);
MailServerUserName=up.Substring(0,up.IndexOf(":"));
MailServerPassWord=up.Substring(up.IndexOf(":")+1,up.Length-up.IndexOf(":")-1);
string ms=maidomain.Substring(tempint+1,maidomain.Length-tempint-1);
tempint=ms.IndexOf(":");
if(tempint!=-1)
{
mailserver=ms.Substring(0,tempint);
mailserverport=System.Convert.ToInt32(ms.Substring(tempint+1,ms.Length-tempint-1));
}
}
else
{
tempint=maidomain.IndexOf(":");
if(tempint!=-1)
{
mailserver=maidomain.Substring(0,tempint);
mailserverport=System.Convert.ToInt32(maidomain.Substring(tempint+1,maidomain.Length-tempint-1));
}
else
{
mailserver=maidomain;
}
}
}
}
}
/// <summary>
/// 郵件服務(wù)器端口號(hào)
/// </summary>
private int mailserverport=25;
/// <summary>
/// 郵件服務(wù)器端口號(hào)
/// </summary>
public int MailDomainPort
{
set
{
mailserverport=value;
}
}
/// <summary>
/// 是否需要SMTP驗(yàn)證
/// </summary>
private bool ESmtp=false;
/// <summary>
/// SMTP認(rèn)證時(shí)使用的用戶名
/// </summary>
private string username="";
/// <summary>
/// SMTP認(rèn)證時(shí)使用的用戶名
/// </summary>
public string MailServerUserName
{
set
{
if(value.Trim()!="")
{
username=value.Trim();
ESmtp=true;
}
else
{
username="";
ESmtp=false;
}
}
}
/// <summary>
/// SMTP認(rèn)證時(shí)使用的密碼
/// </summary>
private string password="";
/// <summary>
/// SMTP認(rèn)證時(shí)使用的密碼
/// </summary>
public string MailServerPassWord
{
set
{
password=value;
}
}
/// <summary>
/// 郵件主題
/// </summary>
public string Subject="";
/// <summary>
/// 是否Html郵件
/// </summary>
public bool Html=false;
/// <summary>
/// 郵件正文
/// </summary>
public string Body="";
/// <summary>
/// 收件人列表
/// </summary>
private Hashtable Recipient=new Hashtable();
/// <summary>
/// 密送收件人列表
/// </summary>
private Hashtable RecipientBCC=new Hashtable();
/// <summary>
/// 郵件發(fā)送優(yōu)先級(jí),可設(shè)置為"High","Normal","Low"或"1","3","5"
/// </summary>
private string priority="Normal";
/// <summary>
/// 郵件發(fā)送優(yōu)先級(jí),可設(shè)置為"High","Normal","Low"或"1","3","5"
/// </summary>
public string Priority
{
set
{
switch(value.ToLower())
{
case "high":
priority="High";
break;
case "1":
priority="High";
break;
case "normal":
priority="Normal";
break;
case "3":
priority="Normal";
break;
case "low":
priority="Low";
break;
case "5":
priority="Low";
break;
default:
priority="Normal";
break;
}
}
}
/// <summary>
/// 錯(cuò)誤消息反饋
/// </summary>
private string errmsg;
/// <summary>
/// 錯(cuò)誤消息反饋
/// </summary>
public string ErrorMessage
{
get
{
return errmsg;
}
}
/// <summary>
/// 服務(wù)器交互記錄
/// </summary>
private string logs="";
/// <summary>
/// 服務(wù)器交互記錄,如發(fā)現(xiàn)本組件不能使用的SMTP服務(wù)器,請(qǐng)將出錯(cuò)時(shí)的Logs發(fā)給我(flycloud@btamail.net.cn),我將盡快查明原因。
/// </summary>
public string Logs
{
get
{
return logs;
}
}
private string enter="\r\n";
/// <summary>
/// TcpClient對(duì)象,用于連接服務(wù)器
/// </summary>
private TcpClient tc;
/// <summary>
/// NetworkStream對(duì)象
/// </summary>
private NetworkStream ns;
/// <summary>
/// SMTP錯(cuò)誤代碼哈希表
/// </summary>
private Hashtable ErrCodeHT = new Hashtable();
/// <summary>
/// SMTP正確代碼哈希表
/// </summary>
private Hashtable RightCodeHT = new Hashtable();
/// <summary>
/// SMTP回應(yīng)代碼哈希表
/// </summary>
private void SMTPCodeAdd()
{
ErrCodeHT.Add("500","郵箱地址錯(cuò)誤");
ErrCodeHT.Add("501","參數(shù)格式錯(cuò)誤");
ErrCodeHT.Add("502","命令不可實(shí)現(xiàn)");
ErrCodeHT.Add("503","服務(wù)器需要SMTP驗(yàn)證");
ErrCodeHT.Add("504","命令參數(shù)不可實(shí)現(xiàn)");
ErrCodeHT.Add("421","服務(wù)未就緒,關(guān)閉傳輸信道");
ErrCodeHT.Add("450","要求的郵件操作未完成,郵箱不可用(例如,郵箱忙)");
ErrCodeHT.Add("550","要求的郵件操作未完成,郵箱不可用(例如,郵箱未找到,或不可訪問)");
ErrCodeHT.Add("451","放棄要求的操作;處理過程中出錯(cuò)");
ErrCodeHT.Add("551","用戶非本地,請(qǐng)嘗試<forward-path>");
ErrCodeHT.Add("452","系統(tǒng)存儲(chǔ)不足,要求的操作未執(zhí)行");
ErrCodeHT.Add("552","過量的存儲(chǔ)分配,要求的操作未執(zhí)行");
ErrCodeHT.Add("553","郵箱名不可用,要求的操作未執(zhí)行(例如郵箱格式錯(cuò)誤)");
ErrCodeHT.Add("432","需要一個(gè)密碼轉(zhuǎn)換");
ErrCodeHT.Add("534","認(rèn)證機(jī)制過于簡單");
ErrCodeHT.Add("538","當(dāng)前請(qǐng)求的認(rèn)證機(jī)制需要加密");
ErrCodeHT.Add("454","臨時(shí)認(rèn)證失敗");
ErrCodeHT.Add("530","需要認(rèn)證");
rightcodeht.add("220","服務(wù)就緒");
RightCodeHT.Add("250","要求的郵件操作完成");
RightCodeHT.Add("251","用戶非本地,將轉(zhuǎn)發(fā)向<forward-path>");
RightCodeHT.Add("354","開始郵件輸入,以<CRLF>.<CRLF>結(jié)束");
RightCodeHT.Add("221","服務(wù)關(guān)閉傳輸信道");
RightCodeHT.Add("334","服務(wù)器響應(yīng)驗(yàn)證Base64字符串");
RightCodeHT.Add("235","驗(yàn)證成功");
}
/// <summary>
/// 將字符串編碼為Base64字符串
/// </summary>
/// <param name="estr">要編碼的字符串</param>
private string Base64Encode(string estr)
{
byte[] barray;
barray=Encoding.Default.GetBytes(estr);
return Convert.ToBase64String(barray);
}
/// <summary>
/// 將Base64字符串解碼為普通字符串
/// </summary>
/// <param name="dstr">要解碼的字符串</param>
private string Base64Decode(string dstr)
{
byte[] barray;
barray=Convert.FromBase64String(dstr);
return Encoding.Default.GetString(barray);
}
public ESmtpMail()
{
}
~esmtpmail()
{
}
/// <summary>
/// 收件人姓名
/// </summary>
public string RecipientName="";
private int RecipientNum=0;//收件人數(shù)量
private int RecipientBCCNum=0;//密件收件人數(shù)量
/// <summary>
/// 添加一個(gè)收件人
/// </summary>
/// <param name="str">收件人地址</param>
public bool AddRecipient(string str)
{
str=str.Trim();
if(str==null||str==""||str.IndexOf("@")==-1)
return true;
if(RecipientNum<10)
{
Recipient.Add(RecipientNum,str);
RecipientNum++;
return true;
}
else
{
errmsg+="收件人過多";
return false;
}
}
/// <summary>
/// 添加一組收件人(不超過10個(gè)),參數(shù)為字符串?dāng)?shù)組
/// </summary>
/// <param name="str">保存有收件人地址的字符串?dāng)?shù)組(不超過10個(gè))</param>
public bool AddRecipient(string[] str)
{
for(int i=0;i<str.Length;i++)
{
if(!AddRecipient(str[i]))
{
return false;
}
}
return true;
}
/// <summary>
/// 添加一個(gè)密件收件人
/// </summary>
/// <param name="str">收件人地址</param>
public bool AddRecipientBCC(string str)
{
if(str==null||str.Trim()=="")
return true;
if(RecipientBCCNum<10)
{
RecipientBCC.Add(RecipientBCCNum,str);
RecipientBCCNum++;
return true;
}
else
{
errmsg+="收件人過多";
return false;
}
}
/// <summary>
/// 添加一組密件收件人(不超過10個(gè)),參數(shù)為字符串?dāng)?shù)組
/// </summary>
/// <param name="str">保存有收件人地址的字符串?dāng)?shù)組(不超過10個(gè))</param>
public bool AddRecipientBCC(string[] str)
{
for(int i=0;i<str.Length;i++)
{
if(!AddRecipientBCC(str[i]))
{
return false;
}
}
return true;
}
/// <summary>
/// 發(fā)送SMTP命令
/// </summary>
private bool SendCommand(string Command)
{
byte[] WriteBuffer;
if(Command==null||Command.Trim()=="")
{
return true;
}
logs+=Command;
WriteBuffer = Encoding.Default.GetBytes(Command);
try
{
ns.Write(WriteBuffer,0,WriteBuffer.Length);
}
catch
{
errmsg="網(wǎng)絡(luò)連接錯(cuò)誤";
return false;
}
return true;
}