报错如下:
Traceback (most recent call last):
File "D:/AUTOTEST/sweetest_example/sendemaildemo2.py", line 53, in <module>
server.sendmail(from_addr,areceiver.split(',')+acc.split(','),msg.as_string())
File "D:\ToolsDownloads\Python-3.7.3\lib\smtplib.py", line 888, in sendmail
raise SMTPDataError(code, resp)
smtplib.SMTPDataError: (554, b'DT:SPM 163 smtp12,EMCowAAnhwelrDZdzm1+CQ--.27641S2 1563864230
代码如下:
# -*- coding: UTF-8 -*-
import smtplib
from emailimport encoders
from email.headerimport Header
from email.mime.textimport MIMEText
from email.utilsimport parseaddr,formataddr
from email.mime.applicationimport MIMEApplication
from email.mime.multipartimport MIMEMultipart
#发件人邮箱
asender="sunzhi@163.com"
#收件人邮箱
areceiver="1905774291@qq.com"
#抄送人邮箱
acc="1949348145@qq.com"
asubject="这是一份测试邮件"
#发件人地址
from_addr ="sunzhi@163.com"
#邮箱授权码
password="123456"
#邮件设置
msg = MIMEMultipart()
msg['Subject'] = asubject
msg['to'] = areceiver
msg['Cc'] = acc
msg['from'] ="sunzhi@163.com"
#邮件正文内容
body ="自动化测试结果"
#添加邮件正文
msg.attach(MIMEText(body,'plain','utf-8'))
#添加附件,注意文件路径是斜杠
xlsxpart = MIMEApplication(open('D:/AUTOTEST/sweetest_example/report/SystemM_UserM_001/SystemM_UserM_001-Report@20190606_104202.xlsx','rb').read())
xlsxpart.add_header('Content-Disposition','attachment',filename="测试结果的附件")
msg.attach(xlsxpart)
#设置邮箱服务器地址以及端口
smtp_server ="smtp.163.com"
server = smtplib.SMTP(smtp_server,25)
# server.set_debuglevel(1)
#登录邮箱
server.login(from_addr,password)
print(areceiver.split(',')+acc.split(','))
#发送邮件
server.sendmail(from_addr,areceiver.split(',')+acc.split(','),msg.as_string())
#断开服务器链接
server.quit()
解决办法:
26行:msg['from'] ="sunzhi@163.com" 这行代码给变量赋值的是个字符串,应该改成:msg['from'] = asender,这样才是把邮箱地址赋值给了这个参数