标签:邮件发送 目录 att ase tip chm str aac 主题
python实现发送带附件的邮件
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header
# 第三方 SMTP 服务
mail_host = "smtp.qq.com" # 设置服务器
mail_user = "1986109019@qq.com" # 用户名
mail_pass = "jiluxfebnxpmcacj" # 口令
sender = '1986109019@qq.com' # 发送方
receivers = ['1986109019@qq.com','631813342@qq.com'] # 接收邮件,可设置为你的QQ邮箱或者其他邮箱 可写多个
# 创建一个带附件的实例
message = MIMEMultipart()
message['From'] = Header("我是发件人", 'utf-8') # 发件人
message['To'] = Header("我是收件人", 'utf-8') # 收件人
subject = 'Python发送带附件的邮件示例' #邮件主题
message['Subject'] = Header(subject, 'utf-8')
# 邮件正文内容
send_content = 'hi man,你收到附件了吗?'
content_obj = MIMEText(send_content, 'plain', 'utf-8') # 第一个参数为邮件内容
message.attach(content_obj)
# 构造附件1,发送当前目录下的 t1.txt 文件
att1 = MIMEText(open(r'F:\壁纸\1.jpg', 'rb').read(), 'base64', 'utf-8')
att1["Content-Type"] = 'application/octet-stream'
# 这里的filename可以任意写,写什么名字,邮件附件中显示什么名字
att1["Content-Disposition"] = 'attachment; filename="1.jpg"'
message.attach(att1)
# 构造附件2,发送当前目录下的 t2.py 文件
att2 = MIMEText(open('F:\Python学习\python-lch\Web_crawlers项目\day74爬取汽车之家标题.py', 'rb').read(), 'base64', 'utf-8')
att2["Content-Type"] = 'application/octet-stream'
att2["Content-Disposition"] = 'attachment; filename="day74_aaaacar.py"'
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: 无法发送邮件")
标签:邮件发送 目录 att ase tip chm str aac 主题
原文地址:https://www.cnblogs.com/Nayears/p/12109068.html