用socket发送匿名邮件之python实现
发送邮件可以用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 邮件客户
用socket发送匿名邮件之python实现的更多相关文章
- Windows命令实现匿名邮件发送
在日常工具开发中,常常会有发送邮件的需求.在一些高级语言中,如Python.C#中,都有专门的邮件发送模块,如Python 中的 smtplib 模块.那么.一封邮件究竟是怎样发送到一个特定的邮箱呢? ...
- python socket发送魔法包网络唤醒开机.py
python socket发送魔法包网络唤醒开机.py 现在的电脑应该都普遍支持有线网络的WOL了,支持无线网络唤醒的电脑,可能比较少. """ python socke ...
- python 发送安全邮件
用python 写了一个发送邮件的脚本,配上host 和端口,发现一直报错: smtplib.SMTPException: No suitable authentication method foun ...
- 使用python发送QQ邮件
这里用到了Python的两个包来发送邮件: smtplib 和 email . Python 的 email 模块里包含了许多实用的邮件格式设置函数,可以用来创建邮件“包裹”.使用的 MIMEText ...
- 九、Python发送QQ邮件(SMTP)
看了廖雪峰老师的教程: 一封电子邮件的旅程就是 发件人 -> MUA -> MTA -> MTA -> 若干个MTA -> MDA <- MUA <- 收件人 ...
- 【python】脚本连续发送QQ邮件
今天习得用python写一个连续发送QQ邮件的脚本,经过测试,成功给国内外的服务器发送邮件,包括QQ邮箱.163邮箱.google邮箱,香港科技大学的邮箱和爱丁堡大学的邮箱.一下逐步解答相关技巧. 首 ...
- Python通过yagmail和smtplib模块发送简单邮件
SMTP是发送邮件的协议,Python内置对SMTP的支持,可以发送纯文本邮件.HTML邮件以及带附件的邮件.python发邮件需要掌握两个模块的用法,smtplib和email,这俩模块是pytho ...
- 【Python开发】python发送各类邮件的方法
转载: python发送各类邮件的主要方法 python中email模块使得处理邮件变得比较简单,今天着重学习了一下发送邮件的具体做法,这里写写自己的的心得,也请高手给些指点. 一.相关模块介绍 发送 ...
- python实现发送文本邮件
简单实现了python发送文本邮件 #!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2018/4/25 17:09 # @Author ...
随机推荐
- Linux内核设计与实现第六周读书笔记
第三章 进程管理 3.1 进程 进程是处于执行期的代码.通常进程还要包含其他资源,像打开的文件.挂起的信号.内核的内部数据.处理器状态.一个或多个具有内存映射的内存地址空间及一个或多个执行线程,当然还 ...
- 解题:POI 2015 Pieczęć
题面 发现好像没有什么好做法,那就模拟么=.= 以印章左上角的'x'为基准,记录印章上'x'的相对位置模拟.记录相对位置是因为可能有这种情况↓ 直接模拟是会漏掉的=.= #include<cst ...
- 《剑指offer》— JavaScript(10)矩形覆盖
矩形覆盖 题目描述 我们可以用(2*1)的小矩形横着或者竖着去覆盖更大的矩形.请问用n个(2*1)的小矩形无重叠地覆盖一个(2*n)的大矩形,总共有多少种方法? 实现代码 function jumpF ...
- Overlaying GPS Coordinates for Camera Crosshairs
Hey Guys! I am working on a project to allow us to implement GPS coordinates for the location of the ...
- The 14th Zhejiang Provincial Collegiate Programming Contest Sponsored by TuSimple - C 暴力 STL
What Kind of Friends Are You? Time Limit: 1 Second Memory Limit: 65536 KB Japari Park is a larg ...
- bzoj1467 Pku3243 clever Y
1467: Pku3243 clever Y Time Limit: 4 Sec Memory Limit: 64 MBSubmit: 313 Solved: 181[Submit][Status ...
- isnotblank与isnotempty的区别
- C++实现人员信息管理系统模拟
利用C++语言实现基本的学生信息管理系统: 要求: 1-设置管理员密码 2-人员数据有:姓名,性别等基本的信息 3-可以添加,删除,保存,统计 #include<iostream> #in ...
- tp 中 where条件,字段和字段的大小比较
$map = array( , 'start_time' => array('lt',$now), 'end_time' => array('gt',$now), , '_string' ...
- socket--接受大数据
一.简单ssh功能 1.1 实现功能 在前面的一篇博客中,我们已经实现了一个简单的类似Linux服务器ssh功能的小程序,可以输入系统命令来返回命令运行结果,今天我们也以此开始,看看socket如何来 ...