1. 程式人生 > >mail smtp data命令

mail smtp data命令

一 背景

探測163的發件伺服器smtp.163.com,對於流程中的data命令總是報500 bad syntax 錯誤,後找資料發現,data有格式要求,“end with <CRLF>.<CRLF> ”, 但嘗試 “data  \r\n.\r\n” 仍然失敗。

二 telnet流程

程式碼未走通,先通過telnet嘗試,流程如下:

220 163.com Anti-spam GT for Coremail System (163com[20141201])
HELO 163.COM
250 OK
AUTH LOGIN
334 dXNlcm5hbWU6
xxxxxxxxxxxxxxxx       // 163郵箱賬號的base64編碼
334 UGFzc3dvcmQ6
xxxxxxxxxxxxxxxx     // 賬戶密碼的base64編碼
235 Authentication successful
MAIL FROM :<

[email protected]>  // 發件人郵箱賬戶
250 Mail OK
rcpt to : <[email protected]>  // 收件人郵箱賬戶
250 Mail OK
data
354 End data with <CR><LF>.<CR><LF>  
SUBJECT: hahahahahahah     // 傳送郵件主題
.   // 郵件編輯結束符
250 Mail OK queued as smtp12,EMCowACnrlbF94RblO5eDg--.15992S3 1535441150

發現data命令直接回車後能夠正確返回354.

於是將程式碼改為“data\r\n” ,通過。

三 RFC5321說明

檢視rfc5321中data命令的相關說明:

……

The mail data are terminated by a line containing only a period, thatis, the character sequence "<CRLF>.<CRLF>", where the first <CRLF> isactually the terminator of the previous line (see Section 4.5.2).This is the end of mail data indication. The first <CRLF> of this terminating sequence is also the <CRLF> that ends the final line ofthe data (message text) or, if there was no mail data, ends the DATAcommand itself (the "no mail data" case does not conform to this specification

since it would require that neither the trace header fields required by this specification nor the message header section required by RFC 5322 [4] be transmitted).

……

也就是說data命令在"no mail data"場景下,並不需要遵循<CRLF>.<CRLF>的結束格式。