用socket发送匿名邮件之python实现

简介: 发送邮件可以用smtp协议,整个过程为:用户代理(user-agent,比如outlook、foxmail等邮件客户端)---(smtp协议)--->本地邮件服务器 --- (smtp协议)---> 远程邮件服务器 --- (imap,pop3或http协议)--->远程客户代理(收取邮件)其中本地邮件服务器和远程邮件服务器是直通式,一般不经过中转,如果远程邮件服务器没开启等原因导致发送失败那么过一段时间后重复发送。
+关注继续查看

发送邮件可以用smtp协议,整个过程为:

用户代理(user-agent,比如outlook、foxmail等邮件客户端)---(smtp协议)--->本地邮件服务器 --- (smtp协议)---> 远程邮件服务器 --- (imap,pop3或http协议)--->远程客户代理(收取邮件)

其中本地邮件服务器和远程邮件服务器是直通式,一般不经过中转,如果远程邮件服务器没开启等原因导致发送失败那么过一段时间后重复发送。这是本地邮件服务器的职责。

smtp是应用层协议,下面需要tcp协议支撑。smtp报文作为tcp报文的数据部分进行传输。因此我们自行创建TCP socket,并将smtp报文作为数据,塞到tcp socket中进行发送。

smtp报文的构造,根据协议,包括MAIL FROM, RCPT TO, DATA, . ,QUIT等部分。构造起来不复杂。

最后一个子问题是获取邮件服务器的地址。通过nslookup -qt=mx xxx.com来获取,比如nslookup -qt=mx 163.com得到163mx02.mxmail.netease.com,端口一般是25。

那么接下来就是code

#coding:utf-8
from socket import *
msg = "\r\n I love computer networks!"   #需要发送的数据
endmsg = "\r\n.\r\n"
# Choose a mail server (e.g. Google mail server) and call it mailserver
mailserver = ("163mx02.mxmail.netease.com",25)  #Fill in start #Fill in end
# Create socket called clientSocket and establish a TCP connection with mailserver
#Fill in start
clientSocket = socket(AF_INET, SOCK_STREAM)
clientSocket.connect(mailserver)
#Fill in end
recv = clientSocket.recv(1024)
print 'recv:',recv
if recv[:3] != '220':
    print '220 reply not received from server.'
# Send HELO command and print server response.
heloCommand = 'HELO Alice\r\n'
clientSocket.send(heloCommand)
recv1 = clientSocket.recv(1024)
print 'recv1:',recv1
if recv1[:3] != '250':
    print '250 reply not received from server.'

# Send MAIL FROM command and print server response.
# Fill in start
fromCommand = "MAIL FROM:<system@net.cn>\r\n"  #匿名邮件的「发送人」,可以随意伪造
clientSocket.send(fromCommand)
recv2 = clientSocket.recv(1024)
print 'recv2:', recv2
# Fill in end
# Send RCPT TO command and print server response.
# Fill in start
toCommand = "RCPT TO:<superman@163.com>\r\n"   #收件人地址。
clientSocket.send(toCommand)
recv3 = clientSocket.recv(1024)
print 'recv3:', recv3
# Fill in end
# Send DATA command and print server response.
# Fill in start
dataCommand = "DATA\r\n"
clientSocket.send(dataCommand)
recv4 = clientSocket.recv(1024)
print 'recv4:', recv4
# Fill in end
# Send message data.
# Fill in start
clientSocket.send(msg)
# Fill in end
# Message ends with a single period.
# Fill in start
clientSocket.send(endmsg)
# Fill in end
# Send QUIT command and get server response.
# Fill in start
quitCommand = "QUIT\r\n"
clientSocket.send(quitCommand)
recv5 = clientSocket.recv(1024)
print 'recv5:', recv5
# Fill in end
clientSocket.close()

注意如果邮件找不到,可能归类到垃圾邮件了。一个解决办法是把邮件内容数据(msg)加密后再发送。
ref:《计算机网络:自顶向下方法》第6版 第二章 套接字编程作业3 邮件客户

目录
相关文章
|
3天前
|
Python
python socket 编程实现猜数字
python socket 编程实现猜数字
16 0
|
13天前
|
网络协议 Python
python socket 阻塞
python socket 阻塞
15 0
|
14天前
|
网络协议 Python
python高级-socket和web相关(上)
python高级-socket和web相关
23 0
|
14天前
|
Python
python高级-socket和web相关(下)
python高级-socket和web相关(上)
27 0
|
1月前
|
网络协议 Python
140 python网络编程 - socket简介
140 python网络编程 - socket简介
8 0
|
4月前
|
网络协议 Python
【从零学习python 】72. 深入理解Socket通信及创建套接字的方法
【从零学习python 】72. 深入理解Socket通信及创建套接字的方法
27 0
|
4月前
|
网络协议 网络安全
python-- socket 粘包、实现 ssh
python-- socket 粘包、实现 ssh
|
4月前
|
设计模式 网络协议 Unix
python-- socket介绍
python-- socket介绍
|
5月前
|
JSON 数据可视化 定位技术
python数据可视化开发(3):使用psutil和socket模块获取电脑系统信息(Mac地址、IP地址、主机名、系统用户、硬盘、CPU、内存、网络)
python数据可视化开发(3):使用psutil和socket模块获取电脑系统信息(Mac地址、IP地址、主机名、系统用户、硬盘、CPU、内存、网络)
102 0
|
6月前
|
存储 Python
【python】基于Socket的聊天室Python开发
【python】基于Socket的聊天室Python开发
相关产品
云迁移中心
推荐文章
更多