SMTP是Simple Mail Transfer Protocol的簡寫。
郵件是日常工作、生活中不能缺少的一個工具,下面是郵件收發的流程。
郵件的發送,主要是通過SMTP協議來實現的。SMTP協議最早在RFC 821(1982年)中定義,最后更新是在RFC 5321(2008年)中,更新中包含了擴展SMTP(ESMTP)。
在平時的程序開發中經常會有發送郵件的這種需求,所以免不了要對郵件發送服務器的可用性進行測試。下面是整理的命令,用來在命令行下測試SMTP服務器,進行郵件發送等操作,相信對於加深SMTP的理解可以起到促進作用。
SMTP默認使用25端口,我們可以使用telnet工具進行測試。
1、連接SMTP服務器,查看是否存活。
>telnet smtp.sina.com 25
>Connected to mail.sina.com.
>Escape character is '^]'.
>220 smtp ready
在telnet下轉義符是Ctrol+],如果想退出,按轉義符后輸入quit,就可以退回到命令行了。
2、用戶登陸
連接到服務器后,使用AUTH LOGIN命令進行用戶登陸(SMTP命令不區分大小寫)
>auth login
>334 VXNlcm5hbWU6
>c2VydmljZUBoZWVwLmNx
>334 UGFzc3dvcmQ6
>xxxxxxxx
>235 go ahead
235返回碼表明登陸驗證成功,用戶可以進行后續的操作了。用戶郵箱和密碼是經過Base64編碼的,這個與服務器的安全特性相關,屬於可配置項。
3、發送郵件。
用戶身份驗證通過后,執行下面的命令進行郵件的發送。
>235 #2.0.0 OK Authenticated
>MAIL FROM: yunpan001@sina.com
>250 sender <yunpan001@sina.com> ok
>RCPT to: cocowool@gmail.com
>250 recipient <cocowool@gmail.com> ok
>DATA
>354 go ahead
>Subject: Hi smtp mail
>hello mail
>.
>250 ok: Message 1763097690 accepted
SMTP命令列表
HELO
客戶端為標識自己的身份而發送的命令(通常帶域名)
EHLO
使服務器可以表明自己支持擴展簡單郵件傳輸協議 (ESMTP) 命令。
MAIL FROM
標識郵件的發件人;以 MAIL FROM: 的形式使用。
RCPT TO
標識郵件的收件人;以 RCPT TO: 的形式使用。
TURN
允許客戶端和服務器交換角色,並在相反的方向發送郵件,而不必建立新的連接。
ATRN
ATRN (Authenticated TURN) 命令可以選擇將一個或多個域作為參數。如果該會話已通過身份驗證,則ATRN 命令一定會被拒絕。
SIZE
提供一種使 SMTP 服務器可以指出所支持的最大郵件大小的機制。兼容的服務器必須提供大小范圍,以指出可以接受的最大郵件大小。客戶端發送的郵件不應大於服務器所指出的這一大小。
ETRN
SMTP 的擴展。SMTP 服務器可以發送 ETRN 以請求另一台服務器發送它所擁有的任何電子郵件。
PIPELINING
提供發送命令流(而無需在每個命令之后都等待響應)的能力。
CHUNKING
替換 DATA 命令的 ESMTP 命令。該命令使 SMTP 主機不必持續地掃描數據的末尾,它發送帶參數的 BDAT 命令,該參數包含郵件的總字節數。接收方服務器計算郵件的字節數,如果郵件大小等於 BDAT 命令發送的值時,則該服務器假定它收到了全部的郵件數據。
DATA
客戶端發送的、用於啟動郵件內容傳輸的命令。
DSN
啟用傳遞狀態通知的 ESMTP 命令。
RSET
使整個郵件的處理無效,並重置緩沖區。
VRFY
確認在郵件傳遞過程中可以使用郵箱;例如,vrfy ted 確認在本地服務器上駐留 Ted 的郵箱。該命令在 Exchange 實現中默認關閉。
HELP
返回 SMTP 服務所支持的命令列表。
QUIT
終止會話。
SMTP命令響應碼
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> (See Section 3.4)
252 Cannot VRFY user, but will accept message and attempt delivery (See Section 3.5.3)
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 or temporarily blocked for policy reasons)
451 Requested action aborted: local error in processing
452 Requested action not taken: insufficient system storage
455 Server unable to accommodate parameters
500 Syntax error, command unrecognized (This may include errors such as command line too long)
501 Syntax error in parameters or arguments502 Command not implemented (see Section 4.2.4)
503 Bad sequence of commands
504 Command parameter not implemented
550 Requested action not taken: mailbox unavailable (e.g., mailbox not found, no access, or command rejected for policy reasons)
551 User not local; please try <forward-path> (See Section 3.4)
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 (Or, in the case of a connection-opening response, "No SMTP service here")
555 MAIL FROM/RCPT TO parameters not recognized or not implemente
參考資料:
1、SMTP指令說明
2、SMTP百度百科
4、RFC 5321

