# -*- coding:utf-8 -*- from email.mime.text import MIMEText from email.header import Header import smtplib from email.mime.application import MIMEApplication from email.mime.multipart import MIMEMultipart msg = MIMEMultipart() msg[‘Subject‘] = Header("来自Python的邮件",‘utf-8‘) msg[‘From‘] = Header(‘***@sina.com‘) msg[‘To‘] = Header(‘receiver‘,‘utf-8‘) from_addr = ‘***@sina.com‘ #发件邮箱 password = ‘****‘ #邮箱密码 to_addr = ‘****@163.com‘ #收件邮箱 smtp_server = ‘smtp.sina.com‘ #SMTP服务器 part = MIMEApplication(open(‘result.jtl‘,‘rb‘).read()) part.add_header(‘Content-Disposition‘, ‘attachment‘, filename="result.jtl") msg.attach(part) try: server = smtplib.SMTP(smtp_server,25) #第二个参数为默认端口为25,有些邮件有特殊端口 print(‘开始登录‘) server.login(from_addr,password) #登录邮箱 print(‘登录成功‘) print("邮件开始发送") server.sendmail(from_addr,to_addr,msg.as_string()) #将msg转化成string发出 server.quit() print("邮件发送成功") except smtplib.SMTPException as e: print("邮件发送失败",e)
原文地址:https://www.cnblogs.com/yetj/p/9030078.html
时间: 2024-10-07 06:11:23