1.實例:從qq郵箱 發送到其餘地址的郵箱html
>telnet smtp.qq.com 25安全
>helo qq.com服務器
>auth loginapp
>NzI3MTU0MTg3QHFxLmNvbQ== (郵箱名的base64編碼 http://tool.chinaz.com/Tools/base64.aspx)ide
>。。。。。。。。。。。。。。。。。(郵箱密碼的base64編碼)post
>mail from:727154187@qq.comthis
>rcpt to:jimei@outlook.com編碼
>Dataurl
From:727154187@qq.comspa
TO:jimei@outlook.com
Subject:test mail
. (注意最後以'.'結束)
[注]查找smtp 地址的命令: nslookup -type=mx [域名]
2.經常使用命令:
參數
|
做用
|
HELO
|
使用標準的SMTP,向服務器標識用戶身份。發送者能進行欺騙,但通常狀況下服務器都能檢測到
|
EHLO
|
使用ESMTP,向服務器標識用戶身份。發送者能進行欺騙,但通常狀況下服務器都能檢測到。
|
STARTTLS
|
啓用TLS
|
MAIL FROM
|
命令中指定的地址是發件人地址
|
RCPT TO
|
標識單個的郵件接收人;可有多個 RCPT TO;常在 MAIL 命令後面
|
DATA
|
在單個或多個 RCPT 命令後,表示全部的郵件接收人已標識,並初始化數據傳輸,以CRLF.CRLF 結束
|
VRFY
|
用於驗證指定的用戶/郵箱是否存在;因爲安全方面的緣由,服務器常禁止此命令
|
EXPN
|
驗證給定的郵箱列表是否存在,擴充郵箱列表,也常被禁用
|
HELP
|
查詢服務器支持什麼命令
|
NOOP
|
無操做,服務器響應 250 OK
|
RSET
|
重置會話,當前傳輸被取消,服務器響應 250 OK
|
QUIT
|
結束會話
|
3.常見錯誤碼:
421 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
552 Requested mail action aborted: exceeded storage allocation
553 Requested action not taken: mailbox name not allowed (E.g., mailbox syntax incorrect)
554 Transaction failedThe other codes that provide you with helpful information about what’s happening with your messages are:
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 Service ready
221 Service closing transmission channel
250 Requested mail action okay, completed
251 User not local; will forward to
354 Start mail input; end with . (a dot)
‘*************************
‘* 郵件服務返回代碼含義
‘* 500 格式錯誤,命令不可識別(此錯誤也包括命令行過長)
‘* 501 參數格式錯誤
‘* 502 命令不可實現
‘* 503 錯誤的命令序列
‘* 504 命令參數不可實現
‘* 211 系統狀態或系統幫助響應
‘* 214 幫助信息
‘* 220 服務就緒
‘* 221 服務關閉傳輸信道
‘* 421 服務未就緒,關閉傳輸信道(當必須關閉時,此應答能夠做爲對任何命令的響應)
‘* 250 要求的郵件操做完成
‘* 251 用戶非本地,將轉發向
‘* 450 要求的郵件操做未完成,郵箱不可用(例如,郵箱忙)
‘* 550 要求的郵件操做未完成,郵箱不可用(例如,郵箱未找到,或不可訪問)
‘* 451 放棄要求的操做;處理過程當中出錯
‘* 551 用戶非本地,請嘗試
‘* 452 系統存儲不足,要求的操做未執行
‘* 552 過量的存儲分配,要求的操做未執行
‘* 553 郵箱名不可用,要求的操做未執行(例如郵箱格式錯誤)
‘* 354 開始郵件輸入,以.結束
‘* 554 操做失敗
‘* 535 用戶驗證失敗
‘* 235 用戶驗證成功
‘* 334 等待用戶輸入驗證信息
1 <%@ WebHandler Language="C#" Class="SendMailHandler" %> 2 3 using System; 4 using System.Linq; 5 using System.Web; 6 using System.Web.SessionState; 7 8 using System.Collections.Generic; 9 using System.Web.Script.Serialization; 10 11 12 using System.Text.RegularExpressions; 13 using System.Net; 14 using System.Net.Mail; 15 16 public class SendMailHandler : IHttpHandler 17 { 18 public void ProcessRequest(HttpContext context) 19 { 20 context.Response.ContentType = "text/plain"; 21 JavaScriptSerializer ser = new JavaScriptSerializer(); 22 var form=context.Request.Form; 23 string email =form["email"]??""; 24 25 bool status=false; 26 string msg=""; 27 string log=""; 28 try 29 { 30 31 32 var smtpSetting=new Smtp 33 { 34 Port=25 , 35 EnableSsl=false , 36 Host="smtp.qq.com" , 37 Password="******" , 38 UserName="727154187@qq.com" , 39 From="727154187@qq.com" , 40 To=new string[] { "jimei@outlook.com" } 41 }; 42 43 Regex reg = new Regex(@"^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$"); 44 45 if(smtpSetting!=null && !string.IsNullOrEmpty(email) && reg.IsMatch(email)) 46 { 47 48 var template=@"<div> 49 <p><b>Gender:</b> <span>$$gender$$</span></p> 50 <p><b>Name:</b> <span>$$name$$</span></p> 51 <p><b>Email:</b> <span>$$email$$</span></p> 52 <p><b>Topic:</b> <span>$$topic$$</span></p> 53 <p><b>Content:</b> <span>$$content$$</span></p></div>"; 54 var g=form["gender"]??"1"; 55 56 template=template.Replace("$$gender$$" ,g=="1"?"male":"female"); 57 template=template.Replace("$$name$$" , form["name"]??""); 58 template=template.Replace("$$email$$" , form["email"]??""); 59 template=template.Replace("$$topic$$" , form["topic"]??""); 60 template=template.Replace("$$content$$" ,form["content"]??""); 61 62 status=SendEmail(smtpSetting , "ContactUs" , template,out log); 63 64 } 65 } 66 catch(Exception e) 67 { 68 msg=e.Message; 69 status=false; 70 } 71 var objRtn = new { result=status.ToString(),Msg=msg,Log=log}; 72 context.Response.Write(ser.Serialize(objRtn)); 73 context.Response.End(); 74 } 75 76 private bool SendEmail(Smtp smtpSetting,string subject , string body,out string result) 77 { 78 SmtpClient smtpClient = new SmtpClient(); 79 result=""; 80 if(smtpSetting != null) 81 { 82 smtpClient.Host = smtpSetting.Host; 83 smtpClient.Port = smtpSetting.Port; 84 smtpClient.EnableSsl = smtpSetting.EnableSsl; 85 86 87 if(!String.IsNullOrEmpty(smtpSetting.UserName) && !String.IsNullOrEmpty(smtpSetting.Password)) 88 { 89 try 90 { 91 smtpClient.UseDefaultCredentials = false; 92 smtpClient.Credentials = new NetworkCredential(smtpSetting.UserName , smtpSetting.Password); 93 94 if(!string.IsNullOrEmpty(smtpSetting.From) && smtpSetting.To!=null && smtpSetting.To.Length>0) 95 { 96 MailMessage message = new MailMessage(); 97 message.From = new MailAddress(smtpSetting.From , smtpSetting.From); 98 foreach(string item in smtpSetting.To) 99 { 100 message.To.Add(new MailAddress(item)); 101 } 102 message.Subject = subject; 103 message.Body = body; 104 message.IsBodyHtml = true; 105 106 smtpClient.Send(message); 107 return true; 108 } 109 } 110 catch(Exception e) 111 { 112 result=e.Message; 113 return false; 114 } 115 116 } 117 118 } 119 return false; 120 } 121 122 public bool IsReusable 123 { 124 get 125 { 126 return false; 127 } 128 } 129 }
1 public class Smtp 2 { 3 public Smtp() { } 4 5 public bool EnableSsl { get; set; } 6 public string From { get; set; } 7 public string Host { get; set; } 8 public string Password { get; set; } 9 public int Port { get; set; } 10 public string[] To { get; set; } 11 public string UserName { get; set; } 12 }