如何修复SMTPRecipientsRefused:{“”:(421,b'4.7.0)此连接上的协议错误太多(6),关闭传输通道错误?

2024-04-26 03:12:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我在Python中通过smptplib向特定列表发送电子邮件时遇到此错误

SMTPRecipientsRefused: {'': (421, b'4.7.0 Too many protocol errors (6) on this connection, closing transmission channel.')}?

我正在使用Office365 SMTP详细信息,下面是代码片段:-

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封电子邮件。未发送任何内容。我在登录时收到上述错误。相反,当我试着将它发送到testemails列中包含3封电子邮件的列表中时,同样的电子邮件发送得非常好。有人能指出我哪里做得不对吗?我怀疑是电子邮件列表太长,还是电子邮件地址有问题,从而导致协议错误?这是SMTPlib限制吗


Tags: thetoidssldf列表attachmentdate
2条回答

您在MIMEMultipart()中指定的内容显示在消息头中,但这并不总是等于收件人列表。您可以尝试将server.send_message(msg)更改为server.sendmail(sender,recipients,msg.as_string())

请记住sendmail()需要一个收件人列表,而msg['To']应设置为一个字符串,因此如果变量email_to是逗号分隔的,则应按如下方式编写:

s2.sendmail(email_from,email_to.split(','),msg.as_string())

有关sendmail()的更多详细信息,请参见here

我通过创建一个列表并使用逗号字符作为分隔符,将元组中的所有地址连接到一个字符串中,从而解决了这个错误

family=所有收件人的电子邮件地址列表

family = [
'name1@example.com',
'name2@example.com',
'name3@example.com',
'name4@example.com',
'name5@example.com',
'name6@example.com',
'name7@example.com',
'name8@example.com'
]

msg['To'] =', '.join(family)

相关问题 更多 >