?? smtpclass.cs
字號:
using System;
using System.IO;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Collections;
using System.Collections.Specialized ;
namespace devApp.Mail
{
class smtpClass
{
/// <summary>
/// 郵件內容
/// </summary>
public class MailMessage
{
private string sender=null;
private System.Collections.Specialized.StringDictionary receivers = new System.Collections.Specialized.StringDictionary();
private string subject="";
private string xMailer="";
private System.Collections.Specialized.StringDictionary attachments = new System.Collections.Specialized.StringDictionary();
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 System.Collections.Specialized.StringDictionary 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 System.Collections.Specialized.StringDictionary 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 System.Collections.Specialized.StringDictionary validateTypes = new System.Collections.Specialized.StringDictionary();
/// <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;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -