首先我们了解到python代理登录QQ邮箱发送邮件,是需要更改自己QQ邮箱的设置的。即邮箱开启SMTP功能,获得授权码。(设置QQ邮箱授权码可以参考一下网址:[http://service.mail.qq.com/cgi-bin/help?subtype=1&&id=28&&no=1001256])
这是一个QQ邮箱的帮助中心,有问题可以在其中搜索
1.发送纯文本到指定邮件,上代码
# 导入需要的模块
from email.mime.text import MIMEText
from email.header import Header
from smtplib import SMTP_SSL
def run():
# QQ邮箱smtp服务器
host_server = "smtp.qq.com"
# sender_qq为发件人的额qq号码
sender_qq = "594616387"
# pwd为QQ邮箱的授权码
pwd = "rflsszleaojtbajc"
# 发件人邮箱
sender_mail = "594616387@qq.com"
# 收件人邮箱
receiver_mail = "1107773183@qq.com"
# 邮件的正文内容
mail_content = "你好,这里是使用python登录QQ邮箱发送邮件的测试"
# 邮件的标题
mail_title = "高加林的邮箱"
try:
# ssl登录
smtp = SMTP_SSL(host_server)
# set_debuglevel()是用来调试的,参数值为1表示开启调试模式,0为关闭调试模式
smtp.set_debuglevel(1)
smtp.ehlo(host_server)
smtp.login(sender_qq,pwd)
# 定义邮件内容
msg = MIMEText(mail_content,"plain","utf-8")
msg["Subject"] = Header(mail_title,"utf-8")
msg["From"] = sender_mail
msg["To"] = receiver_mail
# 发送邮件
smtp.sendmail(sender_mail,receiver_mail,msg.as_string())
smtp.quit()
print("邮件发送成功")
except Exception as e:
print("邮件发送失败")
print(e)
if __name__ == '__main__':
run()
网友评论