【发布时间】:2021-11-19 02:43:12
【问题描述】:
我将使用 python 通过 gmail smtp 发送邮件。我将添加我的代码。
gmail_user = 'mymail@gmail.com'
gmail_password = 'xxxxxx'
context = ssl.create_default_context()
sent_from = gmail_user
to = ['yyyyyyyyy@gmail.com']
subject = 'xxxxx'
body = 'xxxxxxxxxx'
email_text = """\
From: %s
To: %s
Subject: %s
Car crash accident has been happened in xxxx.
You can check the location on a Waze map with below link.
%s
""" % (sent_from, ", ".join(to), subject, body)
try:
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls(context=context)
server.login(gmail_user, gmail_password)
server.sendmail(sent_from, to, email_text)
server.close()
print('Email sent!')
except:
print('Something went wrong...')
但我收到“出了点问题...”。我希望收到“已发送电子邮件!”。
请帮助我。谢谢。
【问题讨论】:
-
删除最后一个
try-except语句,并在没有它们的情况下运行其中的代码,然后告诉错误你得到了什么。 -
我已经按照您的建议进行了尝试。然后,我收到如下错误。
Username and Password not acceptable但我可以使用该信息登录并查看邮件。 -
用户名和密码是否正确?因为只有在获取错误凭据时才会显示错误
-
是的,我现在可以用 gmail 和密码登录 google 帐户并查看邮件。
-
然后在您的 gmail 设置中,您可能需要启用“访问不太安全的应用程序”,还需要进行 2 因素身份验证。您可以在谷歌中查看如何启用 2 因素身份验证