用python发送带附件的邮件

from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
from email.header import Header
import smtplib
def send_mail(newReport):
    
    smtpserver = 'smtp.mxhichina.com'
    user = '******@**.com'
    password = "password"
    sender = '******@**.com'
    receiver = "******@**.com"
    
    subject = 'auto test report'
           
    att = MIMEApplication(open(newReport[0], 'rb').read())
    att.add_header('Content-Disposition', 'attachment', filename=newReport[1])
        
    f = open(newReport[0], 'rb')
    mail_body = f.read()
    f.close()  
    msg = MIMEText(mail_body, 'html', 'utf-8')
       
    msgRoot = MIMEMultipart('related')
    msgRoot['Subject'] = subject
    msgRoot.attach(msg)
    msgRoot.attach(att)
    
    smtp = smtplib.SMTP_SSL()
    smtp.connect(smtpserver)
    smtp.login(user, password)
    smtp.sendmail(sender, receiver, msgRoot.as_string())
    smtp.quit()
注意:smtplib.SMTPAuthenticationError: (530, 'Error: A secure connection is requiered(such as ssl). More information at http://service.mail.qq.com/cgi-bin/help?id=28')
问题原因:发送邮箱没有开启IMAP/SMTP服务
解决办法:开启邮箱IMAP/SMTP服务【smtp = smtplib.SMTP_SSL() 】
原文地址:https://www.cnblogs.com/ybcao/p/7458500.html