繁体   English   中英

尝试向 gmail python 发送电子邮件时出错

[英]Error trying to send email to gmail python

大家好,我是 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')

您的代码可以缩短为两行:

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

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

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

您可以使用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)

'

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM