首页 > 解决方案 > 无法发送多封电子邮件,因为我被中止(断开连接)

问题描述

使用 smtplib(python 3.6)发送多封电子邮件时出现“中止(断开连接)”,我在网上找不到太多信息。我很确定错误来自服务器而不是我的代码,但我想知道如何解决这个问题,或者至少知道如何通过谷歌获取更多信息,因为我对自己的情况一无所知要解决这个问题。

这是我正在使用发送一封电子邮件的函数:(我调用了这个函数五次,我得到了错误)。

import smtplib

def enviar_um_mail(to, subject, body):
    #source: https://stackabuse.com/how-to-send-emails-with-gmail-using-python/
    user = '<mail here>'
    password = '<passwrd here>' 

    sent_from = user
    to = 'somemail@somedomain.org'
    email_text = 'text of e-mail here'

    try:
        server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
        server.ehlo()
        server.login(user, password)
        server.sendmail(sent_from, to, email_text)
        server.close()

        print ('Email sent!')
    except Exception as e:
        print ('Something went wrong...')
        print(e)

当调用此函数五次时,python shell 显示:

Email sent!
Email sent!
Email sent!
Email sent!
Email sent!aborted (disconnected)

标签: pythongmailsendsmtplibdisconnected

解决方案


推荐阅读