使用smtplib模块发送邮件,它对smtp协议进行了简单的封装。

smtp协议的基本命令包括:

HELO 向服务器标识用户身份

MAIL 初始化邮件传输 mail from:

RCPT 标识单个的邮件接收人;常在MAIL命令后面,可有多个rcpt to:

DATA 在单个或多个RCPT命令后,表示所有的邮件接收人已标识,并初始化数据传输,以.结束

VRFY 用于验证指定的用户/邮箱是否存在;由于安全方面的原因,服务器常禁止此命令

EXPN 验证给定的邮箱列表是否存在,扩充邮箱列表,也常被禁用

HELP 查询服务器支持什么命令

NOOP 无操作,服务器应响应OK

QUIT 结束会话

RSET 重置会话,当前传输被取消

MAIL FROM 指定发送者地址

RCPT TO 指明的接收者地址

一般smtp会话有两种方式,一种是邮件直接投递,就是说,比如你要发邮件給zzz@163.com,那就直接连接163.com的邮件服务器,把信投給zzz@163.com; 另一种是验证过后的发信,它的过程是,比如你要发邮件給zzz@163.com,你不是直接投到163.com,而是通过自己在sina.com的另一个邮箱来发。这样就要先连接sina.com的smtp服务器,然后认证,之后在把要发到163.com的信件投到sina.com上,sina.com会帮你把信投递到163.com。

文件形式的邮件

#!/usr/bin/env python3

#coding: utf-8

import smtplib

from email.mime.text import MIMEText

from email.header import Header

sender = '***'

receiver = '***'

subject = 'python email test'

smtpserver = 'smtp.163.com'

username = '***'

password = '***'

msg = MIMEText('你好','text','utf-8')#中文需参数‘utf-8’,单字节字符不需要

msg['Subject'] = Header(subject, 'utf-8')

smtp = smtplib.SMTP()

smtp.connect('smtp.163.com')

smtp.login(username, password)

smtp.sendmail(sender, receiver, msg.as_string())

smtp.quit()

html形式的邮件

#!/usr/bin/env python3

#coding: utf-8

import smtplib

from email.mime.text import MIMEText

sender = '***'

receiver = '***'

subject = 'python email test'

smtpserver = 'smtp.163.com'

username = '***'

password = '***'

msg = MIMEText('

你好

','html','utf-8')

msg['Subject'] = subject

smtp = smtplib.SMTP()

smtp.connect('smtp.163.com')

smtp.login(username, password)

smtp.sendmail(sender, receiver, msg.as_string())

smtp.quit()

带图片的html形式邮件

#!/usr/bin/env python3

#coding: utf-8

import smtplib

from email.mime.multipart import MIMEMultipart

from email.mime.text import MIMEText

from email.mime.image import MIMEImage

sender = '***'

receiver = '***'

subject = 'python email test'

smtpserver = 'smtp.163.com'

username = '***'

password = '***'

msgRoot = MIMEMultipart('related')

msgRoot['Subject'] = 'test message'

msgText = MIMEText('Some HTML text and an image.
good!','html','utf-8')

msgRoot.attach(msgText)

fp = open('h:\\python\\1.jpg', 'rb')

msgImage = MIMEImage(fp.read())

fp.close()

msgImage.add_header('Content-ID', '')

msgRoot.attach(msgImage)

smtp = smtplib.SMTP()

smtp.connect('smtp.163.com')

smtp.login(username, password)

smtp.sendmail(sender, receiver, msgRoot.as_string())

smtp.quit()

带附件的邮件

#!/usr/bin/env python3

#coding: utf-8

import smtplib

from email.mime.multipart import MIMEMultipart

from email.mime.text import MIMEText

from email.mime.image import MIMEImage

sender = '***'

receiver = '***'

subject = 'python email test'

smtpserver = 'smtp.163.com'

username = '***'

password = '***'

msgRoot = MIMEMultipart('related')

msgRoot['Subject'] = 'test message'

#构造邮件中的附件

att = MIMEText(open('h:\\python\\1.jpg', 'rb').read(), 'base64', 'utf-8')

att["Content-Type"] = 'application/octet-stream'

att["Content-Disposition"] = 'attachment; filename="1.jpg"'

msgRoot.attach(att)

smtp = smtplib.SMTP()

smtp.connect('smtp.163.com')

smtp.login(username, password)

smtp.sendmail(sender, receiver, msgRoot.as_string())

smtp.quit()

群邮件

#!/usr/bin/env python3

#coding: utf-8

import smtplib

from email.mime.text import MIMEText

sender = '***'

receiver = ['***','****',……]

subject = 'python email test'

smtpserver = 'smtp.163.com'

username = '***'

password = '***'

msg = MIMEText('你好','text','utf-8')

msg['Subject'] = subject

smtp = smtplib.SMTP()

smtp.connect('smtp.163.com')

smtp.login(username, password)

smtp.sendmail(sender, receiver, msg.as_string())

smtp.quit()

也可使用yagmail模块来快速实现

实现代码如下:

import yagmail

yag = yagmail.SMTP(user='XXX@gmail.com',password='XXX'

yag.send(to = 'XXX@qq.com',subject ='test',contents = 'This is a test e-mail from Windows CMD tools with the yagmai')

python邮件发送_Python实现邮件发送相关推荐

  1. python邮件模块_Python收发邮件模块,用,来,发送,接收

    用Python来发送接收邮件模块 python实现发送和接收邮件功能主要用到poplib和smtplib模块. poplib用于接收邮件,而smtplib负责发送邮件. # -- coding :ut ...

  2. python smtplib模块_python实现邮件接口——smtplib模块

    1. 思路 使用脚本发送邮件的思路其实和客户端发送邮件一样,过程都是: 登录 -> 写邮件 -> 发送 只不过通过脚本发送时我们需要考虑到整个过程的方方面面.以下为思路导图: 2. Pyt ...

  3. python获取邮件内容_python 接收邮件获取邮件内容

    收取邮件有两种协议,POP3和IMAP,POP3相对于IMAP功能较少无法对邮件进行更深层次的操作,因此本文使用IMAP协议收取邮件.python提供了很多收邮件的模块,本文使用imaplib来接收邮 ...

  4. python爬取邮件内容_python 接收邮件获取邮件内容

    收取邮件有两种协议,POP3和IMAP,POP3相对于IMAP功能较少无法对邮件进行更深层次的操作,因此本文使用IMAP协议收取邮件.python提供了很多收邮件的模块,本文使用imaplib来接收邮 ...

  5. python发邮件实例_python 发邮件实例

    1 importsmtplib2 from email.mime.multipart importMIMEMultipart3 from email.mime.text importMIMEText4 ...

  6. python发送邮件群发_Python发邮件脚本,Python调用163邮箱SMTP服务实现邮件群发

    邮件营销,一个昔日辉煌,如今没落的广告营销方式,曾经的恶意广告邮件群发,到现在还存留着的大站协议群发,可能还是有不少人能够做到正常群发邮件,但大概率很多人都不会点开邮件查看邮件内容吧,除非是很许可的, ...

  7. python 邮件解析_Python解析邮件

    邮件的解析是个大课题,远超一般人的预期.它远比发送邮件和接收邮件要复杂的多的多. 这就是为什么网上中文外文搜邮件的问题,绝大多数都是讲发送的而讲接收的很少. 发送邮件好说,接收和下载邮件也好说.关键是 ...

  8. python编写赛车_python udp 协议发送接收秒速赛车平台搭建数据

    python udp 协议发送接收秒速赛车平台搭建数据 Socket socket 简称套接字, 秒速赛车平台搭建 [企鹅 217-1793-408] 是进程间通信的一种方式. 与其他的方式的进程间的 ...

  9. python发邮件实例_python发邮件实例

    文件形式的邮件 #!/usr/bin/env python3 #coding: utf-8 import smtplib from email.mime.text import MIMEText fr ...

最新文章

  1. iOS架构-制作静态库.a和.framework 的相互依赖(10)
  2. directx 9.27.1734 多语言完整版 下载
  3. 部署虚拟服务器,把网站部署到虚拟服务器
  4. VTK:网格之ClipClosedSurface
  5. iphone3G恢复到3.1.2遇到的问题
  6. ffmpeg和SDL学习笔记
  7. c语言三个杠的等号是什么,数学3个横杠的等号表示什么意思?比如这个定 – 手机爱问...
  8. 谷歌:中国版搜索引擎之心不死!
  9. [leetcode] 140. 单词拆分 II
  10. 安卓开发之刮刮乐实例教程
  11. Linux CentOS修改网卡IP/网关设置
  12. 一文看雷击浪涌的防护解析
  13. 香港低价linux虚拟主机,
  14. 鬼谷八荒逆天改命存档
  15. 十个Python图像处理工具,不可不知!
  16. UUIDUtil获取八位UUID
  17. datawhale学习小组 Task4:方差分析
  18. linux 查看文件创建时间
  19. 推荐系统4--AutoRec与Deep Crossing(改变神经网络的复杂程度)
  20. 2019蓝桥杯B组省赛反思总结

热门文章

  1. 【nginx】nginx 负载均衡
  2. 【Kafka】kafka Java api 获取 kafka topic 或者 partition 占用的磁盘大小
  3. 【Http】Apache HttpClient 4.5实现https
  4. Spring : 异步注解 @EnableAsync 和 @Async
  5. 13-Java和Scala中的Future
  6. ajaxutil java,Ajax的工具类AjaxUtils,使用struts返回Json类型
  7. 趋势探讨:容器会取代虚拟机吗?
  8. Flask使用ajax进行前后端交互
  9. Java多线程学习十:线程池实现“线程复用”的原理
  10. 隐藏java_Java方法隐藏