本文主要是介绍解决Python使用139邮箱报错smtplib.SMTPDataError: (550, b‘2f0d645e15038f4-1891b Mail rejected‘),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
封装了一个邮件发送类,负责建立连接并发送邮件。
import smtplib
from datetime import datetime
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.header import Headerclass Mail:# 登录及服务器信息mail_host = None # 邮箱服务器地址mail_port = None # 邮箱服务端口sender = None # 发送者(邮箱账号)authCode = None # 授权码receivers = None # 接收者ccs = None # 抄送者# 邮箱连接mailServerCon = None# 邮件对象envelope = Nonedef __init__(self, mailhost, mailport, authCode, sender, receivers, cc):''':param authCode:授权码:param sender:发送者(邮箱账号):param receivers:接受者,多个接受者用英文分号分隔:param cc:抄送者,多个抄送者用英文分号分隔'''Mail.mail_host = mailhostMail.mail_port = mailportMail.authCode = authCodeMail.sender = senderMail.receivers = receiversMail.ccs = ccdef login(self):# 连接邮箱服务器Mail.mailServerCon = smtplib.SMTP_SSL(Mail.mail_host, Mail.mail_port)# 登录邮箱Mail.mailServerCon.login(Mail.sender, Mail.authCode)def write(self, title, content, attachment=[]):''':param title: 邮件的主题:param content: 邮件的内容:param attachment: 邮件的附件,字符列表对象,附件的绝对路径:return:'''# 1.创建邮件对象Mail.envelope = MIMEMultipart()# 2.设置邮件主题subject = Header(title, 'utf-8').encode()Mail.envelope['Subject'] = subject# 3.设置邮件发送者Mail.envelope['From'] = Mail.sender# 4.设置邮件抄送者if Mail.ccs is not None and Mail.ccs !="":Mail.envelope['Cc'] = Mail.ccs# 4.设置邮件接收者Mail.envelope['To'] = Mail.receivers# 5.添加文字内容body = MIMEText(content, 'plain', 'utf-8') # 类型:plain(简单的⽂字内容)、html(超文本) 邮件对象.attach(文字对象)Mail.envelope.attach(body)# 6.添加附件for filepath in attachment:filename = filepath[filepath.rfind("/")+1:]annex = MIMEApplication(open(filepath, 'rb').read())annex["Content-Type"] = 'application/octet-stream' # 设置内容类型annex.add_header('Content-Disposition', 'attachment', filename=filename) # 添加到header信息Mail.envelope.attach(annex)def send(self):receivers = Mail.receiversif Mail.ccs is not None and Mail.ccs !="":receivers = receivers+";"+Mail.ccsMail.mailServerCon.sendmail(Mail.sender, receivers.split(";"), Mail.envelope.as_string())Mail.mailServerCon.quit()if __name__ == '__main__':# 执行邮件发送mailhost = "smtp.139.com"mailport = 465mailauthcode = "8baxxxxxxxxxxxxx00" # 授权码mailsender = "182xxxxxxxx@139.com" # 发送方邮箱mailreceiver = "xxxxx@qq.com;yyyyyy@qq.com" # 收件人邮件mailcc = "" # 抄送人邮箱mail = Mail(mailhost, mailport, mailauthcode, mailsender, mailreceiver, mailcc)mail.login()huaweiOutputName = dataPath + "\文件1.xlsx"zteOutputName = dataPath + "\文件2.xlsx"attachments = [huaweiOutputName, zteOutputName]curTime = datetime.now()formCurTime = curTime.strftime('%Y-%m-%d %H:%M:%S')mail.write("【XXX分析】,第"+str(1)+"次", "分析时间:"+formCurTime, attachments)mail.send()print("邮件发送成功...")
运行报错如下:
smtplib.SMTPDataError: (550, b'2f0d645e15038f4-1891b Mail rejected')
经过检查,发现是139邮箱的设置有问题,错误设置如下图:
邮箱协议设置中,两个邮箱是都打开的。
经过实测,修改为只打开下一个“开启IMAP/SMTP服务”,入下图所示,就可以发送成功。
到此,结束。
这篇关于解决Python使用139邮箱报错smtplib.SMTPDataError: (550, b‘2f0d645e15038f4-1891b Mail rejected‘)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!