import smtplib
from email.mime.text import MIMEText
from email.utils import formataddr
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage
my_sender='xxxxxxxx@qq.com' # 发件人邮箱账号
my_pass = 'xxxxxxxxxxxxx' # 发件人邮箱口令
my_user='xxxxxxxx@qq.com' # 收件人邮箱账号
def mail():
ret=True
try:
msg=MIMEText('内容','plain','utf-8')
msg['From']=formataddr(["棒棒糖",my_sender]) # 发件人邮箱昵称
msg['To']=formataddr(["xxxx",my_user]) # 收件人邮箱昵称
msg['Subject']="Python发邮箱" # 邮件的标题
imageFile = 'a.jpg' #附件路径
#文件二进制存储
imageApart = MIMEImage(open(imageFile, 'rb').read(),imageFile.split('.')[-1])
imageApart.add_header('Content-Disposition', 'attachment', filename=imageFile)
m = MIMEMultipart()
m.attach(msg)
m.attach(imageApart)
server=smtplib.SMTP_SSL("smtp.qq.com", ???) # 发件人的SMTP服务器,端口号是465
server.login(my_sender, my_pass) # 发件人邮箱账号、邮箱口令
server.sendmail(my_sender,[my_user,],m.as_string()) # 发件人邮箱账号、收件人邮箱账号、邮件内容
server.quit()# 关闭连接
except Exception:
ret=False
return ret
ret=mail()
if ret:
print("邮件发送成功")
else:
print("邮件发送失败")
Python发送QQ邮件
最新推荐文章于 2023-10-13 17:07:06 发布