您好我正试图通过谷歌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()
有人可以解释什么是错的或我错过了什么?
答案 0 :(得分:5)
我找到了解决方案,代码没有任何问题 这是Google邮件中的安全问题.. 我跟着这个link 并打开访问不太安全的应用程序,现在脚本作为一个魅力! :)
参考:Allowing less secure apps to access your account - Account Help