发送邮件可以用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实现的更多相关文章

  1. Windows命令实现匿名邮件发送

    在日常工具开发中,常常会有发送邮件的需求.在一些高级语言中,如Python.C#中,都有专门的邮件发送模块,如Python 中的 smtplib 模块.那么.一封邮件究竟是怎样发送到一个特定的邮箱呢? ...

  2. python socket发送魔法包网络唤醒开机.py

    python socket发送魔法包网络唤醒开机.py 现在的电脑应该都普遍支持有线网络的WOL了,支持无线网络唤醒的电脑,可能比较少. """ python socke ...

  3. python 发送安全邮件

    用python 写了一个发送邮件的脚本,配上host 和端口,发现一直报错: smtplib.SMTPException: No suitable authentication method foun ...

  4. 使用python发送QQ邮件

    这里用到了Python的两个包来发送邮件: smtplib 和 email . Python 的 email 模块里包含了许多实用的邮件格式设置函数,可以用来创建邮件“包裹”.使用的 MIMEText ...

  5. 九、Python发送QQ邮件(SMTP)

    看了廖雪峰老师的教程: 一封电子邮件的旅程就是 发件人 -> MUA -> MTA -> MTA -> 若干个MTA -> MDA <- MUA <- 收件人 ...

  6. 【python】脚本连续发送QQ邮件

    今天习得用python写一个连续发送QQ邮件的脚本,经过测试,成功给国内外的服务器发送邮件,包括QQ邮箱.163邮箱.google邮箱,香港科技大学的邮箱和爱丁堡大学的邮箱.一下逐步解答相关技巧. 首 ...

  7. Python通过yagmail和smtplib模块发送简单邮件

    SMTP是发送邮件的协议,Python内置对SMTP的支持,可以发送纯文本邮件.HTML邮件以及带附件的邮件.python发邮件需要掌握两个模块的用法,smtplib和email,这俩模块是pytho ...

  8. 【Python开发】python发送各类邮件的方法

    转载: python发送各类邮件的主要方法 python中email模块使得处理邮件变得比较简单,今天着重学习了一下发送邮件的具体做法,这里写写自己的的心得,也请高手给些指点. 一.相关模块介绍 发送 ...

  9. python实现发送文本邮件

    简单实现了python发送文本邮件 #!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2018/4/25 17:09 # @Author ...

随机推荐

  1. IPC$入侵大全

    0x01  ipc$的定义 IPC$(Internet Process Connection)是共享"命名管道"的资源(大家都是这么说的),它是为了让进程间通信而开放的命名管道,可 ...

  2. 修改apache的注册表值提高系统权限

    In Windows environments when a service is registered with the system a new key is created in the reg ...

  3. 解题:POI 2015 Kinoman

    题面 发现每种电影只在两场之间产生贡献(只有$pos$的一场的就在$[pos,n]$产生贡献).那么我们针对每个位置$i$求出这场电影下一次出现的位置$nxt[i]$,然后每次更新一下,求整个区间的最 ...

  4. 【bzoj1502】月下柠檬树

    Portal -->bzoj1502 Solution 额其实说实在这题我一开始卡在了..这个阴影长啥样上QwQ 首先因为是平行光线然后投影到了一个水平面上所以这个投影一定是..若干个圆再加上这 ...

  5. joomla! 3.X 开发系列教程

    http://www.mengyunzhi.com/members-resource/joomla/87-joomla-menu-study.html 学习地址 http://blog.csdn.ne ...

  6. Codeforces Round #338 (Div. 2) B dp

    B. Longtail Hedgehog time limit per test 3 seconds memory limit per test 256 megabytes input standar ...

  7. 简单shell 编程

    简单shell编程  by  dreamboy #!/bin/bash while true do echo clear echo echo " 系统维护菜单 " echo &qu ...

  8. python学习(十六)写爬虫爬取糗事百科段子

    原文链接:爬取糗事百科段子 利用前面学到的文件.正则表达式.urllib的知识,综合运用,爬取糗事百科的段子先用urllib库获取糗事百科热帖第一页的数据.并打开文件进行保存,正好可以熟悉一下之前学过 ...

  9. [LeetCode] 29. Divide Two Integers ☆☆

    Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...

  10. Android Studio Gradle's dependency cache may be corrupt Re-download dependencies and sync project (requires network)

    转:Android studio 快速解决Gradle's dependency cache may be corrupt 和 Gradle配置 gradle-3.*-all.zip快速下载 Andr ...