【问题标题】:How do I fix SMTPRecipientsRefused: {'': (421, b'4.7.0 Too many protocol errors (6) on this connection, closing transmission channel error Python?如何修复 SMTPRecipientsRefused: {'': (421, b'4.7.0 Too many protocol errors (6) on this connection, close transmission channel error Python?
【发布时间】: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


    【解决方案1】:

    您在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找到。

    【讨论】:

      【解决方案2】:

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

      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)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-03-19
        • 2021-12-31
        • 1970-01-01
        • 2022-01-03
        • 2017-02-18
        • 2016-12-09
        • 2021-11-16
        • 2014-07-26
        相关资源
        最近更新 更多