Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2:SMTPServerDisconnected:连接意外关闭_Python_Python 2.7_Email_Smtp - Fatal编程技术网

Python 2:SMTPServerDisconnected:连接意外关闭

Python 2:SMTPServerDisconnected:连接意外关闭,python,python-2.7,email,smtp,Python,Python 2.7,Email,Smtp,我在用Python发送电子邮件时遇到一个小问题: #me == my email address #you == recipient's email address me = "some.email@gmail.com" you = "some_email2@gmail.com" # Create message container - the correct MIME type is multipart/alternative. msg = MIMEMultipart('alternativ

我在用Python发送电子邮件时遇到一个小问题:

#me == my email address
#you == recipient's email address
me = "some.email@gmail.com"
you = "some_email2@gmail.com"

# Create message container - the correct MIME type is multipart/alternative.
msg = MIMEMultipart('alternative')
msg['Subject'] = "Alert"
msg['From'] = me
msg['To'] = you

# Create the body of the message (a plain-text and an HTML version).
html = '<html><body><p>Hi, I have the following alerts for you!</p></body></html>'

# Record the MIME types of both parts - text/plain and text/html.
part2 = MIMEText(html, 'html')

# Attach parts into message container.
# According to RFC 2046, the last part of a multipart message, in this case
# the HTML message, is best and preferred.
msg.attach(part2)

# Send the message via local SMTP server.
s = smtplib.SMTP('aspmx.l.google.com')
# sendmail function takes 3 arguments: sender's address, recipient's address
# and message to send - here it is sent as one string.
s.sendmail(me, you, msg.as_string())
s.quit()

如何修复此问题?

TLDR:通过TLS切换到经过身份验证的连接

最有可能的是gmail服务器在数据命令发出后拒绝了连接(他们中的大多数人在这个阶段拒绝了:)。实际信息很可能是这样的:

    retcode (421); Msg: 4.7.0 [ip.octets.listed.here      15] Our system has detected an unusual rate of
    4.7.0 unsolicited mail originating from your IP address. To protect our
    4.7.0 users from spam, mail sent from your IP address has been temporarily
    4.7.0 rate limited. Please visit
    4.7.0  https://support.google.com/mail/answer/81126 to review our Bulk Email
    4.7.0 Senders Guidelines. qa9si9093954wjc.138 - gsmtp
我怎么知道?因为我已经尝试过:)使用
s.set\u debuglevel(1)
,它打印SMTP对话,您可以直接看到问题所在

这里有两个选项:

  • 继续使用该继电器,这是未加密的gmail到gmail,你必须通过他们的程序取消你的ip黑名单

  • 最简单的选择是切换到具有身份验证的TLS

  • 以下是更改的源的外观:

    # skipped your comments for readability
    import smtplib
    from email.mime.multipart import MIMEMultipart
    from email.mime.text import MIMEText
    
    me = "some.email@gmail.com"
    my_password = r"your_actual_password"
    you = "some.email2@gmail.com"
    
    msg = MIMEMultipart('alternative')
    msg['Subject'] = "Alert"
    msg['From'] = me
    msg['To'] = you
    
    html = '<html><body><p>Hi, I have the following alerts for you!</p></body></html>'
    part2 = MIMEText(html, 'html')
    
    msg.attach(part2)
    
    # Send the message via gmail's regular server, over SSL - passwords are being sent, afterall
    s = smtplib.SMTP_SSL('smtp.gmail.com')
    # uncomment if interested in the actual smtp conversation
    # s.set_debuglevel(1)
    # do the smtp auth; sends ehlo if it hasn't been sent already
    s.login(me, my_password)
    
    s.sendmail(me, you, msg.as_string())
    s.quit()
    
    #为了便于阅读,跳过了您的评论
    导入smtplib
    从email.mime.multipart导入MIMEMultipart
    从email.mime.text导入MIMEText
    给我一些。email@gmail.com"
    我的密码=r“你的实际密码”
    你需要一些。email2@gmail.com"
    msg=MIMEMultipart('alternative')
    msg['Subject']=“警报”
    msg['From']=我
    msg['To']=您
    html='嗨,我有以下提醒给你

    " part2=MIMEText(html,'html') 附加信息(第2部分) #通过gmail的常规服务器,通过SSL发送消息——毕竟,密码正在被发送 s=smtplib.SMTP\u SSL('SMTP.gmail.com')) #如果对实际smtp对话感兴趣,请取消注释 #s.set_调试级别(1) #执行smtp身份验证;如果尚未发送,则发送ehlo s、 登录(我,我的密码) s、 sendmail(我,你,msg.as_string()) s、 退出
    现在,如果试图“欺骗”系统并使用不同的(非gmail)地址发送,它将a)要求您连接到不同的主机名(gmail的一些MX记录),然后b)以黑名单上的ip为理由阻止您并关闭连接,c)执行反向DNS,DKIM和许多其他对策,以确保您实际控制您在邮件发件人:address中提供的域


    最后,还有选项3)-使用任何其他电子邮件中继服务,有很多好的:)

    我也遇到了同样的问题,只需指定正确的端口即可解决,如下所示:

    smtplib.SMTP('smtp.gmail.com', 587)
    

    使用
    smtplib.SMTP\u SSL()
    而不是
    smtplib.SMTP()
    对我很有效。试试这个。

    我意识到一种奇怪的行为。我使用了类似的代码,包括问题和答案。我的代码在过去几天一直在工作。然而,今天我遇到了问题中提到的错误消息

    我的解决方案: 我通过图书馆网络尝试了我的成功尝试。今天我通过星巴克网络(通过专属门户网站)尝试了一下。我把它改成了我的移动网络。它又开始工作了


    谷歌可能会拒绝来自不可靠网络的请求。

    我也面临同样的问题。就我而言,密码几天前就被更改了。所以,它给出了错误。当我在代码中更新密码时,它就像一个符咒一样工作

    谷歌:我在本地运行了一个测试smtp服务器。我之所以出现此错误,是因为我在关闭客户端smtp之前关闭了本地smtp服务器。

    您是否检查了您的internet连接?嘿,看到这个问题:-可能与本地smtp服务器重复我认为问题出在本地smtp服务器是否有人知道任何其他公共smtp服务器?您认为这就是问题所在,或者你添加这个仅仅是因为它有相同的错误信息。如果只是因为错误消息相同,那么最好添加一个新问题,并将其作为新问题的答案提供。为什么不针对上述所有答案提出相同的问题?因为审阅队列一次只显示一个供审阅的答案。问其他人同样的问题可能是公平的,如果你愿意的话,你可以标记他们进行审查。这是有道理的。我想这可能是对上述问题的一个回答,没有人被接受。我将答案部分视为对具有相同错误消息的未来用户的帮助,不仅仅是询问问题的人,尤其是在回答未被接受的情况下。使用新问题的措辞的好处是,如果有人搜索错误消息和一些关键字,则更有可能找到新问题。一个基本上是错误信息的问题是,如果错误信息是一般性的,那么你可以在下面找到大量不相关的答案,这些都可能是更具体问题的好答案。
    smtplib.SMTP('smtp.gmail.com', 587)