# 首先导入模块
import smtplib
from email.mime.text import MIMEText
# 第三方
mail_host = 'smtp.qq.com' # 腾讯smtp服务器
mail_user = 'xx@qq.com' # 需要登录的邮箱账号
mail_pass = 'xx' # 对应邮箱密码也就是授权码,需要开启smtp 并启用授权码,代码后面会有提示
message = MIMEText('内容这是', 'plain', 'utf-8') # 邮件内容
senderOne = 'xx@qq.com' # 发件人邮箱(mail_user = 'xx@qq.com')
receiversOne = ['xx@qq.com', 'xx@qq.com', 'xxcom'] # 接收邮件,可设置为你的QQ邮箱或者其他邮箱
message['From'] = "{}".format(senderOne)
message['To'] = ",".join(receiversOne)
title = '标题这是'
message['Subject'] = title
try:
smtpObj = smtplib.SMTP_SSL(mail_host, 465) # 启用SSL发信, 端口一般是465
smtpObj.login(mail_user, mail_pass) # 登录验证
smtpObj.sendmail(senderOne, receiversOne, message.as_string()) # 发送
print("mail has been send successfully.")
except smtplib.SMTPException as e:
print(e)
- 经过验证不用SSL(端口号25)发送邮件无法发送成功有连接错误或者其他,如果有人能发送成功请留言谢谢,所以用SMTP_SSL发信
- 授权码