?? sendmail.cs
字號:
}
return SendEmail();
}
/// <summary>
/// 發送郵件方法
/// </summary>
/// <param name="smtpserver">smtp服務器信息,如"username:password@www.smtpserver.com:25",也可去掉部分次要信
public bool Send(string smtpserver)
{
MailDomain=smtpserver;
return Send();
}
/// <summary>
/// 發送郵件方法
/// </summary>
/// <param name="smtpserver">smtp服務器信息,如"username:password@www.smtpserver.com:25",也可去掉部分次要信
/// <param name="from">發件人mail地址</param>
/// <param name="fromname">發件人姓名</param>
/// <param name="to">收件人地址</param>
/// <param name="toname">收件人姓名</param>
/// <param name="html">是否HTML郵件</param>
/// <param name="subject">郵件主題</param>
/// <param name="body">郵件正文</param>
public bool Send(string smtpserver,string from,string fromname,string to,bool html,string
subject,string body)
{
MailDomain=smtpserver;
From=from;
FromName=fromname;
Recipient=to;
Html=html;
Subject=subject;
Body=body;
return Send();
}
void Dispose()
{
if(ns!=null)ns.Close();
if(tc!=null)tc.Close();
}
/// <summary>
/// SMTP回應代碼哈希表
/// </summary>
private void SMTPCodeAdd()
{
ErrCodeHT.Add("500","郵箱地址錯誤");
ErrCodeHT.Add("501","參數格式錯誤");
ErrCodeHT.Add("502","命令不可實現");
ErrCodeHT.Add("503","服務器需要SMTP驗證");
ErrCodeHT.Add("504","命令參數不可實現");
ErrCodeHT.Add("421","服務未就緒,關閉傳輸信道");
ErrCodeHT.Add("450","要求的郵件操作未完成,郵箱不可用(例如,郵箱忙)");
ErrCodeHT.Add("550","要求的郵件操作未完成,郵箱不可用(例如,郵箱未找到,或不可訪問)");
ErrCodeHT.Add("451","放棄要求的操作;處理過程中出錯");
ErrCodeHT.Add("551","用戶非本地,請嘗試<forward-path>");
ErrCodeHT.Add("452","系統存儲不足,要求的操作未執行");
ErrCodeHT.Add("552","過量的存儲分配,要求的操作未執行");
ErrCodeHT.Add("553","郵箱名不可用,要求的操作未執行(例如郵箱格式錯誤)");
ErrCodeHT.Add("432","需要一個密碼轉換");
ErrCodeHT.Add("534","認證機制過于簡單");
ErrCodeHT.Add("538","當前請求的認證機制需要加密");
ErrCodeHT.Add("454","臨時認證失敗");
ErrCodeHT.Add("530","需要認證");
RightCodeHT.Add("220","服務就緒");
RightCodeHT.Add("250","要求的郵件操作完成");
RightCodeHT.Add("251","用戶非本地,將轉發向<forward-path>");
RightCodeHT.Add("354","開始郵件輸入,以<enter>.<enter>結束");
RightCodeHT.Add("221","服務關閉傳輸信道");
RightCodeHT.Add("334","服務器響應驗證Base64字符串");
RightCodeHT.Add("235","驗證成功");
}
/// <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>
/// 發送SMTP命令
/// </summary>
private bool SendCommand(string str)
{
byte[] WriteBuffer;
if(str==null||str.Trim()==String.Empty)
{
return true;
}
WriteBuffer = Encoding.Default.GetBytes(str);
try
{
ns.Write(WriteBuffer,0,WriteBuffer.Length);
}
catch
{
errmsg="網絡連接錯誤";
return false;
}
return true;
}
/// <summary>
/// 接收SMTP服務器回應
/// </summary>
private string RecvResponse()
{
int StreamSize;
string ReturnValue = String.Empty;
byte[] ReadBuffer = new byte[1024] ;
try
{
StreamSize=ns.Read(ReadBuffer,0,ReadBuffer.Length);
}
catch(Exception ex)
{
MessageBox.Show (ex.ToString ());
return "false";
}
if (StreamSize==0)
{
return ReturnValue ;
}
else
{
ReturnValue = Encoding.Default.GetString(ReadBuffer).Substring(0,StreamSize);
return ReturnValue;
}
}
/// <summary>
/// 與服務器交互,發送一條命令并接收回應。
/// </summary>
/// <param name="str">一個要發送的命令</param>
/// <param name="errstr">如果錯誤,要反饋的信息</param>
private bool Dialog(string str,string errstr)
{
if(str==null||str.Trim()=="")
{
return true;
}
if(SendCommand(str))
{
string RR=RecvResponse();
if(RR=="false")
{
return false;
}
try
{
string RRCode=RR.Substring(0,3);
if(RightCodeHT[RRCode]!=null)
{
return true;
}
else
{
if(ErrCodeHT[RRCode]!=null)
{
errmsg+=(RRCode+ErrCodeHT[RRCode].ToString());
errmsg+=enter;
}
else
{
errmsg+=RR;
}
errmsg+=errstr;
return false;
}
}
catch
{
MessageBox.Show ("發送的附件超過本服務器對個人軟件的支持!","請檢查附件的大小");
return false;
}
}
else
{
return false;
}
}
/// <summary>
/// 與服務器交互,發送一組命令并接收回應。
/// </summary>
private bool Dialog(string[] str,string errstr)
{
for(int i=0;i<str.Length;i++)
{
//如果在身份驗證階段有一個不成功,就返回錯誤標志位
if(!Dialog(str[i],""))
{
errmsg+=enter;
errmsg+=errstr;
return false;
}
}
//身份驗證全部正確的話,則返回正確標志位
return true;
}
/// <summary>
/// SendEmail
/// </summary>
/// <returns></returns>
private bool SendEmail()
{
try
{
tc=new TcpClient(mailserver,mailserverport);
}
catch
{
MessageBox.Show ("連接失敗","請確認");
return false;
}
ns = tc.GetStream();
SMTPCodeAdd();
//驗證網絡連接是否正確
if(RightCodeHT[RecvResponse().Substring(0,3)]==null)
{
errmsg="網絡連接失敗";
return false;
}
string[] SendBuffer;
string SendBufferstr;
//進行SMTP驗證
if(ESmtp)
{
SendBuffer=new String[4];
SendBuffer[0]="EHLO " + mailserver + enter;
SendBuffer[1]="AUTH LOGIN" + enter;
SendBuffer[2]=Base64Encode(username) + enter;
SendBuffer[3]=Base64Encode(password) + enter;
if(!Dialog(SendBuffer,"SMTP服務器驗證失敗,請核對用戶名和密碼。"))
{
MessageBox.Show ("SMTP服務器驗證失敗,請核對用戶名和密碼。");
return false;
}
}
else
{
SendBufferstr="HELO " + mailserver + enter;
if(!Dialog(SendBufferstr,""))
return false;
}
//
SendBufferstr="MAIL FROM:<" + From + ">" + enter;
if(!Dialog(SendBufferstr,"發件人地址錯誤,或不能為空"))
{
MessageBox.Show("發件人地址錯誤,或不能為空");
return false;
}
//把傳過來的收件人的地址分割然后提交給服務器
string split=",";
string []address=Regex.Split (RecipientName,split);
SendBuffer=new string [address.Length];
for(int i=0;i<SendBuffer.Length;i++)
{
SendBuffer[i]="RCPT TO:<" +address[i]+">" + enter;
}
if(!Dialog(SendBuffer,"收件人地址有誤"))
{
MessageBox.Show("收件人地址有誤");
return false;
}
SendBufferstr="DATA" + enter;
if(!Dialog(SendBufferstr,""))
return false;
SendBufferstr="From:" + FromName + "<" + From +">" +enter;
SendBufferstr += "To:<"+RecipientName+">"+enter;
SendBufferstr+=((Subject==String.Empty || Subject==null)?"Subject:":((Charset=="")?("Subject:" +
Subject):("Subject:" + "=?" + Charset.ToUpper() + "?B?" + Base64Encode(Subject) +"?="))) + enter;
SendBufferstr+="X-Priority:" + priority + enter;
SendBufferstr+="X-Mailer: ArgentSwan Mail Sender" + enter;
SendBufferstr+="MIME-Version: 1.0" + enter;
//MIME定義了5個新的信頭字段,可以與原有信頭字段一樣,用在RF822郵件的首部中。
//1.MIME版本信頭字段 格式:MIME-Version:1.0 <CRLF>
//2.郵件唯一標識信頭字段 格式:Content-ID:唯一標識信件的字符串 <CRLF>
//3.郵件內容描述信頭字段 格式:Content-Description:描述文本 <CRLF>
//4.MIME郵件的內容類型信頭字段 格式:Content-Type:主類別標識符/子類別標識符 [;參數列表] <CRLF>
//5.內容傳送編碼方式信頭字段 格式:Content-Transfer-Encoding:編碼方式標識符 <CRLF>
if(Attachments.Count!=0)
{
//mixed 按照特定順序的幾個獨立部分
SendBufferstr+="Content-Type: multipart/mixed;" + enter;
SendBufferstr += " boundary=\"***"+
(Html?"001_yinhu19821115":"001_yinhu19831115")+"***\""+enter+enter;
}
if(Html)
{
if(Attachments.Count==0)
{
//multipart多部分 alternative 不同格式的同一郵件
SendBufferstr += "Content-Type: multipart/alternative;"+enter;//內容格式和分隔符
SendBufferstr += " boundary=\"***003_yinhu19821115***\""+enter+enter;
SendBufferstr += "This is a multi-part message in MIME format."+enter+enter;
}
else
{
SendBufferstr +="This is a multi-part message in MIME format."+enter+enter;
SendBufferstr += "--***001_yinhu19821115***"+enter;
SendBufferstr += "Content-Type: multipart/alternative;"+enter;//內容格式和分隔符
SendBufferstr += " boundary=\"***003_yinhu19821115***\""+enter+enter;
}
SendBufferstr += "--***003_yinhu19821115***"+enter;
SendBufferstr += "Content-Type: text/plain;"+ enter;
SendBufferstr += ((Charset=="")?(" charset=\"iso-8859-1\""):(" charset=\"" +
Charset.ToLower() + "\"")) + enter;
SendBufferstr+="Content-Transfer-Encoding: base64" + enter + enter;
SendBufferstr+= Base64Encode("郵件內容為HTML格式,請選擇HTML方式查看") + enter + enter;
SendBufferstr += "--***003_yinhu19821115***"+enter;
SendBufferstr+="Content-Type: text/html;" + enter;
SendBufferstr+=((Charset=="")?(" charset=\"iso-8859-1\""):(" charset=\"" +
Charset.ToLower() + "\"")) + enter;
SendBufferstr+="Content-Transfer-Encoding: base64" + enter + enter;
SendBufferstr+=Base64Encode(Body)+ enter + enter;
SendBufferstr += "--***003_yinhu19821115***--"+enter;
}
else
{
if(Attachments.Count!=0)
{
SendBufferstr += "--***001_yinhu19831115***"+enter;
}
SendBufferstr+="Content-Type: text/plain;" + enter;
SendBufferstr+=((Charset=="")?(" charset=\"iso-8859-1\""):(" charset=\"" +
Charset.ToLower() + "\"")) + enter;
SendBufferstr+="Content-Transfer-Encoding: base64" + enter + enter;
SendBufferstr+= Base64Encode(Body) + enter;
}
if(Attachments.Count!=0)
{
for(int i=0;i<Attachments.Count;i++)
{
string filepath = Attachments[i].ToString();
SendBufferstr += "--***"+
(Html?"001_yinhu19821115":"001_yinhu19831115") +"***"+enter;
//用于在電子郵件中傳輸一個含有任意數據的實體。內容是一個未解釋的字節序列。
//當內容類型未知或者對數據沒有具體定義媒體類別時,通常使用它描述
SendBufferstr += "Content-Type: application/octet-stream"+enter;
SendBufferstr += " name=\"=?"+Charset.ToUpper()+"?B?"+Base64Encode
(filepath.Substring(filepath.LastIndexOf("\\")+1))+"?=\""+enter;
SendBufferstr += "Content-Transfer-Encoding: base64"+enter;
SendBufferstr += "Content-Disposition: attachment;"+enter;
SendBufferstr += " filename=\"=?"+Charset.ToUpper()+"?B?"+Base64Encode
(filepath.Substring(filepath.LastIndexOf("\\")+1))+"?=\""+enter+enter;
SendBufferstr += GetStream(filepath)+enter+enter;
}
SendBufferstr += "--***"+ (Html?"001_yinhu19821115":"001_yinhu19831115")
+"***--"+enter+enter;
}
SendBufferstr += enter + "." + enter;
if(!Dialog(SendBufferstr,"錯誤信件信息"))
return false;
SendBufferstr="QUIT" + enter;
if(!Dialog(SendBufferstr,"斷開連接時錯誤"))
return false;
ns.Close();
tc.Close();
return true;
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -