标签:deb email login qq邮箱 str import net print att
import smtplib
from smtplib import SMTP_SSL
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header
host_server = ‘smtp.qq.com‘ #qq邮箱smtp服务器
sender_qq = ‘zzzzxx@qq.com‘ # sender_sina为发件人的邮箱
pwd = ‘xxxxxxxxxxxxxxxx‘#‘# pwd为邮箱的授权码密码
sender_qq_mail = ‘xxx@qq.com‘ # 发件人的邮箱
receiver = [‘xxx_1@126.com‘,‘xxx_2@yeah.net‘,‘xxxx_3@163.com‘] # 收件人的邮箱
# receiver = [‘1@123.com’,’2@123.com’,’3@123.com’] # 给多人发送邮件
mail_title = ‘python自动化的测试邮件‘ #邮件的标题
mail_content = ‘ <html><body><h2>您好,</h2><p>这是一封使用python自动发出的第二份测试邮件</p></body></html> ‘ #邮件的正文内容
# ‘<html><body><h2>您好</h2>,<p>这是一封使用python登录QQ邮箱发送文本邮件的测试</p></body></html>’
msg = MIMEMultipart()# 邮件的主体
msg[‘Subject‘] = Header(mail_title,‘utf-8‘)
msg[‘From‘] = sender_qq_mail
msg[‘To‘] = Header(‘测试邮箱‘,‘utf-8‘)
msg.attach(MIMEText(mail_content,‘html‘,‘utf-8‘)) # 邮件正文内容
try:
smtp = SMTP_SSL(host_server)
smtp.set_debuglevel(1)
smtp.ehlo(host_server)
# smtp = SMTP_SSL(host_server) # ssl登录
smtp.login(sender_qq,pwd)
smtp.sendmail(sender_qq_mail,receiver,msg.as_string())
smtp.quit()
print(‘邮件发送成功‘)
except smtplib.SMTPException:
print(‘无法发送邮件‘)
标签:deb email login qq邮箱 str import net print att
原文地址:https://www.cnblogs.com/tomhu/p/12343091.html