1.python邮件发送代码参考
前提条件:开启IMAP/SMTP服务,会生成在第三方客户端登录时授权密码
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import smtplib
from email.mime.text import MIMEText
from email.header import Header
# 第三方 SMTP 服务
mail_host = 'smtp.163.com' #设置服务器
mail_user = '189955XXX79@163.com' #用户名
#mail_pass="jhunsgywwzbsjdfg" #口令,
mail_pass = 'XXXXXXXXX'
sender = '189955XXX79@163.com'
receivers = ['15XXX07551@qq.com'] # 接收邮件,可设置为你的QQ邮箱或者其他邮箱
message = MIMEText('您好,收到请回复!', 'plain', 'utf-8') #邮件正文内容,plain纯文本格式,后面参数编码类型
message['From'] = Header('189955XXX79@163.com') #发送者
message['To'] = Header('15XXX07551@qq.com') #接收者
subject = 'Python邮件' #邮件主题
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: 无法发送邮件")
2.邮件优化:添加内容链接,指定内容信息
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import smtplib
from email.mime.text import MIMEText
from email.header import Header
# 第三方 SMTP 服务
mail_host = 'smtp.163.com' #设置服务器
mail_user = '18xxx511479@163.com' #用户名
#mail_pass="jhunsgywwzbsjdfg" #口令 18xxx511479
mail_pass = 'DYYZCQGWVNJAXFFL'
sender = '18xxx511479@163.com'
receivers = ['15xxx07551@qq.com'] # 接收邮件,可设置为你的QQ邮箱或者其他邮箱
mail_msg = """
<p>Python 邮件发送测试...</p>
<p><a href="http://www.runoob.com">这是一个链接</a></p>
"""
message = MIMEText(mail_msg, 'html', 'utf-8') #邮件正文内容,plain纯文本格式,后面参数编码类型
message['From'] = Header('18xxx511479@163.com') #发送者
message['To'] = Header('15xxx07551@qq.com') #接收者
subject = 'Python邮件' #邮件主题
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 smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header
# 第三方 SMTP 服务
mail_host = 'smtp.163.com' #设置服务器
mail_user = '18995XXXX9@163.com' #用户名
#mail_pass="jhunsgywwzbsjdfg" #口令
mail_pass = 'ETQSMKZANAZDMNGV'
sender = '18995XXXX9@163.com'
receivers = ['157xxx7551@qq.com'] # 接收邮件,可设置为你的QQ邮箱或者其他邮箱
#创建一个带附件的实例
message = MIMEMultipart()
message['From'] = Header('18995XXXX9@163.com') #发送者
message['To'] = Header('157xxx7551@qq.com') #接收者
subject = 'Python邮件' #邮件主题
message['Subject'] = Header(subject, 'utf-8') #邮件主题
#邮件正文内容
message.attach(MIMEText('这是菜鸟教程Python 邮件发送测试……', 'plain', 'utf-8'))
# 构造附件1,传送当前目录下的 test.txt 文件
att1 = MIMEText(open('a.xls', 'rb').read(), 'base64', 'utf-8')
att1["Content-Type"] = 'application/octet-stream'
# 这里的filename可以任意写,写什么名字,邮件中显示什么名字
att1["Content-Disposition"] = 'attachment; filename="a.xls"'
message.attach(att1)
# 构造附件1,传送当前目录下的 test.txt 文件
att2 = MIMEText(open('a.txt', 'rb').read(), 'base64', 'utf-8')
att2["Content-Type"] = 'application/octet-stream'
# 这里的filename可以任意写,写什么名字,邮件中显示什么名字
att2["Content-Disposition"] = 'attachment; filename="a.txt"'
message.attach(att2)
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: 无法发送邮件")
4.邮件优化:添加附件内容