Python 2:SMTPServerDisconnected:连接意外关闭

Mic*_*ael 17 python email smtp python-2.7

使用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()
Run Code Online (Sandbox Code Playgroud)

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

SMTPServerDisconnected: Connection unexpectedly closed
Run Code Online (Sandbox Code Playgroud)

我怎样才能解决这个问题?

Tod*_*kov 13

很可能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
Run Code Online (Sandbox Code Playgroud)

我怎么知道的?因为我已经尝试过了:) s.set_debuglevel(1)打印SMTP会话,你可以直接看到问题是什么.

你有两个选择:

  1. 继续使用该继电器; 正如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()
Run Code Online (Sandbox Code Playgroud)

现在,如果试图'欺骗'系统并发送一个不同的(非gmail)地址,它将a)要求你连接到不同的主机名(gmail的一些MX记录),然后b)阻止你关闭以黑名单为基础的连接,以及c)做反向DNS,DKIM和许多其他对策,以确保您实际上控制您在MAIL FROM:地址中提供的域.

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


Tiw*_*tiw 8

我有同样的问题,只需通过指定正确的端口解决它:

smtplib.SMTP('smtp.gmail.com', 587)
Run Code Online (Sandbox Code Playgroud)


Nur*_*hid 8

使用smtplib.SMTP_SSL()而不是smtplib.SMTP()对我有用。尝试这个。