900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > python语言实现【使用自带smtp服务的阿里云邮箱 发送邮件】

python语言实现【使用自带smtp服务的阿里云邮箱 发送邮件】

时间:2019-12-06 05:59:26

相关推荐

python语言实现【使用自带smtp服务的阿里云邮箱 发送邮件】

使用163或者qq邮箱的smtp协议,需要自己手动开启,然后手机发送短信,浪费1毛钱不说,还经常自动关闭该协议,烦的一比啊!

21世纪邮箱自带smtp协议无需开启,也无需授权码,使用自己的登录密码即可,缺点就是自己邮箱的密码有泄漏的风险。所以建议申请一个新的邮箱。

实现标题功能代码:

def send_mail(self):""":return:"""log_path = self.log_pathreport_path = self.report_pathmail_host = ""mail_pass = "****" #邮箱登录密码sender = "****@" # 发件人receivers = ['**@', '***@'] # 收件人today = time.strftime('%Y-%m-%d', time.localtime(time.time()))detail_time = time.strftime('%H:%M:%S', time.localtime(time.time()))send_header = "自动化测试报告 " + today + " " + detail_time # 邮件标题msg = MIMEMultipart()msg['Subject'] = send_headermsg['From'] = sendermsg['To'] = ",".join(receivers)# 发送html格式正文content = self.set_value_to_html()msg.attach(MIMEText(content, _subtype='html', _charset='utf-8'))# 发送log.html附件log_file = MIMEApplication(open(log_path, 'rb').read())log_file.add_header('Content-Disposition', 'attachment', filename='log.html')msg.attach(log_file)# 发送report.html附件report_file = MIMEApplication(open(report_path, 'rb').read())report_file.add_header('Content-Disposition', 'attachment', filename='report.html')msg.attach(report_file)try:server = smtplib.SMTP_SSL(mail_host, 465)server.login(sender, mail_pass)server.sendmail(sender, receivers, msg.as_string())server.close()print('邮件发送成功')#return Trueexcept Exception as e:print('邮件发送失败', str(e))# return False

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。