gpt4 book ai didi

python - 在 python 中使用谷歌 SMTP 发送电子邮件

转载 作者:行者123 更新时间:2023-11-28 21:48:54 24 4
gpt4 key购买 nike

您好,我正在尝试通过 google SMTP 在 python 中发送电子邮件
我认为代码是正确的,但我收到以下异常..

"G:\Installed Applications\Python\Python35-32\python.exe" "C:/Users/brand/Desktop/Test Projects/Python Projects/SMTP.py"
Traceback (most recent call last):
File "C:/Users/brand/Desktop/Test Projects/Python Projects/SMTP.py", line 22, in <module>
server.login(sender, password)
File "G:\Installed Applications\Python\Python35-32\lib\smtplib.py", line 730, in login
raise last_exception
File "G:\Installed Applications\Python\Python35-32\lib\smtplib.py", line 721, in login
initial_response_ok=initial_response_ok)
File "G:\Installed Applications\Python\Python35-32\lib\smtplib.py", line 627, in auth
initial_response = (authobject() if initial_response_ok else None)
File "G:\Installed Applications\Python\Python35-32\lib\smtplib.py", line 664, in auth_login
raise SMTPAuthenticationError(code, resp)
smtplib.SMTPAuthenticationError: (502, b'5.5.1 Unrecognized command. m16sm36193099wmb.13 - gsmtp')

Process finished with exit code 1

我的代码显示在这里:

import smtplib

sender = 'myEmail@gmail.com' # will be replaced with my real email address

password = 'mypassword' # will be replaced with my real password

receivers = ['yourEmail@gmail.com']

message = """From: DaftPunk <myEmail@gmail.com>
To: BlueStar <yourEmail@gmail.com>
MIME-Version: 1.0
Content-Type: text/html
Subject: Test E-Mail

Please visit this new website
<a href="http://www.google.com">http://www.google.com</a>
"""

server = smtplib.SMTP('smtp.gmail.com:587')
server.ehlo()
server.starttls()
server.login(sender, password) # Exception here
try:
server.sendmail(sender, receivers, message)
print("Message sent successfully")
except:
print("Failed to send message")
server.quit()

有人能解释一下哪里出了问题或我遗漏了什么吗?

最佳答案

我已经找到解决办法了,代码没有问题
这是谷歌邮件中的一个安全问题..我关注了这个link并打开对安全性较低的应用程序的访问,现在该脚本非常有用! :)


引用:Allowing less secure apps to access your account - Account Help

关于python - 在 python 中使用谷歌 SMTP 发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34460814/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com