查看完整版本: [轉貼] SMTP 指令說明

chun 2009-1-14 20:58

[轉貼] SMTP 指令說明

http://www.cqinc.com.tw/coopermaa/932-DC/practice/SMTPCmds.htm<br><p class="Title" align="center">SMTP 指令說明</p>
<p class="SubTitle2">Summary</p>
<p class="text11">儘管 WWW 和 IM (Instant Messaging) 相繼起而流行,E-Mail
這個最為古老、行之有年的網路服務,還是公認為目前最有效的溝通工具。為了要發出一封信件,Mail Client 會與遠端的 Mail Server
建立起通信的管道,它們彼此之間你來我往資料傳送時,共同遵守著同一套規則: SMTP 協定 (Simple Mail Transfer Protocol)。</p>
<p class="text11">底下說明幾個常用的 SMTP 指令,還有 Mail Server 回傳代碼的意義。</p>
<blockquote>
        <p class="text11"><font color="#626262">註:其實可以利用 telnet 工具,讓 telnet 程式扮演
        Mail Client 的角色來與 Mail Server 建立連線、溝通並發出信件,方便練習這邊所介紹的 SMTP 指令。可參考這份<a href="http://www.cqinc.com.tw/coopermaa/932-DC/practice/winmail.rar">影片</a>。</font></p>
</blockquote>
<p class="SubTitle2">SMTP Commands</p>
<p class="text11">下列是 Mail Client 用來與 Server 溝通的指令。這些指令沒有大小寫的區別(not case
sensitive)。</p>
<table id="table1" width="90%" border="0">
        <tbody><tr>
                <td class="etext9" width="80" nowrap="nowrap">
                <p class="etext9"><b>HELO</b></p></td>
                <td class="text11"> </td>
        </tr>
        <tr>
                <td class="etext9" width="80" nowrap="nowrap"> </td>
                <td class="text11">
                <p class="text11">啟始與 Mail Server 之間的對話。一般來說,HELO 是 Mail client
                送出的第一個指令,用途是告訴 Mail Server 來者為何,通常我們會把電腦的 domain name 告訴 Server。例如: </p>
                <blockquote>
                        <p class="etext9"><b>Helo mail.companyName.com</b></p>
                </blockquote>
                </td>
        </tr>
        <tr>
                <td class="etext9" width="80" nowrap="nowrap"><b>MAIL FROM</b></td>
                <td class="text11"> </td>
        </tr>
        <tr>
                <td class="etext9" width="80" nowrap="nowrap"> </td>
                <td class="text11">告訴 Server 發信人是誰,例如:<blockquote>
                        <p class="etext9"><b>MAIL FROM: google@yahoo.com</b></p>
                </blockquote>
                <p>
                注意,這邊的發信人郵件地址並不會顯示在信件的寄件者欄中,當信件無法順利送到目的地時,退回的信件就會寄到這個地方。(寄件者欄、收件者欄、標題欄、副本、密件副本欄,這些是在
                Message 的 Header 中指定的。有關 Mail Message 的格式,請參考
                <a href="http://www.cqinc.com.tw/coopermaa/932-DC/practice/0427-EMailService.ppt">Email Service</a> 投影片。)</p></td>
        </tr>
        <tr>
                <td class="etext9" width="80" nowrap="nowrap"><b>RCPT TO</b></td>
                <td class="text11"> </td>
        </tr>
        <tr>
                <td class="etext9" width="80" nowrap="nowrap"> </td>
                <td class="text11">告訴 Server 收件人是誰,例如:<blockquote>
                        <p class="etext9"><b>RCPT TO: yahoo@google.com</b></p>
                </blockquote>
                <p>如果需要同時寄一封信給很多人,多下幾次 RCPT TO 指令即可。如果給的不是完整的 e-mail address,代表要寄信到該台
                Mail Server 的指定信箱,例如:</p>
                <blockquote>
                        <p class="etext9"><b>RCPT TO: kimo</b></p>
                </blockquote>
                <p>意思就是要寄信到連線中這台 Mail Server 的 kimo 這個信箱。有一點要注意的是,為了避免成為發垃圾信的工具,大部份 Mail
                Server 是不允許你透過它寄信到其它地方的,除非先經過<a href="http://www.cqinc.com.tw/coopermaa/932-DC/practice/SMTPCmds.htm#SMTP_Authentication">身份的確認</a>。</p></td>
        </tr>
        <tr>
                <td class="etext9" width="80" nowrap="nowrap"><b>DATA</b></td>
                <td class="text11"> </td>
        </tr>
        <tr>
                <td class="etext9" width="80" nowrap="nowrap"> </td>
                <td class="text11">告訴 Server 你已經準備好要開始輸入信件的本文了(Mail Message, 包括 Message
                Header 與 Message Body)。要結束 Message 的輸入時,在信件結尾輸入個 . 點符號即可。</td>
        </tr>
        <tr>
                <td class="etext9" width="80" nowrap="nowrap"><b>NOOP</b></td>
                <td class="text11"> </td>
        </tr>
        <tr>
                <td class="etext9" width="80" nowrap="nowrap"> </td>
                <td class="text11">NOOP 是『NO OPeration 』的縮寫,用途是請 Server 回應一下,藉此讓 Client
                得以確定與 Server 的連線還在。</td>
        </tr>
        <tr>
                <td class="etext9" width="80" nowrap="nowrap"><b>QUIT</b></td>
                <td class="text11"> </td>
        </tr>
        <tr>
                <td class="etext9" width="80" nowrap="nowrap"> </td>
                <td class="text11">結束對話,關閉與 Server 間的連線。</td>
        </tr>
</tbody></table>


<p class="SubTitle2">SMTP Server Reply codes</p>
<p class="text11">每當 Mail Client 丟出一個指令,Server 這端就會回應一個固定格式的訊息:一個 3
位數的數字,然後緊接的一串文字描述,說明 Server 收到 client 端傳來的指令之後,Server 處理完後的情形,例如:</p>
<blockquote>
        <p class="etext9"><b>250 ok</b></p>
</blockquote>
<p class="text11">或是</p>
<blockquote>
        <p class="etext9"><b>221 Bye bye</b></p>
</blockquote>
<p class="text11" align="left">底下列出 Mail Server 回傳的各個代碼以及文字說明(只要下的 SMTP
指令正確,底下絕大部份的代碼應該都不會遇到的!):</p>
<table id="table2" width="90%" border="0" cellpadding="0" cellspacing="0">
        <tbody><tr>
                <td class="etext9" width="80" nowrap="nowrap">
                <p class="etext9"><b>221</b></p></td>
                <td class="text11"> </td>
        </tr>
        <tr>
                <td class="etext9" width="80" nowrap="nowrap"> </td>
                <td class="text11">A system status or help reply</td>
        </tr>
        <tr>
                <td class="etext9" width="80" nowrap="nowrap"><b>214</b></td>
                <td class="text11"> </td>
        </tr>
        <tr>
                <td class="etext9" width="80" nowrap="nowrap"> </td>
                <td class="text11">Help Message</td>
        </tr>
        <tr>
                <td class="etext9" width="80" nowrap="nowrap"><b>220</b></td>
                <td class="text11"> </td>
        </tr>
        <tr>
                <td class="etext9" width="80" nowrap="nowrap"> </td>
                <td class="text11">The server is ready. </td>
        </tr>
        <tr>
                <td class="etext9" width="80" nowrap="nowrap"><b>221</b></td>
                <td class="text11"> </td>
        </tr>
        <tr>
                <td class="etext9" width="80" nowrap="nowrap"> </td>
                <td class="text11">The server is ending the conversation. </td>
        </tr>
        <tr>
                <td class="etext9" width="80" nowrap="nowrap"><b>250</b></td>
                <td class="text11"> </td>
        </tr>
        <tr>
                <td class="etext9" width="80" nowrap="nowrap"> </td>
                <td class="text11">The requested action was completed</td>
        </tr>
        <tr>
                <td class="etext9" width="80" nowrap="nowrap"><b>251</b></td>
                <td class="text11"> </td>
        </tr>
        <tr>
                <td class="etext9" width="80" nowrap="nowrap"> </td>
                <td class="text11">The specified user is not local, but the server will
                forward the mail message</td>
        </tr>
        <tr>
                <td class="etext9" width="80" nowrap="nowrap"><b>354</b></td>
                <td class="text11"> </td>
        </tr>
        <tr>
                <td class="etext9" width="80" nowrap="nowrap"> </td>
                <td class="text11">This is a reply to the DATA command. After getting
                this, start sending the body of the mail message, ending with
                "\r\n.\r\n." </td>
        </tr>
        <tr>
                <td class="etext9" width="80" nowrap="nowrap"><b>421</b></td>
                <td class="text11"> </td>
        </tr>
        <tr>
                <td class="etext9" width="80" nowrap="nowrap"> </td>
                <td class="text11">The mail server will be shut down. Save the mail
                message and try again later</td>
        </tr>
        <tr>
                <td class="etext9" width="80" nowrap="nowrap"><b>450</b></td>
                <td class="text11"> </td>
        </tr>
        <tr>
                <td class="etext9" width="80" nowrap="nowrap"> </td>
                <td class="text11">The mailbox that you are trying to reach is busy.
                Wait a little while and try again. </td>
        </tr>
        <tr>
                <td class="etext9" width="80" nowrap="nowrap"><b>451</b></td>
                <td class="text11"> </td>
        </tr>
        <tr>
                <td class="etext9" width="80" nowrap="nowrap"> </td>
                <td class="text11">The requested action was not done. Some error
                occurmiles in the mail server</td>
        </tr>
        <tr>
                <td class="etext9" width="80" nowrap="nowrap"><b>452</b></td>
                <td class="text11"> </td>
        </tr>
        <tr>
                <td class="etext9" width="80" nowrap="nowrap"> </td>
                <td class="text11">The requested action was not done. The mail server
                ran out of system storage. </td>
        </tr>
        <tr>
                <td class="etext9" width="80" nowrap="nowrap"><b>500</b></td>
                <td class="text11"> </td>
        </tr>
        <tr>
                <td class="etext9" width="80" nowrap="nowrap"> </td>
                <td class="text11">The last command contained a syntax error or the
                command line was too long. </td>
        </tr>
        <tr>
                <td class="etext9" width="80" nowrap="nowrap"><b>501</b></td>
                <td class="text11"> </td>
        </tr>
        <tr>
                <td class="etext9" width="80" nowrap="nowrap"> </td>
                <td class="text11">The parameters or arguments in the last command
                contained a syntax error. </td>
        </tr>
        <tr>
                <td class="etext9" width="80" nowrap="nowrap"><b>502</b></td>
                <td class="text11"> </td>
        </tr>
        <tr>
                <td class="etext9" width="80" nowrap="nowrap"> </td>
                <td class="text11">The mail server has not implemented the last command.
                </td>
        </tr>
        <tr>
                <td class="etext9" width="80" nowrap="nowrap"><b>503</b></td>
                <td class="text11"> </td>
        </tr>
        <tr>
                <td class="etext9" width="80" nowrap="nowrap"> </td>
                <td class="text11">The last command was sent out of sequence. For
                example, you might have sent DATA before sending RECV</td>
        </tr>
        <tr>
                <td class="etext9" width="80" nowrap="nowrap"><b>504</b></td>
                <td class="text11"> </td>
        </tr>
        <tr>
                <td class="etext9" width="80" nowrap="nowrap"> </td>
                <td class="text11">One of the parameters of the last command has not
                been implemented by the server. </td>
        </tr>
        <tr>
                <td class="etext9" width="80" nowrap="nowrap"><b>550</b></td>
                <td class="text11"> </td>
        </tr>
        <tr>
                <td class="etext9" width="80" nowrap="nowrap"> </td>
                <td class="text11">The mailbox that you are trying to reach can't be
                found or you don't have access rights. </td>
        </tr>
        <tr>
                <td class="etext9" width="80" nowrap="nowrap"><b>551</b></td>
                <td class="text11"> </td>
        </tr>
        <tr>
                <td class="etext9" width="80" nowrap="nowrap"> </td>
                <td class="text11">The specified user is not local; part of the text of
                the message will contain a forwarding address. </td>
        </tr>
        <tr>
                <td class="etext9" width="80" nowrap="nowrap"><b>552</b></td>
                <td class="text11"> </td>
        </tr>
        <tr>
                <td class="etext9" width="80" nowrap="nowrap"> </td>
                <td class="text11">The mailbox that you are trying to reach has run out
                of space. Store the message and try again tomorrow or in a few
                days-after the user gets a chance to delete some messages.</td>
        </tr>
        <tr>
                <td class="etext9" width="80" nowrap="nowrap"><b>553</b></td>
                <td class="text11"> </td>
        </tr>
        <tr>
                <td class="etext9" width="80" nowrap="nowrap"> </td>
                <td class="text11">The mail address that you specified was not
                syntactically correct</td>
        </tr>
        <tr>
                <td class="etext9" width="80" nowrap="nowrap"><b>554</b></td>
                <td class="text11"> </td>
        </tr>
        <tr>
                <td class="etext9" width="80" nowrap="nowrap"> </td>
                <td class="text11">The mail transaction has failed for unknown causes.
                </td>
        </tr>
</tbody></table>
<p class="SubTitle2"><a name="SMTP_Authentication">SMTP Authentication</a></p>
<p class="text11">由於 SMTP 協定極為簡單,任何人只要懂得 SMTP 指定,就有可能寫出自動發廣告信、垃圾信的程式。為了防堵這些日益氾濫的垃圾信件,後來就出現了加強版的 ESMTP 協定 (Enhanced SMTP ),支援 ESMTP 的 Mail Server
會對那些和它連線的 Mail Client 有較為嚴格的要求,在某些狀況下,例如要透過 Server
發信到其它地方,而非寄到本地的信箱時,限制他們必須先通過身份的驗證,才允許發信出去。(換句話說,如果你透過了身份的確認,要發信到什麼地方都是可以的!)</p>
<p class="text11">一般而言,現在網路上的 Mail Server 主要支援兩種驗證身份的方式: 1. 明碼驗證 和 2. base64
編碼驗證。</p>
<ol><li>
        <p class="text11">明碼驗證</p></li></ol>
<blockquote>
        <p class="text11">明碼驗證指的是,當 Client 要跟 Server 進行身份驗證時,傳送的 username 和 password 不需要經過任何編碼,只要送給
        Server 端 AUTH PLAIN 這個指令後,再直接將 username 和 password 傳給 Server 端,Server
        就會進行身份的驗證。(註:大部份 Mail Server 甚至連明碼驗證都覺得不夠安全,因而捨棄這種方式只支援 base64 編碼驗證!)</p>
</blockquote>
<ol><li value="2">
        <p class="text11" align="left">base64 編碼驗證</p></li></ol>

        <p class="text11">當 Client 端送出 AUTH LOGIN 這個指令到 Server 端時,代表著要使用 base64
        編碼的身份驗證。此時 Client 端就必須先將 username 和 password 以 base64 演算法編碼之後,再傳送給 Server
        端,Server 這端在拿到編碼字串後先做解碼然後再進行驗證。底下是個 base64 編碼驗證成功的範例畫面,畫面上的 username 和 password 已經使用 base64 演算法編碼過。:</p>
        <p class="text11" align="center">
        <img src="http://www.cqinc.com.tw/coopermaa/932-DC/practice/images/SMTPCm3.gif" border="0"></p>
        <p class="text11">你可以不用自己撰寫 base64 演算法,有一些網站提供了線上
        base64 編碼的服務,例如:<a target="_blank" href="http://www.motobit.com/util/base64-decoder-encoder.asp">http://www.motobit.com/util/base64-decoder-encoder.asp</a>。在
        ActiveTcl 中,也有 base64 的套件可供使用,底下是個 base64 編碼的範例: </p>
       
        <!-- BEGIN: CUT HERE FOR EMBEDDING -->

    <span style="font-family: 'Courier New'; font-size: 10pt; font-weight: 400; color: rgb(64, 64, 64);">
        <span style="color: rgb(0, 128, 0);"># Load base64 package</span><br>
        <span style="color: rgb(189, 15, 146);">package</span> require base64<br><br>
        <span style="color: rgb(0, 128, 0);"># encode username "maa", you'll get "</span></span><font color="#ff0000"><span style="font-family: 'Courier New'; font-size: 10pt; font-weight: 400;">bWFh</span></font><span style="font-family: 'Courier New'; font-size: 10pt; font-weight: 400; color: rgb(64, 64, 64);"><span style="color: rgb(0, 128, 0);">"</span><br>base64<span style="color: rgb(189, 15, 146);">:</span><span style="color: rgb(189, 15, 146);">:</span>encode maa<br>
        <br><span style="color: rgb(0, 128, 0);"># encode password "12345", you'll get "</span></span><font color="#ff0000"><span style="font-family: 'Courier New'; font-size: 10pt; font-weight: 400;">MTIzNDU=</span></font><span style="font-family: 'Courier New'; font-size: 10pt; font-weight: 400; color: rgb(64, 64, 64);"><span style="color: rgb(0, 128, 0);">"</span><br>base64<span style="color: rgb(189, 15, 146);">:</span><span style="color: rgb(189, 15, 146);">:</span></span><span style="font-family: 'Courier New'; font-size: 10pt; font-weight: 400; color: rgb(64, 64, 64);">encode </span>
        <span style="font-family: 'Courier New'; font-size: 10pt; font-weight: 400; color: rgb(64, 64, 64);">12345</span><br>
頁: [1]
查看完整版本: [轉貼] SMTP 指令說明