【发布时间】:2021-07-26 04:58:15
【问题描述】:
在 Python 中通过 smptplib 向某个列表发送电子邮件时出现此错误
SMTPRecipientsRefused: {'': (421, b'4.7.0 Too many protocol errors (6) on this connection, closing transmission channel.')}?
我正在使用 Office365 SMTP 详细信息,代码的 sn-p 如下:-
import smtplib, ssl
from email.message import EmailMessage
import getpass
ids = df['IDs']
emails_to = df['Emails']
namesofcompanies = df["CompanyNames"]
sendfrom = df["SenderList"]
date_7days = (datetime.now() + timedelta(days=7)).strftime('%d/%m/%Y')
date_14days = (datetime.now() + timedelta(days=13)).strftime('%d/%m/%Y')
email_pass = input() #Office 365 password
context=ssl.create_default_context()
for i in range(len(emails_to)): # iterate through the records
# for every record get the name and the email addresses
ID = str(ids[i])
Emaitstosendto = emails_to[i]
companynames = namesofcompanies[i]
tosendfrom = sendfrom[i]
if my_files_dict.get(ID): #Looks for attachments in the same folder with same name as the corresponding record
smtp_ssl_host = 'smtp.office365.com'
smtp_ssl_port = 587
email_login = "xxx@xxx.com" #Office 365 email
email_from = tosendfrom
email_to = Emaitstosendto
msg = MIMEMultipart()
msg['Subject'] = "Received Emails between "+date_7days+" - "+date_14days
msg['From'] = email_from
msg['To'] = email_to
msg['X-Priority'] = '2'
text = ("XXXX,\n"
f"xxxxxx\n\n")
msg.attach(MIMEText(text))
filename = my_files_dict.get(ID)#Files in the folder matching the ID
fo = open(filename,'rb')
s2 = smtplib.SMTP(smtp_ssl_host, smtp_ssl_port)
s2.starttls(context=context)
s2.login(email_login, email_pass)
attachment = email.mime.application.MIMEApplication(fo.read(),_subtype="xlsx")
fo.close()
attachment.add_header('Content-Disposition','attachment',filename=filename)
msg.attach(attachment)
s2.send_message(msg)
s2.quit()
平均而言,我会将电子邮件发送到一个以分号 (;) 分隔的列表,每条记录大约有 8 封电子邮件。这意味着对于每个附件,我将发送到大约 8 封电子邮件,我将为大约 70 个这样的联系人发送邮件。总共将有大约 560 封电子邮件。什么都没有发送出去我在登录的那一刻就收到了上述错误。相反,当我尝试将其发送到测试电子邮件列中的 3 封电子邮件列表时,相同的电子邮件会很好地发送出去。谁能指出我可能做错的地方?我怀疑电子邮件列表太长或者电子邮件地址有问题因此协议错误?这是 SMTPlib 限制吗?
【问题讨论】:
标签: python email outlook office365 smtplib