博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python SMTP发送邮件
阅读量:5242 次
发布时间:2019-06-14

本文共 2842 字,大约阅读时间需要 9 分钟。

转载 菜鸟教程

1. 不带附件的 邮件:

#!/usr/bin/python# -*- coding: UTF-8 -*-import smtplibfrom email.mime.text import MIMETextfrom email.header import Headersender = 'zhoux.tian@xxx.com'receivers = ['zhoux.tian@xxx.com','zhoux.tian@xxx.com']  # 接收邮件,可设置为你的QQ邮箱或者其他邮箱# 三个参数:第一个为文本内容,第二个 plain 设置文本格式,第三个 utf-8 设置编码message = MIMEText('Python 邮件发送测试...', 'plain', 'utf-8')message['From'] = Header("菜鸟教程", 'utf-8')  # 发送者message['To'] = Header("测试", 'utf-8')  # 接收者#message['From'] = sender#message['To'] = ','.join(receivers)subject = 'Python SMTP 邮件测试'message['Subject'] = Header(subject, 'utf-8')try:    smtpObj = smtplib.SMTP('smtp.xxx.com')    smtpObj.sendmail(sender, receivers, message.as_string())    print "邮件发送成功"except smtplib.SMTPException:    print "Error: 无法发送邮件"

2. 如果我们本机没有 sendmail 访问,也可以使用其他邮件服务商的 SMTP 访问(QQ、网易、Google等):

#!/usr/bin/python# -*- coding: UTF-8 -*- import smtplibfrom email.mime.text import MIMETextfrom email.header import Header # 第三方 SMTP 服务mail_host="smtp.XXX.com"  #设置服务器mail_user="XXXX"    #用户名mail_pass="XXXXXX"   #口令   sender = 'from@runoob.com'receivers = ['429240967@qq.com']  # 接收邮件,可设置为你的QQ邮箱或者其他邮箱 message = MIMEText('Python 邮件发送测试...', 'plain', 'utf-8')message['From'] = Header("菜鸟教程", 'utf-8')message['To'] =  Header("测试", 'utf-8') subject = 'Python SMTP 邮件测试'message['Subject'] = Header(subject, 'utf-8')  try:    smtpObj = smtplib.SMTP()     smtpObj.connect(mail_host, 25)    # 25 为 SMTP 端口号    smtpObj.login(mail_user,mail_pass)      smtpObj.sendmail(sender, receivers, message.as_string())    print "邮件发送成功"except smtplib.SMTPException:    print "Error: 无法发送邮件"

3. 带附件:

#!/usr/bin/python# -*- coding: UTF-8 -*- import smtplibfrom email.mime.text import MIMETextfrom email.mime.multipart import MIMEMultipartfrom email.header import Header sender = 'from@runoob.com'receivers = ['429240967@qq.com']  # 接收邮件,可设置为你的QQ邮箱或者其他邮箱 #创建一个带附件的实例message = MIMEMultipart()message['From'] = Header("菜鸟教程", 'utf-8')message['To'] =  Header("测试", 'utf-8')subject = 'Python SMTP 邮件测试'message['Subject'] = Header(subject, 'utf-8') #邮件正文内容message.attach(MIMEText('这是菜鸟教程Python 邮件发送测试……', 'plain', 'utf-8')) # 构造附件1,传送当前目录下的 test.txt 文件att1 = MIMEText(open('test.txt', 'rb').read(), 'base64', 'utf-8')att1["Content-Type"] = 'application/octet-stream'# 这里的filename可以任意写,写什么名字,邮件中显示什么名字att1["Content-Disposition"] = 'attachment; filename="test.txt"'message.attach(att1) # 构造附件2,传送当前目录下的 runoob.txt 文件att2 = MIMEText(open('runoob.txt', 'rb').read(), 'base64', 'utf-8')att2["Content-Type"] = 'application/octet-stream'att2["Content-Disposition"] = 'attachment; filename="runoob.txt"'message.attach(att2) try:    smtpObj = smtplib.SMTP('localhost')    smtpObj.sendmail(sender, receivers, message.as_string())    print "邮件发送成功"except smtplib.SMTPException:    print "Error: 无法发送邮件"

 

转载于:https://www.cnblogs.com/nevermore29/p/10716890.html

你可能感兴趣的文章
[HDOJ5500]Reorder the Books
查看>>
RabbitMQ各种交换机类型Exchange Types介绍
查看>>
Unhandled event loop exception No more handles
查看>>
JAVA基础补漏---数组
查看>>
关于字节对齐的理解
查看>>
处理跨域的方式
查看>>
sqlplus中break命令的使用
查看>>
由于SVN导致桌面图标都带有?标记
查看>>
C++容器在遍历时的删除问题
查看>>
C#编程语言与面向对象——类与对象
查看>>
Openstack: change endpoint IP addresses after installation
查看>>
仿新浪微博的ListView下拉更新功能
查看>>
胆小勿入!!万圣节特效
查看>>
linux find 命令
查看>>
ios fixed定位问题
查看>>
Entity Framework 4.1 Code-First 学习笔记
查看>>
Android开发——Fragment知识整理(一)
查看>>
一些奇思幻想
查看>>
半分钟内能看透问题本质的人是如何思考的?
查看>>
EL表达式和JSTL
查看>>