Python 3,通过Gmail托管的大学帐户发送电子邮件

时间:2017-11-06 22:57:25

标签: python email gmail-api

所以最近我一直试图通过我的大学电子邮件帐户my_email@my_college.edu使用Python 3.6发送电子邮件。此帐户托管在Google上,因此我可以将其与Gmail,云端硬盘等一起使用。因此我认为编写一个可以实现我想要的简单程序会很简单,但似乎没有任何效果。以下是我在几乎每个教程网站上找到的基本代码:

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。

任何帮助都将非常感谢!谢谢

1 个答案:

答案 0 :(得分:1)

尝试将服务器从smtp.gmail.com更改为my_college.edu(或者something.my_college.edu,或者类似的东西)。我已经配置了需要使用学校Gmails的电子邮件服务器URL的内容,如果我没记错的话,它会像那样工作。