【发布时间】:2018-04-19 05:23:03
【问题描述】:
最近我一直在尝试通过我的大学电子邮件帐户my_email@my_college.edu 使用 Python 3.6 发送电子邮件。该帐户托管在 google 上,因此我可以将其与 Gmail、Drive 等一起使用。因此,我认为编写一个简单的程序来完成我想要的事情就足够简单了,但似乎没有任何效果。这是我在几乎每个教程网站上找到的基本代码:
import smtplib
TO = 'receiver_email'
SUBJECT = 'TEST MAIL'
TEXT = 'Here is a message from python.'
# Gmail Sign In
gmail_sender = 'my_email@my_college.edu'
gmail_passwd = 'my_password'
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.login(gmail_sender, gmail_passwd)
BODY = '\r\n'.join(['To: %s' % TO,
'From: %s' % gmail_sender,
'Subject: %s' % SUBJECT,
'', TEXT])
try:
server.sendmail(gmail_sender, [TO], BODY)
print ('email sent')
except:
print ('error sending mail')
server.quit()
每当我运行它时,我都会收到“错误凭据”错误。具体来说,我得到的错误是这个:
Traceback (most recent call last):
File "D:\Coding Files\Projects in Progress\autoEmail.py", line 14, in
<module>
server.login(gmail_sender, gmail_passwd)
File "C:\Users\Joe\AppData\Local\Programs\Python\Python36-
32\lib\smtplib.py", line 730, in login
raise last_exception
File "C:\Users\Joe\AppData\Local\Programs\Python\Python36-
32\lib\smtplib.py", line 721, in login
initial_response_ok=initial_response_ok)
File "C:\Users\Joe\AppData\Local\Programs\Python\Python36-
32\lib\smtplib.py", line 642, in auth
raise SMTPAuthenticationError(code, resp)
smtplib.SMTPAuthenticationError: (535, b'5.7.8 Username and Password not
accepted. Learn more at\n5.7.8 https://support.google.com/mail/?
p=BadCredentials g8sm9220621qtg.23 - gsmtp')
我尝试使用 Gmail API,它能够让我获得 Gmail 凭据并让我登录 chrome 页面,但我似乎找不到足够的教程来让我能够理解使用 Gmail API .
任何帮助将不胜感激!谢谢
【问题讨论】:
-
问题是GMail在默认配置中不允许用户/密码识别。您需要使用 oauth 或使用他们所谓的不太安全的应用配置:support.google.com/accounts/answer/6010255?hl=en
-
错误消息中包含一个链接,说明如何操作,support.google.com/mail/?p=BadCredentials
-
我查看了链接,但不确定哪个问题与我相关。我现在明白它源于不安全的设备,但我仍然不确定这是否是最好的方法。