python - raise SMTPServerDisconnected("连接意外关闭")
问题描述
带有 smtlib 连接的新问题
例子
当我尝试使用带有 gmail 或私人 gmail 域的 python 发送电子邮件时,阻止/关闭/断开所有 smtp
编码
import pandas as pd
import smtplib
e = pd.read_excel("xxx")
emails = e['xxx'].values
server = smtplib.SMTP("smtp.gmail.com", 587)
server.login = "xxx"
server.password = "xxx"
server.starttls()
msg = "Hello this is a email form python"
subject = "Hello world"
body = "Subject: {}\n\n{}".format(subject, msg)
for email in emails:
server.sendmail(server.login, server.password, body)
server.quit()
错误
File "C:\Python39\lib\smtplib.py", line 637, in auth
(code, resp) = self.docmd("AUTH", mechanism + " " + response)
File "C:\Python39\lib\smtplib.py", line 427, in docmd
return self.getreply()
File "C:\Python39\lib\smtplib.py", line 400, in getreply
raise SMTPServerDisconnected("Connection unexpectedly closed")
smtplib.SMTPServerDisconnected: Connection unexpectedly closed
解决方案
为了通过 发送邮件smtp.gmail.com
,您需要验证您的 SMTP 会话。您通常会通过调用对象login
上的方法来执行此操作smtplib.SMTP
...但是您已将此方法替换为字符串,因此即使您尝试过也无法执行此操作。
因为您没有进行身份验证,Gmail 会断开您的连接。
此外,您似乎正在使用您的用户名和密码,该sendmail
方法需要“发件人”和“收件人”电子邮件地址。
您可能希望您的代码看起来更像这样:
import pandas as pd
import smtplib
e = pd.read_excel("xxx")
emails = e['xxx'].values
server = smtplib.SMTP("smtp.gmail.com", 587)
server.starttls()
server.login("your_gmail_account_here", "your_password_here")
msg = "Hello this is a email from python"
subject = "Hello world"
body = "Subject: {}\n\n{}".format(subject, msg)
for email in emails:
server.sendmail("you@your_email_address", email, body)
server.quit()
这假设这emails
实际上是一个电子邮件地址列表。您需要在此处填写信息(您的用户名、密码和电子邮件地址)才能正常工作。
推荐阅读
- node.js - 猫鼬模式问题文档无法存储某些字段的值
- python - 我的文件仍在运行,但它不应该运行
- asp.net-core - 操作的冲突方法/路径组合 - Swagger 无法将替代版本与 Route 区分开来
- sql - 在选择中获取最高值
- ansible - Ansible AWX - 在一个 AWX 模板中使用一个剧本部署具有不同凭据的多个主机
- javascript - jQuery 在控制台中工作,但是当我将它保存在我的站点中时,它没有
- django - 如何使用 gatling 测试受 CSRF 保护的站点?
- c# - VB.NET Webservice (asmx) - 返回一个 PDF
- perl - Perl Tk 打开附加对话框
- angular - 无数据的 AgGrid 自定义模板无法正常工作