首页 > 编程语言 > 详细

Python 3.8发送邮件

时间:2020-03-01 20:49:15      阅读:176      评论:0      收藏:0      [点我收藏+]
import smtplib from email.mime.text import MIMEText from email.utils import formataddr my_sender = ‘916551516@qq.com‘ # 发件人邮箱账号 my_pass = ‘xxxxxxxxxxxxxxxxxxxx‘ # 发件人的邮箱授权码 my_user = ‘lv916551516@163.com‘ # 收件人的邮箱号 def mail(): ret = True try: msg = MIMEText(‘测试邮件内容_____当你收到这份邮件,说明我使用python发送邮件成功了‘, ‘plain‘, ‘utf-8‘) msg[‘From‘] = formataddr(["吕建钊", my_sender]) # 括号里对应的分别是邮箱昵称、发件人邮箱账号 msg[‘To‘] = formataddr(["张三", my_user]) # 括号里的对应收件人邮箱昵称、收件人邮箱账 msg[‘Subject‘] = "Python发送邮件测试。" # 邮件的主题,也可以说是标题 server = smtplib.SMTP_SSL("smtp.qq.com", 465) # 发件人邮箱中的SMTP服务器,SSL端口是465 server.login(my_sender, my_pass) # 括号中对应的是发件人邮箱账号、邮箱密码 server.sendmail(my_sender, [my_user, ], msg.as_string()) # 括号中对应的是发件人邮箱账号、收件人邮箱账号、发送邮件 server.quit() # 关闭连接 except Exception: # 如果 try 中的语句没有执行成功,则会执行下面的 ret=False ret = False return ret ret = mail() if ret: print("邮件发送成功!!!") else: print("邮件发送失败!!!")

脚本执行后,收件人收到如下邮件:

技术分享图片

Python 3.8发送邮件

原文:https://blog.51cto.com/14154700/2474700

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!