计算机网络---命令行邮箱登录发信及python实现

计算机网络套接字编程

Your task is to develop a simple mail client that sends email to any recipient. Your client will need to connect to a mail server, dialogue with the mail server using the SMTP protocol, and send an email message to the mail server. Python provides a module, called smtplib, which has built in methods to send mail using SMTP protocol. However, we will not be using this module in this lab, because it hide the details of SMTP and socket programming.
创建一个向任何接收方发送电子邮件的简单邮件客户。客户端必须与邮件服务器创建一个TCP连接,使用SMTP协议与邮件服务器进行交谈,经该邮件服务器向某接收方发送一个电子邮件报文,最后关闭邮件服务器的TCP连接

postfix学习
在命令行下可以实行

cyc@cyc-Inspiron-7460:/home/fmaster$ netcat smtp.163.com 25    //bash
220 163.com Anti-spam GT for Coremail System (163com[20141201])   //server
helo [email protected]                                          //client
250 OK                                                            //s
auth login														  //c	
334 dXNlcm5hbWU6												//s
			               //此处添加用户名的账号的base64编码c
334 UGFzc3dvcmQ6												//s
                             //此处输入密码的base64编码		//c
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>
from:[email protected]
to:[email protected]
Subject:My

hi,huhi
hu
aa
.
250 Mail OK queued as smtp10,DsCowAAXicSbn6tbZwHHSA--.16143S2 1537974289

下面看python代码

from socket import *
import base64


def receiveFromServer():
    msg = clientSocket.recv(1024)  //1024 buffersize
    print(msg)

    return msg


msg = "this is demo\r\n"

endmsg = "\r\n.\r\n"
mailServer = "smtp.163.com"
user = base64.b64encode(b'[email protected]').decode() + '\r\n'
pasword = base64.b64encode(b'*******').decode() + '\r\n' ## ****** 处是密码
clientSocket = socket(AF_INET, SOCK_STREAM)
clientSocket.connect((mailServer, 25))
recv = receiveFromServer()

if (recv[:3] != '220'):
    print
    '220 reply not received from server.'

helloCommand = 'helo fanse\r\n'
clientSocket.sendall(helloCommand.encode())           //sendall方法byte型

recv1 = receiveFromServer()
if (recv1[:3] != '250'):
    print
    '250 reply not received from server.'

clientSocket.sendall(('auth login\r\n').encode())
receiveFromServer()
clientSocket.sendall(('MTM5NDk1NjkwMjdAMTYzLmNvbQ==\r\n').encode())
receiveFromServer()
clientSocket.sendall(('Y3ljOTIxNTA3\r\n').encode())  # The base64 code of your password
receiveFromServer()
# Send mail from command
clientSocket.sendall(('mail from: <[email protected]>\r\n').encode())
receiveFromServer()
clientSocket.sendall(('rcpt to: <[email protected]>\r\n').encode())
receiveFromServer()
clientSocket.sendall(('data\r\n').encode())
clientSocket.recv(1024)
clientSocket.sendall(('from:[email protected]\r\n').encode())
clientSocket.sendall(('to:[email protected]\r\n').encode())
clientSocket.sendall(('subject:kuku is smart\r\n').encode())
# clientSocket.sendall('Content-Type:text/plain\t\n')  # WTF?
clientSocket.sendall(('\r\n').encode())
clientSocket.sendall(msg.encode())
clientSocket.sendall(endmsg.encode())
receiveFromServer()
clientSocket.sendall(('quit\r\n').encode())
receiveFromServer()

主要注意登录方式
网易云的退信的代码说明

列表中"LOGIN PLAIN CRAM-MD5"说明了该SMTP Server支持的验证方式,本文将详细解释这三种验证方式。

ehlo 
250-mail
250-PIPELINING
250-AUTH LOGIN PLAIN
250-AUTH=LOGIN PLAIN
250-coremail 1Uxr2xKj7kG0xkI17xGrU7I0s8FY2U3Uj8Cz28x1UUUUU7Ic2I0Y2UFOX00SUCa0xDrUUUUj
250-STARTTLS
250 8BITMIME

列表中"LOGIN PLAIN"说明了该SMTP Server支持的验证方式.

LOGIN方式
使用login方式的验证序列如下 (C:表示Client,S:表示Server)

C:auth login ------------------------------------------------- 进行用户身份认证 
S:334 VXNlcm5hbWU6 ----------------------------------- BASE64编码“Username:”
C:Y29zdGFAYW1heGl0Lm5ldA== ----------------------------------- 用户名,使用BASE64编码
S:334 UGFzc3dvcmQ6 -------------------------------------BASE64编码"Password:"
C: ***************  ----------------------------------------------- 密码,使用BASE64编码  //省略密码的编码
S:235 auth successfully -------------------------------------- 身份认证成功

猜你喜欢

转载自blog.csdn.net/qq_37976565/article/details/88651119