用telnet发送电子邮件(转载)

Posted by Smeagol | Posted in Uncategorized | Posted on 10-10-2008

SMTP基本命令集:

  命令 描述

  HELO 向服务器标识用户身份,发送者能欺骗,说谎,但一般情况下服务器都能检测到

  MAIL 初始化邮件传输 mail from:

  RCPT 标识单个的邮件接收人;常在MAIL命令后面,可有多个rcpt to:

  DATA 在单个或多个RCPT命令后,表示所有的邮件接收人已标识,并初始化数据传输,以.结束

  VRFY 用于验证指定的用户/邮箱是否存在;由于安全方面的原因,服务器常禁止此命令

  EXPN 验证给定的邮箱列表是否存在,扩充邮箱列表,也常被禁用BR>  HELP 查询服务器支持什么命令

  NOOP 无操作,服务器应响应OK

  QUIT 结束会话

  RSET 重置会话,当前传输被取消

  MAIL FROM命令中指定的地址是称作envelope from地址,不需要和发送者自己的地址是一致的

  RCPT TO 与之等同,指明的接收者地址称为envelope to地址,而与实际的to:行是什么无关

 

现在的SMTP服务器一般都需要身份验证,下面是一个telnet发送邮件的例子:

 

telnet smtp.sina.com.cn 25

220 smtp.sina.com.cn ESMTP SINAMAIL (Postfix Rules!)

EHLO sina.com.cn

250-smtp.sina.com.cn

250-PIPELINING

250-SIZE 19660800

250-VRFY

250-ETRN

250-AUTH LOGIN

250-AUTH=LOGIN

250 8BITMIME

AUTH LOGIN

334 VXNlcm5hbWU6

dXNlcm5hbWU=

334 UGFzc3dvcmQ6

bXlwYXNzd29yZA==

235 Authentication successful

MAIL FROM: <test@sina.com>

250 Ok

RCPT TO: <test@163.com>

250 Ok

DATA

502 Error: command not implemented

Data

354 End data with <CR><LF>.<CR><LF>

hello,this is a test.

.

250 Ok: queued as CB9AE175F6

Quit

221 Bye

 

下面是身份验证的部分

AUTH LOGIN‘客户端输入

334 VXNlcm5hbWU6‘服务端返回,明文为Username:

dXNlcm5hbWU=‘客户端输入,明文为username,即登陆用户名

334 UGFzc3dvcmQ6‘服务端返回,明文为Password:

bXlwYXNzd29yZA==‘客户端输入,明文为mypassword

235 Authentication successful‘服务端返回成功信息

 

其他部分应该没什么问题,身份验证部分使用了Base64编码,需要进行转换

但只要使用下面这段HTML代码就能轻松进行转化

源代码:

<html>

   <head>

      <title>base64 Encoding/Decoding</title>

   </head>

   <script type=”text/javascript”><!–

   var keyStr = “ABCDEFGHIJKLMNOP” +

                “QRSTUVWXYZabcdef” +

                “ghijklmnopqrstuv” +

                “wxyz0123456789+/” +

                “=”;

   function encode64(input) {

      input = escape(input);

      var output = “”;

      var chr1, chr2, chr3 = “”;

      var enc1, enc2, enc3, enc4 = “”;

      var i = 0;

      do {

         chr1 = input.charCodeAt(i++);

         chr2 = input.charCodeAt(i++);

         chr3 = input.charCodeAt(i++);

         enc1 = chr1 >> 2;

         enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);

         enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);

         enc4 = chr3 & 63;

         if (isNaN(chr2)) {

            enc3 = enc4 = 64;

         } else if (isNaN(chr3)) {

            enc4 = 64;

         }

         output = output +

            keyStr.charAt(enc1) +

            keyStr.charAt(enc2) +

            keyStr.charAt(enc3) +

            keyStr.charAt(enc4);

         chr1 = chr2 = chr3 = “”;

         enc1 = enc2 = enc3 = enc4 = “”;

      } while (i < input.length);

      return output;

   }

   function decode64(input) {

      var output = “”;

      var chr1, chr2, chr3 = “”;

      var enc1, enc2, enc3, enc4 = “”;

      var i = 0;

      // remove all characters that are not A-Z, a-z, 0-9, +, /, or =

      var base64test = /[^A-Za-z0-9\\+\\/\\=]/g;

      if (base64test.exec(input)) {

         alert(“There were invalid base64 characters in the input text.\\n” +

               “Valid base64 characters are A-Z, a-z, 0-9, ‘+’, ‘/’, and ‘=’\\n” +

               “Expect errors in decoding.”);

      }

      input = input.replace(/[^A-Za-z0-9\\+\\/\\=]/g, “”);

      do {

         enc1 = keyStr.indexOf(input.charAt(i++));

         enc2 = keyStr.indexOf(input.charAt(i++));

         enc3 = keyStr.indexOf(input.charAt(i++));

         enc4 = keyStr.indexOf(input.charAt(i++));

         chr1 = (enc1 << 2) | (enc2 >> 4);

         chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);

         chr3 = ((enc3 & 3) << 6) | enc4;

         output = output + String.fromCharCode(chr1);

         if (enc3 != 64) {

            output = output + String.fromCharCode(chr2);

         }

         if (enc4 != 64) {

            output = output + String.fromCharCode(chr3);

         }

         chr1 = chr2 = chr3 = “”;

         enc1 = enc2 = enc3 = enc4 = “”;

      } while (i < input.length);

      return unescape(output);

   }

   //–></script>

   <body>

      <form name=”base64Form”>

         Type in the message you want to encode in base64, or paste<br>

         base64 encoded text into the text field, select Encode or Decode, <br>

         and click the button!<br>

         <textarea name=”theText” cols=”40″ rows=”6″></textarea><br>

         <input type=”button” name=”encode” value=”Encode to base64″

            onClick=”document.base64Form.theText.value=encode64(document.base64Form.theText.value);”>

         <input type=”button” name=”decode” value=”Decode from base64″

            onClick=”document.base64Form.theText.value=decode64(document.base64Form.theText.value);”>

      </form>

   </body>

</html>

Post your comment