初探smtp协议

初探smtp协议

百度百科

SMTP(Simple Mail Transfer Protocol)即简单邮件传输协议,它是一组用于由源地址到目的地址传送邮件的规则,由它来控制信件的中转方式。SMTP协议属于TCP/IP协议簇,它帮助每台计算机在发送或中转信件时找到下一个目的地。通过SMTP协议所指定的服务器,就可以把E-mail寄到收信人的服务器上了,整个过程只要几分钟。SMTP服务器则是遵循SMTP协议的发送邮件服务器,用来发送或中转发出的电子邮件。
(传送门)

客户端和邮件服务器的交互过程

连接和发送邮件步骤如下:

  (1)建立TCP连接;

  (2)客户端发送HELO命令以标识发件人的身份,服务器以OK作为响应,表明准备接收;

  (3)使用AUTH命令登录SMTP服务器,输入用户名和密码(用户名和密码要用base64进行加密);

  (4)客户端发送MAIL命令输入发件人邮箱地址,服务器以OK作为响应;

  (5)客户端发送RCPT命令,标识该电子邮件的计划接收人,服务器以OK作为响应;

  (6)协商结束后,使用DATA命令发送;

  (7)以"."号表示结束,输入内容一起发送出去,

  (8)结束此次发送,用QUIT命令退出。

PHPmailer发送示例

    2017-06-16 14:29:13     SERVER -> CLIENT: 220 hello
    2017-06-16 14:29:13     CLIENT -> SERVER: EHLO heqian-pc
    2017-06-16 14:29:13     SERVER -> CLIENT: 250-localhost Hello EHLO heqian-pc
                                              250-SIZE 131072
                                              250-PIPELINING
                                              250-AUTH PLAIN LOGIN
                                              250 HELP
    2017-06-16 14:29:13     CLIENT -> SERVER: AUTH LOGIN
    2017-06-16 14:29:13     SERVER -> CLIENT: 334 VXNlcm5hbWU=
    2017-06-16 14:29:13     CLIENT -> SERVER:
    2017-06-16 14:29:13     SERVER -> CLIENT: 334 UGFzc3dvcmQ=
    2017-06-16 14:29:13     CLIENT -> SERVER:
    2017-06-16 14:29:13     SERVER -> CLIENT: 235 Authentication successful
    2017-06-16 14:29:13     CLIENT -> SERVER: MAIL FROM:
    2017-06-16 14:29:13     SERVER -> CLIENT: 250 Ok
    2017-06-16 14:29:13     CLIENT -> SERVER: RCPT TO:
    2017-06-16 14:29:13     SERVER -> CLIENT: 250 Ok
    2017-06-16 14:29:13     CLIENT -> SERVER: DATA
    2017-06-16 14:29:13     SERVER -> CLIENT: 354 End data with .
    2017-06-16 14:29:13     CLIENT -> SERVER: Date: Fri, 16 Jun 2017 14:29:13 +0000
    2017-06-16 14:29:13     CLIENT -> SERVER: To: [email protected]
    2017-06-16 14:29:13     CLIENT -> SERVER: From: =?utf-8?B?5piT6IGU5LqR?= 
    2017-06-16 14:29:13     CLIENT -> SERVER: Reply-To: =?utf-8?B?5piT6IGU5LqR?= <[email protected]>
    2017-06-16 14:29:13     CLIENT -> SERVER: Subject: =?utf-8?B?5piT6IGU5LqR6aqM6K+B56CB?=
    2017-06-16 14:29:13     CLIENT -> SERVER: Message-ID: 
    2017-06-16 14:29:13     CLIENT -> SERVER: X-Mailer: PHPMailer 5.2.23 (https://github.com/PHPMailer/PHPMailer)
    2017-06-16 14:29:13     CLIENT -> SERVER: MIME-Version: 1.0
    2017-06-16 14:29:13     CLIENT -> SERVER: Content-Type: multipart/alternative;
    2017-06-16 14:29:13     CLIENT -> SERVER:       boundary="b1_a2430819ef128b7b55181b6ebd2e021e"
    2017-06-16 14:29:13     CLIENT -> SERVER: Content-Transfer-Encoding: 8bit
    2017-06-16 14:29:13     CLIENT -> SERVER:
    2017-06-16 14:29:13     CLIENT -> SERVER: This is a multi-part message in MIME format.
    2017-06-16 14:29:13     CLIENT -> SERVER:
    2017-06-16 14:29:13     CLIENT -> SERVER: --b1_a2430819ef128b7b55181b6ebd2e021e
    2017-06-16 14:29:13     CLIENT -> SERVER: Content-Type: text/plain; charset=utf-8
    2017-06-16 14:29:13     CLIENT -> SERVER: Content-Transfer-Encoding: 8bit
    2017-06-16 14:29:13     CLIENT -> SERVER:
    2017-06-16 14:29:13     CLIENT -> SERVER: 你好 smtp
    2017-06-16 14:29:13     CLIENT -> SERVER:
    2017-06-16 14:29:13     CLIENT -> SERVER:
    2017-06-16 14:29:13     CLIENT -> SERVER: --b1_a2430819ef128b7b55181b6ebd2e021e
    2017-06-16 14:29:13     CLIENT -> SERVER: Content-Type: text/html; charset=utf-8
    2017-06-16 14:29:13     CLIENT -> SERVER: Content-Transfer-Encoding: 8bit
    2017-06-16 14:29:13     CLIENT -> SERVER:
    2017-06-16 14:29:13     CLIENT -> SERVER: 

你好 smtp

2017-06-16 14:29:13 CLIENT -> SERVER: 2017-06-16 14:29:13 CLIENT -> SERVER: 2017-06-16 14:29:13 CLIENT -> SERVER: 2017-06-16 14:29:13 CLIENT -> SERVER: --b1_a2430819ef128b7b55181b6ebd2e021e-- 2017-06-16 14:29:13 CLIENT -> SERVER: 2017-06-16 14:29:13 CLIENT -> SERVER: . 2017-06-16 14:29:13 SERVER -> CLIENT: 250 ok:queued as 2017-06-16 14:29:13 CLIENT -> SERVER: QUIT 2017-06-16 14:29:13 SERVER -> CLIENT: 221 Bye

跨邮件服务器之间的通信

连接和投递步骤如下:

  (1)建立TCP连接;

  (2)服务器发送HELO命令以标识发件人的身份,目标服务器以OK作为响应,表明准备接收;

  (3)服务器发送MAIL 命令输入发件人邮箱地址,目标服务器以OK作为响应;

  (4)服务器发送RCPT命令,标识该电子邮件的计划接收人,目标服务器以OK作为响应;

  (5)协商结束后,使用DATA命令发送;

  (6)以"."号表示结束,输入内容一起发送出去,

  (7)结束此次发送,用QUIT命令退出。

PHPmailer发送示例

    2017-06-16 14:24:41     SERVER -> CLIENT: 220 hello
    2017-06-16 14:24:41     CLIENT -> SERVER: EHLO heqian-pc
    2017-06-16 14:24:41     SERVER -> CLIENT: 250-localhost Hello EHLO heqian-pc
                                              250-SIZE 131072
                                              250-PIPELINING
                                              250 HELP
    2017-06-16 14:24:41     CLIENT -> SERVER: MAIL FROM:
    2017-06-16 14:24:41     SERVER -> CLIENT: 250 Ok
    2017-06-16 14:24:41     CLIENT -> SERVER: RCPT TO:
    2017-06-16 14:24:41     SERVER -> CLIENT: 250 Ok
    2017-06-16 14:24:41     CLIENT -> SERVER: DATA
    2017-06-16 14:24:41     SERVER -> CLIENT: 354 End data with .
    2017-06-16 14:24:41     CLIENT -> SERVER: Date: Fri, 16 Jun 2017 14:24:41 +0000
    2017-06-16 14:24:41     CLIENT -> SERVER: To: [email protected]
    2017-06-16 14:24:41     CLIENT -> SERVER: From: =?utf-8?B?5piT6IGU5LqR?= 
    2017-06-16 14:24:41     CLIENT -> SERVER: Reply-To: =?utf-8?B?5piT6IGU5LqR?= <[email protected]>
    2017-06-16 14:24:41     CLIENT -> SERVER: Subject: =?utf-8?B?5piT6IGU5LqR6aqM6K+B56CB?=
    2017-06-16 14:24:41     CLIENT -> SERVER: Message-ID: <6fc893187701010670b0d152b2d954f6@heqian-pc>
    2017-06-16 14:24:41     CLIENT -> SERVER: X-Mailer: PHPMailer 5.2.23 (https://github.com/PHPMailer/PHPMailer)
    2017-06-16 14:24:41     CLIENT -> SERVER: MIME-Version: 1.0
    2017-06-16 14:24:41     CLIENT -> SERVER: Content-Type: multipart/alternative;
    2017-06-16 14:24:41     CLIENT -> SERVER:       boundary="b1_6fc893187701010670b0d152b2d954f6"
    2017-06-16 14:24:41     CLIENT -> SERVER: Content-Transfer-Encoding: 8bit
    2017-06-16 14:24:41     CLIENT -> SERVER:
    2017-06-16 14:24:41     CLIENT -> SERVER: This is a multi-part message in MIME format.
    2017-06-16 14:24:41     CLIENT -> SERVER:
    2017-06-16 14:24:41     CLIENT -> SERVER: --b1_6fc893187701010670b0d152b2d954f6
    2017-06-16 14:24:41     CLIENT -> SERVER: Content-Type: text/plain; charset=utf-8
    2017-06-16 14:24:41     CLIENT -> SERVER: Content-Transfer-Encoding: 8bit
    2017-06-16 14:24:41     CLIENT -> SERVER:
    2017-06-16 14:24:41     CLIENT -> SERVER: 你好 smtp
    2017-06-16 14:24:41     CLIENT -> SERVER:
    2017-06-16 14:24:41     CLIENT -> SERVER:
    2017-06-16 14:24:41     CLIENT -> SERVER: --b1_6fc893187701010670b0d152b2d954f6
    2017-06-16 14:24:41     CLIENT -> SERVER: Content-Type: text/html; charset=utf-8
    2017-06-16 14:24:41     CLIENT -> SERVER: Content-Transfer-Encoding: 8bit
    2017-06-16 14:24:41     CLIENT -> SERVER:
    2017-06-16 14:24:41     CLIENT -> SERVER: 

你好 smtp

2017-06-16 14:24:41 CLIENT -> SERVER: 2017-06-16 14:24:41 CLIENT -> SERVER: 2017-06-16 14:24:41 CLIENT -> SERVER: 2017-06-16 14:24:41 CLIENT -> SERVER: --b1_6fc893187701010670b0d152b2d954f6-- 2017-06-16 14:24:41 CLIENT -> SERVER: 2017-06-16 14:24:41 CLIENT -> SERVER: . 2017-06-16 14:24:41 SERVER -> CLIENT: 250 ok:queued as 2017-06-16 14:24:41 CLIENT -> SERVER: QUIT 2017-06-16 14:24:41 SERVER -> CLIENT: 221 Bye

怎么查看从邮件服务器到网易MX服务器的SMTP记录?

是不是发现邮件服务器之间和客户端与邮件服务器之间的协议是如此的相似呢。
他们有什么区别呢。
一般情况下,客户端与服务器交互是通过一个域名实现的,例如:smtp.qq.com,smtp.163.com,smtp.sina.cn
而邮箱服务器之间一般是通过邮箱后缀的mx邮件交换记录来完成的。如何查看邮件服务器的mx记录

站长之家网页在线查询

你可能感兴趣的:(初探smtp协议)