转载

Python发邮件程序部署到Linux云服务器上之后无法获取邮件服务器的连接

import smtplib
import traceback
from email.mime.text import MIMEText
from email.header import Header

sender = '' # 发送者邮箱
receiver = '' # 接收者邮箱
subject = '放假通知' # 邮件主题
smtpserver = "smtp.163.com" # smtp服务器
username = '' #发送者邮箱账号
password = '' #发送者邮箱密码

try:
msg = MIMEText('大家关好窗户', 'plain', 'utf-8') # 中文需参数‘utf-8',单字节字符不需要
msg['Subject'] = Header(subject, 'utf-8')
msg['From'] = sender
msg['To'] = receiver
smtp = smtplib.SMTP()
smtp.connect(smtpserver)
smtp.login(username, password)
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit()
except smtplib.SMTPException:
traceback.print_exc()
print("Error: 无法发送邮件")

这段代码在本地测试都没有问题,只要部署到云服务器上就无法获取连接,最后发现有人连接的时候用了SSL,获取邮件服务器连接时需要改成smtplib.SMTP_SSL()

转载于:https://www.cnblogs.com/dmfcjd/p/11534090.html

正文到此结束
本文目录