?? server.cs
字號:
using System;
using System.Text;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Collections;
namespace HBJ.MailSender.SMTP {
public enum MailFormat{Text,HTML};
public enum MailPriority{Low=1,Normal=3,High=5};
#region Class mailAttachments
public class MailAttachments {
private const int MaxAttachmentNum=10;
private IList _Attachments;
public MailAttachments() {
_Attachments=new ArrayList();
}
public string this[int index] {
get { return (string)_Attachments[index];}
}
/// <summary>
/// 添加郵件附件
/// </summary>
/// <param name="FilePath">附件的絕對路徑</param>
public void Add(params string[] filePath) {
if(filePath==null) {
throw(new ArgumentNullException("非法的附件"));
}
else {
for(int i=0;i<filePath.Length;i++) {
Add(filePath[i]);
}
}
}
/// <summary>
/// 添加一個附件,當指定的附件不存在時,忽略該附件,不產生異常。
/// </summary>
/// <param name="filePath">附件的絕對路徑</param>
public void Add(string filePath) {
//當附件存在時才加入,否則忽略
if (System.IO.File.Exists(filePath)) {
if (_Attachments.Count<MaxAttachmentNum) {
_Attachments.Add(filePath);
}
}
}
public void Clear() {//清除所有附件
_Attachments.Clear();
}
public int Count {//獲取附件個數
get { return _Attachments.Count;}
}
}
#endregion//end Class mailAttachments
#region Class MailMessage
/// <summary>
/// MailMessage 表示SMTP要發送的一封郵件的消息。
/// </summary>
public class MailMessage {
private const int MaxRecipientNum=10;
public MailMessage() {
_Recipients=new ArrayList();//收件人列表
_Attachments=new MailAttachments();//附件
_BodyFormat=MailFormat.Text;//缺省的郵件格式為Text
_Priority=MailPriority.Normal;
_Charset="GB2312";
}
/// <summary>
/// 設定語言代碼,默認設定為GB2312,如不需要可設置為""
/// </summary>
public string Charset {
get { return _Charset;}
set { _Charset=value;}
}
public string From {
get{ return _From;}
set { _From=value;}
}
public string FromName {
get { return _FromName;}
set { _FromName=value;}
}
public string Body {
get { return _Body;}
set { _Body=value;}
}
public string Subject {
get { return _Subject;}
set { _Subject=value;}
}
public MailAttachments Attachments {
get {return _Attachments;}
set { _Attachments=value;}
}
public MailPriority Priority {
get { return _Priority;}
set { _Priority=value;}
}
public IList Recipients {
get { return _Recipients;}
}
/// <summary>
/// 增加一個收件人地址
/// </summary>
/// <param name="recipient">收件人的Email地址</param>
public void AddRecipients(string recipient) {
//先檢查郵件地址是否符合規范
if (_Recipients.Count<MaxRecipientNum) {
_Recipients.Add(recipient);//增加到收件人列表
}
}
public void AddRecipients(params string[] recipient) {
if (recipient==null) {
throw (new ArgumentException("收件人不能為空."));
}
else {
for (int i=0;i<recipient.Length;i++) {
AddRecipients(recipient[i]);
}
}
}
public MailFormat BodyFormat {
set { _BodyFormat=value;}
get { return _BodyFormat;}
}
private string _From;//發件人地址
private string _FromName;//發件人姓名
private IList _Recipients;//收件人
private MailAttachments _Attachments;//附件
private string _Body;//內容
private string _Subject;//主題
private MailFormat _BodyFormat;//郵件格式
private string _Charset="GB2312";//字符編碼格式
private MailPriority _Priority;//郵件優先級
}
#endregion
#region Class SmtpMail
public class SmtpServerHelper {
private string CRLF="\r\n";//回車換行
/// <summary>
/// 錯誤消息反饋
/// </summary>
private string errmsg;
/// <summary>
/// TcpClient對象,用于連接服務器
/// </summary>
private TcpClient tcpClient;
/// <summary>
/// NetworkStream對象
/// </summary>
private NetworkStream networkStream;
/// <summary>
/// 服務器交互記錄
/// </summary>
private string logs="";
/// <summary>
/// SMTP錯誤代碼哈希表
/// </summary>
private Hashtable ErrCodeHT = new Hashtable();
/// <summary>
/// SMTP正確代碼哈希表
/// </summary>
private Hashtable RightCodeHT = new Hashtable();
public SmtpServerHelper() {
SMTPCodeAdd();//初始化SMTPCode
}
~SmtpServerHelper() {
networkStream.Close();
tcpClient.Close();
}
/// <summary>
/// 將字符串編碼為Base64字符串
/// </summary>
/// <param name="str">要編碼的字符串</param>
private string Base64Encode(string str) {
byte[] barray;
barray=Encoding.Default.GetBytes(str);
return Convert.ToBase64String(barray);
}
/// <summary>
/// 將Base64字符串解碼為普通字符串
/// </summary>
/// <param name="str">要解碼的字符串</param>
private string Base64Decode(string str) {
byte[] barray;
barray=Convert.FromBase64String(str);
return Encoding.Default.GetString(barray);
}
/// <summary>
/// 得到上傳附件的文件流
/// </summary>
/// <param name="FilePath">附件的絕對路徑</param>
private string GetStream(string FilePath) {
//建立文件流對象
System.IO.FileStream FileStr=new System.IO.FileStream(FilePath,System.IO.FileMode.Open);
byte[] by=new byte[System.Convert.ToInt32(FileStr.Length)];
FileStr.Read(by,0,by.Length);
FileStr.Close();
return(System.Convert.ToBase64String(by));
}
/// <summary>
/// SMTP回應代碼哈希表
/// </summary>
private void SMTPCodeAdd() {
//[RFC 821 4.2.1.]
/*
4.2.2. NUMERIC ORDER LIST OF REPLY CODES
211 System status, or system help reply
214 Help message
[Information on how to use the receiver or the meaning of a
particular non-standard command; this reply is useful only
to the human user]
220 <domain> Service ready
221 <domain> Service closing transmission channel
250 Requested mail action okay, completed
251 User not local; will forward to <forward-path>
354 Start mail input; end with <CRLF>.<CRLF>
421 <domain> Service not available,
closing transmission channel
[This may be a reply to any command if the service knows it
must shut down]
450 Requested mail action not taken: mailbox unavailable
[E.g., mailbox busy]
451 Requested action aborted: local error in processing
452 Requested action not taken: insufficient system storage
500 Syntax error, command unrecognized
[This may include errors such as command line too long]
501 Syntax error in parameters or arguments
502 Command not implemented
503 Bad sequence of commands
504 Command parameter not implemented
550 Requested action not taken: mailbox unavailable
[E.g., mailbox not found, no access]
551 User not local; please try <forward-path>
552 Requested mail action aborted: exceeded storage allocation
553 Requested action not taken: mailbox name not allowed
[E.g., mailbox syntax incorrect]
554 Transaction failed
*/
ErrCodeHT.Add("421","服務未就緒,關閉傳輸信道");
ErrCodeHT.Add("432","需要一個密碼轉換");
ErrCodeHT.Add("450","要求的郵件操作未完成,郵箱不可用(例如,郵箱忙)");
ErrCodeHT.Add("451","放棄要求的操作;處理過程中出錯");
ErrCodeHT.Add("452","系統存儲不足,要求的操作未執行");
ErrCodeHT.Add("454","臨時認證失敗");
ErrCodeHT.Add("500","郵箱地址錯誤");
ErrCodeHT.Add("501","參數格式錯誤");
ErrCodeHT.Add("502","命令不可實現");
ErrCodeHT.Add("503","服務器需要SMTP驗證");
ErrCodeHT.Add("504","命令參數不可實現");
ErrCodeHT.Add("530","需要認證");
ErrCodeHT.Add("534","認證機制過于簡單");
ErrCodeHT.Add("538","當前請求的認證機制需要加密");
ErrCodeHT.Add("550","要求的郵件操作未完成,郵箱不可用(例如,郵箱未找到,或不可訪問)");
ErrCodeHT.Add("551","用戶非本地,請嘗試<forward-path>");
ErrCodeHT.Add("552","過量的存儲分配,要求的操作未執行");
ErrCodeHT.Add("553","郵箱名不可用,要求的操作未執行(例如郵箱格式錯誤)");
ErrCodeHT.Add("554","傳輸失敗");
/*
211 System status, or system help reply
214 Help message
[Information on how to use the receiver or the meaning of a
particular non-standard command; this reply is useful only
to the human user]
220 <domain> Service ready
221 <domain> Service closing transmission channel
250 Requested mail action okay, completed
251 User not local; will forward to <forward-path>
354 Start mail input; end with <CRLF>.<CRLF>
*/
RightCodeHT.Add("220","服務就緒");
RightCodeHT.Add("221","服務關閉傳輸信道");
RightCodeHT.Add("235","驗證成功");
RightCodeHT.Add("250","要求的郵件操作完成");
RightCodeHT.Add("251","非本地用戶,將轉發向<forward-path>");
RightCodeHT.Add("334","服務器響應驗證Base64字符串");
RightCodeHT.Add("354","開始郵件輸入,以<CRLF>.<CRLF>結束");
}
/// <summary>
/// 發送SMTP命令
/// </summary>
private bool SendCommand(string str) {
byte[]WriteBuffer;
if(str==null||str.Trim()==String.Empty) {
return true;
}
logs+=str;
WriteBuffer = Encoding.Default.GetBytes(str);
try {
networkStream.Write(WriteBuffer,0,WriteBuffer.Length);
}
catch {
errmsg="網絡連接錯誤";
return false;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -