python发送邮件 python实现QQ邮箱发送邮件

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

python发送邮件 python实现QQ邮箱发送邮件

mumuqingwei   2021-04-21 我要评论

1、代码:

#!/usr/bin/python
# -*- coding: utf-8 -*-
import smtplib
from email.mime.text import MIMEText
from email.header import Header
 
mail_host="smtp.qq.com"#设置的邮件服务器host必须是发送邮箱的服务器,与接收邮箱无关。
mail_user="**************"#qq邮箱登陆名
mail_pass="*****************" #开启stmp服务的时候并设置的授权码,注意!不是QQ密码。
 
sender='xxx@qq.com'#发送方qq邮箱
receivers=['xxx@qq.com']#接收方qq邮箱
 
message=MIMEText('测试发送 python 邮件','plain','utf-8')
message['From']=Header("beibei",'utf-8') #设置显示在邮件里的发件人
message['To']=Header("wowo",'utf-8') #设置显示在邮件里的收件人
 
subject ='python smtp email test'
message['Subject']=Header(subject,'utf-8') #设置主题和格式
 
try:
 smtpobj=smtplib.SMTP_SSL(mail_host,465) #本地如果有本地服务器,则用localhost ,默认端口25,腾讯的(端口465或587)
 smtpobj.set_debuglevel(1)
 smtpobj.login(mail_user,mail_pass)#登陆QQ邮箱服务器
 smtpobj.sendmail(sender,receivers,message.as_string())#发送邮件
 print("邮件发送成功")
 smtpobj.quit()#退出
except smtplib.SMTPException as e :
 print("Error:无法发送邮件")
 print(e)

2、执行结果:

3、遇到问题:

3.1、端口错误 SMTP 默认端口是25 ,但QQ邮箱不是

smtplib.SMTP_SSL(mail_host,465)

要用SMTP_SSL 

3.2、发送被拒绝连接  535, b'Error:  

3.3、获取授权码:

会在此处显示授权码,记下来。

猜您喜欢

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们