【问题标题】:Error trying to send email to gmail python尝试向 gmail python 发送电子邮件时出错
【发布时间】:2018-07-26 03:50:53
【问题描述】:

大家好,我是 python 新手。我正在尝试将图像发送到我的 gmail 帐户并收到以下错误,有人可以帮我解决这个问题。 我找了又找,没找到答案我试过换端口。

我已打开谷歌安全性较低的应用程序,但不知道还能做什么。

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email.mime.text import MIMEText
from email import encoders
import os

gmail_user = "you@gmail.com"
gmail_pwd = "pass"

to = "you@gmail.com"
subject = "Report"
text = "Picture report"
attach = 'web.png'

msg = MIMEMultipart()

msg['From'] = gmail_user
msg['To'] = to
msg['Subject'] = subject

msg.attach(MIMEText(text))

part = MIMEBase('application', 'octet-stream')
part.set_payload(open(attach, 'rb').read())
encoders.encode_base64(part)
part.add_header('Content-Disposition',
   'attachment; filename="%s"' % os.path.basename(attach))
msg.attach(part)

mailServer = smtplib.SMTP("smtp.gmail.com", 465)
mailServer.ehlo()
mailServer.starttls()
mailServer.ehlo()
mailServer.login(gmail_user, gmail_pwd)
mailServer.sendmail(gmail_user, to, msg.as_string())
# Should be mailServer.quit(), but that crashes...
mailServer.close()  

  Traceback (most recent call last):
  File "C:\Users\scott\Desktop\PYTHON NEW\newemail.py", line 31, in <module>
    mailServer = smtplib.SMTP("smtp.gmail.com", 465)
  File "C:\Users\scott\AppData\Local\Programs\Python\Python36-32\lib\smtplib.py", line 251, in __init__
    (code, msg) = self.connect(host, port)
  File "C:\Users\scott\AppData\Local\Programs\Python\Python36-32\lib\smtplib.py", line 337, in connect
    (code, msg) = self.getreply()
  File "C:\Users\scott\AppData\Local\Programs\Python\Python36-32\lib\smtplib.py", line 393, in getreply
    raise SMTPServerDisconnected("Connection unexpectedly closed")
smtplib.SMTPServerDisconnected: Connection unexpectedly closed

我已将端口更改为 587 并收到不同的错误

Traceback (most recent call last):
  File "C:\Users\scott\Desktop\PYTHON NEW\newemail.py", line 35, in <module>
    mailServer.login(gmail_user, gmail_pwd)
  File "C:\Users\scott\AppData\Local\Programs\Python\Python36-32\lib\smtplib.py", line 729, in login
    raise last_exception
  File "C:\Users\scott\AppData\Local\Programs\Python\Python36-32\lib\smtplib.py", line 720, in login
    initial_response_ok=initial_response_ok)
  File "C:\Users\scott\AppData\Local\Programs\Python\Python36-32\lib\smtplib.py", line 641, in auth
    raise SMTPAuthenticationError(code, resp)
smtplib.SMTPAuthenticationError: (534, b'5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbt4\n5.7.14 ah5smKPNBMdR8EDhHji_lOLermVkofD0XZiYZtx04cUZGJIvjm6scA9FeCEhJhB--aeW58\n5.7.14 O3uS9IVuNfqKe4HYqXgdBMbvtMSOSSMM4oGYwlvDIoXpIK0IJYKSyAfvPyPcjiF8Q_Es4n\n5.7.14 33gUceqr9ZjlNI066kXt-uTq2V39X6YUS2-ixCCKfoozS9zoQ1KJuLSWU1IhB3gTsGtB9m\n5.7.14 N-AEdgucbByvuI7zr2KG-DZwlvrWw> Please log in via your web browser and\n5.7.14 then try again.\n5.7.14  Learn more at\n5.7.14  https://support.google.com/mail/answer/78754 b65sm27550600wrd.26 - gsmtp')

【问题讨论】:

标签: python email mime


【解决方案1】:

您的代码可以缩短到只有两行:

import smtplib
mailServer = smtplib.SMTP("smtp.gmail.com", 465)

如果您得到 smtplib.SMTPServerDisconnected,这意味着您的问题与代码无关。某些东西阻止了您与端口 465 的网络连接。

【讨论】:

【解决方案2】:

端口 465 上的 SMTP 服务器正在侦听加密的 TLS 连接。客户端不应使用STARTTLS 开始加密,因为 TLS 从一开始就处于活动状态。该 SMTP 命令用于 SMTP 服务器正在侦听明文连接的端口 587。

【讨论】:

  • 嗨,我更改了端口,但出现了另一个错误,我已将其放在上面
  • @scott.turner 错误信息中有来自google的说明。你有没有尝试关注他们?
  • 嗨,是的,我在错误中尝试了网页并说找不到该页面
  • 我可以访问该页面:support.google.com/mail/answer/78754 无论如何,在解决了错误端口的问题后,您现在遇到了另一个我不知道如何解决的问题。 DSN 代码 5.7.14 表示未交付:需要“信任关系”,因此您的代码可能没问题。
  • 谢谢您,我已对其进行了排序,这与我的作品互联网上的安全性有关。欢呼
【解决方案3】:

您可以使用 flask_mail 库来发送邮件。这里只是快速回顾.. '

from flask_mail import Mail, Message
app.config['SECRET_KEY'] = 'your secretkey'
app.config['MAIL_SERVER'] = 'smtp.gmail.com'
app.config['MAIL_PORT'] = 465
app.config['MAIL_USE_TLS'] = False
app.config['MAIL_USE_SSL'] = True
app.config['MAIL_USERNAME'] = 'your gmail id'
app.config['MAIL_PASSWORD'] = 'and its password'
mail = Mail(app)
msg = Message(subject=subject, sender=(master().user(), 
app.config['MAIL_USERNAME']), recipients=[recipients])
msg.body = "%s %s" % (body, msg.sender)
if html is not None:
    msg.html = str(html)
mail.connect()
mail.send(msg)

'

***注意:- 不要忘记关闭两步验证并打开允许不太安全的应用程序登录和安全 > 设备活动和安全事件

【讨论】:

  • Web 应用程序框架的扩展不是正确的工具。
  • 为什么???因为我认为使用已经实现的代码很好,它可以节省我们的时间..@vpfb
  • 上面的代码不完整。试着让它发挥作用。通常你需要flask 来创建app 对象。 flask 包含几个库(每个库有数千行代码)与发送电子邮件的任务完全无关。据我了解,这是浪费资源,而不是节省时间。
  • 但它也适用于我的项目,因为我创建了一个控制器......并使用 ajax 发送邮件数据......我认为它会比 flask_mail 更好
猜你喜欢
  • 1970-01-01
  • 2015-12-21
  • 1970-01-01
  • 2017-10-30
  • 1970-01-01
  • 2016-11-30
  • 1970-01-01
  • 2019-01-11
相关资源
最近更新 更多