开发者社区> 问答> 正文

如何在阿里云esc服务器,通过python使用smtp发送邮件?求助

环境:python3.7

import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart from email.mime.application import MIMEApplication import sys

def sendEmail(title, text, send, to, passwd, smtp_server, file): msg = MIMEMultipart() msg['From'] = send msg['To'] = to msg['Subject'] = title strstr=text
att = MIMEText(strstr,'plain','utf-8') msg.attach(att) att = MIMEApplication(open(file,'rb').read())
att.add_header('Content-Disposition', 'attachment', filename=file) msg.attach(att) server = smtplib.SMTP() server.connect(smtp_server) #连接smtp邮件服务器 server = smtplib.SMTP_SSL(smtp_server, 465) server.login(send, passwd) #登录smtp邮件服务器 server.sendmail(send, to, msg.as_string()) #发送 server.close() #关闭

if name == 'main': today = '2020-09-14' smtp_server = 'smtp.qq.com' from_mail = '@qq.com' mail_pass = '**'
to_mail = '
*@qq.com' title = '****{}'.format(today)
text = '
-{}'.format(today) file = '{}_daily_data.csv'.format(today) sendEmail(title=title, text=text, send=from_mail, to=to_mail, passwd=mail_pass, smtp_server=smtp_server, file=file)

代码如上,465无法发送,代码报错,显示连接超时,问了阿里技术人员,说465和587都是可以正常连接的,但最后超时了。

关闭了防火墙,也添加了安全组,邮箱smtp服务已经打开。

展开
收起
游客h46qjxcr2bsco 2020-09-14 15:57:43 1808 0
1 条回答
写回答
取消 提交回答
  • 我用163的邮箱是正常的,腾讯的邮箱我当时测的也是有问题放弃了。

    2020-09-14 18:16:20
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
From Python Scikit-Learn to Sc 立即下载
Data Pre-Processing in Python: 立即下载
双剑合璧-Python和大数据计算平台的结合 立即下载