web-dev-qa-db-ja.com

Python 2:SMTPServerDisconnected:接続が予期せず閉じられました

Pythonでメールを送信する際に小さな問題があります。

#me == my email address
#you == recipient's email address
me = "[email protected]"
you = "[email protected]"

# 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

どうすれば修正できますか?

16
Michael

ほとんどの場合、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会話を出力し、問題が何であるかを直接見ることができます。

ここには2つのオプションがあります。

  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 = "[email protected]"
my_password = r"your_actual_password"
you = "[email protected]"

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)もあります-他の電子メール中継サービスを使用してください、たくさんの良いものがあります:)

10
Todor Minakov

私は同じ問題を抱えていて、次のように正しいポートを指定するだけで解決しました:

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

私は奇妙な行動に気づきました。質問と回答の両方に言及した同様のコードを使用しました。私のコードはここ数日間機能しています。しかし、今日、質問に記載されているエラーメッセージが表示されました。

私の解決策:図書館ネットワークを介して成功した試みを試みました。今日、スターバックスネットワーク(キャプティブポータル経由)で試してみました。モバイルネットワークに変更しました。再び機能し始めました。

おそらく、Googleは信頼できないネットワークからのリクエストを拒否します。

1
Onur Tuna

smtplib.SMTP_SSL()の代わりにsmtplib.SMTP()を使用するとうまくいきます。これを試して。

1

私は同じ問題に直面していました。私の場合、パスワードは数日前に変更されました。そのため、エラーが発生していました。コードでパスワードを更新すると、それは魅力のように機能します... !!!

1
Sohel Pathan

Google社員:ローカルで実行されているテストSMTPサーバーがありました。クライアントsmtpを閉じる前にローカルsmtpサーバーをシャットダウンしていたため、このエラーが発生していました。

0
Daniel Butler