gpt4 book ai didi

python 2 : SMTPServerDisconnected: Connection unexpectedly closed

转载 作者:太空狗 更新时间:2023-10-29 16:55:18 36 4
gpt4 key购买 nike

我在用 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()

所以在此之前,我的程序没有给我错误,但也没有给我发送电子邮件。现在 python 给我一个错误:

SMTPServerDisconnected: Connection unexpectedly closed

我该如何解决这个问题?

最佳答案

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_debuglevel(1) 尝试过 :),它会打印 SMTP 对话,您可以亲眼看到问题所在。

这里有两个选择:

  1. 继续使用那个中继; as explained by Google ,它只是未加密的 gmail-to-gmail,您必须通过他们的程序取消将您的 ip 列入黑名单

  2. 最简单的选择是切换到带身份验证的 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()

现在,如果尝试“欺骗”系统并使用不同的(非 Gmail)地址发送,它将 a) 要求您连接到不同的主机名(gmail 的一些 MX 记录),然后 b) 停止您并以列入黑名单的 ip 为由关闭连接,并且 c) 执行反向 DNS、DKIM 和许多其他反制措施,以确保您实际上可以控制您在 MAIL FROM: 地址中显示的域。

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

关于 python 2 : SMTPServerDisconnected: Connection unexpectedly closed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17759860/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com