import smtplib from email.mime.text import MIMEText from email.header import Header # 第三方 SMTP 服务 mail_host = "smtp.163.com" # 设置服务器 mail_user = "AAAAAA@163.com" # 发邮件的账户名 mail_pass = "******" # 授权码 sender = 'AAAAAA@163.com' receivers = ['BBBBBB@qq.com'] # 接收邮件,可设置为你的QQ邮箱或者其他邮箱 # 三个参数:第一个为文本内容,第二个设置格式,plain:文本,html:HTML格式,第三个 utf-8 设置编码 message = MIMEText('本次邮件的内容', 'plain', 'utf-8') message['From'] = Header("AAAAAA@163.com") # 邮件中的发件人 message['To'] = Header("BBBBBB@qq.com") # 邮件中的收件人 subject = '邮件主题' message['Subject'] = Header(subject, 'utf-8') try: smtpObj = smtplib.SMTP() smtpObj.connect(mail_host, 25) # 25 为 SMTP 端口号 smtpObj.login(mail_user, mail_pass) # 发件人邮箱账号、收件人邮箱账号、发送邮件 smtpObj.sendmail(sender, receivers, message.as_string()) print("邮件发送成功") except smtplib.SMTPException: print("Error: 无法发送邮件")
SMTP 服务器
授权码
参考
https://www.runoob.com/python/python-email.html
解决 554 DT:SPM 问题
https://www.jianshu.com/p/d487bd784ed0
来源:https://www.cnblogs.com/jiyu-hlzy/p/12270893.html