使用python socket实现smtp协议,出现了神奇的问题

xuzheng00 2017-12-31 02:05:36
问题在于虚线之间,用户信息验证不通过,我仔细检查过协议里登录的格式,没有发现问题。我163邮箱已经开启了smtp服务,密码也用的是服务密码。由于只是实验用的邮箱,服务密码没有屏蔽,大佬可以拿去试一下= =难道现在smtp邮件已经改了发送方式吗?= =
from socket import *           
import base64

msg = "\r\n I Love Computer Networks!" #Declarations
endmsg="\r\n.\r\n"
mailfrom = "MAIL FROM:<l847238926@163.com>\r\n"
rcptto="RCPT TO:<754845950@qq.com>\r\n"
data = "DATA\r\n"
quitmsg="QUIT\r\n"

#Choose a mail server (e.g. Google mail server) and call it mailserver
mailserver="smtp.163.com" #Use the local Host as SMTP Server
mailPort=25 #Specify Port For Local SMTP
connectaddress=(mailserver,mailPort)

#Create socket called clientsocket and establish a TCP connection with mailserver
clientSocket=socket(AF_INET,SOCK_STREAM) #Assign IP address and port number
clientSocket.connect(connectaddress) #Connect to Local SMTP
recv=clientSocket.recv(1024)
print(recv)
if recv[:3].decode()!='220': #Print Error Clause
print("220 reply not received from server")

#Send HELO command and print server response.
heloCommand="HELO Alice\r\n"
clientSocket.send(bytes(heloCommand.encode()))
recv1=clientSocket.recv(1024)
print(recv1.decode())
if recv1.decode()[:3]!="250":
print("250 reply not received from server.")

print('-----------------------------------')

loginini = b'AUTH LOGIN\r\n'
userCommand = base64.b64encode(b'18390242095@163.com\r\n')
passwordCommand = base64.b64encode(b'xudoudou3661615\r\n')
clientSocket.send(loginini)
recv2 = clientSocket.recv(1024).decode('utf-8')
print('222+ ',recv2)

clientSocket.send(userCommand)
recv3 = clientSocket.recv(1024).decode('utf-8')
print('333+ ',recv3)

clientSocket.send(passwordCommand)
recv4 = clientSocket.recv(1024).decode('utf-8')
print('444+ ',recv4)
print('-----------------------------------')
#Send MAIL FROM command and print server response.
clientSocket.send(bytes(mailfrom.encode())) #send mail, had to convert to bytes
check=clientSocket.recv(1024)
print(check) #print confirmation of working messages

#Send RCPT TO command and print server response.
clientSocket.send(bytes(rcptto.encode())) #recieve, had to convert to bytes
check1=clientSocket.recv(1024)
print(check1) #print confirmation of working messages

#Send DATA command and print server response.
clientSocket.send(bytes(data.encode())) #DATA, had to convert to bytes
check2=clientSocket.recv(1024)
print(check2) #print confirmation of working messages

#Message ends with a single period.
clientSocket.send(bytes((mailfrom+msg+endmsg).encode())) #Concatinate bits for message
check3=clientSocket.recv(1024)
print(check3) #print confirmation of working messages

#Send QUIT command and get server response.
clientSocket.send(bytes(quitmsg.encode())) #Quit the session
check4=clientSocket.recv(1024)
print(check4)
...全文
1051 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
王大凤 2018-06-17
  • 打赏
  • 举报
回复
其实上一个人说的是对的啊,但是因为bytes不能和str相加,所以先把bytes转成str再相加,发送的时候再userCommand.encode()就可以了。我这样之后就可以显示authentificaion successfully啦。

userCommand = base64.b64encode(b'18390242095@163.com').decode()+'\r\n'
passwordCommand = base64.b64encode(b'xudoudou3661615').decode()+'\r\n'
REDSENET 2018-03-18
  • 打赏
  • 举报
回复

userCommand = base64.b64encode(b'18390242095@163.com\r\n') 
passwordCommand = base64.b64encode(b'xudoudou3661615\r\n')
改成

userCommand = base64.b64encode(b'18390242095@163.com')+'\r\n'
passwordCommand = base64.b64encode(b'xudoudou3661615')+'\r\n'
应该就可以了
xuzheng00 2018-01-01
  • 打赏
  • 举报
回复
引用 4 楼 oyljerry的回复:
anti spam。看上去是你发送的邮件被服务器认为是垃圾邮件了
啊为什么会被认定为垃圾邮件?因为ip的原因?我这段代码运行到登录认证部分就卡住了,内容部分在登录部分下面,还没有运行到。我用python的smtplib模块发邮件可以成功发送
xuzheng00 2018-01-01
  • 打赏
  • 举报
回复
啊为什么会被认定为垃圾邮件?我这段代码运行到登录认证部分就卡住了,内容部分在登录部分下面,还没有运行到。我用python的smtplib模块发邮件可以成功发送
oyljerry 2018-01-01
  • 打赏
  • 举报
回复
anti spam。看上去是你发送的邮件被服务器认为是垃圾邮件了
xuzheng00 2017-12-31
  • 打赏
  • 举报
回复
xuzheng00 2017-12-31
  • 打赏
  • 举报
回复
终端: b'220 163.com Anti-spam GT for Coremail System (163com[20141201])\r\n' 250 OK ----------------------------------- 222+ 334 dXNlcm5hbWU6 会停在这里 单步调试发现会停在发送登录用户名时返回的信息那里
http://www.amazon.com/Foundations-Python-Network-Programming-Brandon/dp/1430258543 这本书是2014年底出版的,基于最新的 python3.4 版本。 配书源码链接 https://github.com/brandon-rhodes/fopnp 目录 Chapter 1: Introduction to Client-Server Networking Chapter 2: UDP Chapter 3: TCP Chapter 4: Socket Names and DNS Chapter 5: Network Data and Network Errors Chapter 6: TLS/SSL Chapter 7: Server Architecture Chapter 8: Caches and Message Queues Chapter 9: HTTP Clients Chapter 10: HTTP Servers Chapter 11: The World Wide Web Chapter 12: Building and Parsing E-Mail Chapter 13: SMTP Chapter 14: POP Chapter 15: IMAP Chapter 16: Telnet and SSH Chapter 17: FTP Chapter 18: RPC Instead, this book focuses on network programming, using Python 3 for every example script and snippet of code at the Python prompt. These examples are intended to build a comprehensive picture of how network clients, network servers, and network tools can best be constructed from the tools provided by the language. Readers can study the transition from Python 2 to Python 3 by comparing the scripts used in each chapter of the second edition of this book with the listings here in the third edition—both of which are available at https://github.com/brandon-rhodes/fopnp/tree/m/ thanks to the excellent Apress policy of making source code available online. The goal in each of the following chapters is simply to show you how Python 3 can best be used to solve modern network programming problems.

37,743

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • WuKongSecurity@BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧