Python smtplib: Gmail works, but only “sometimes”

我与影子孤独终老i 提交于 2021-02-05 09:41:37

问题


I wrote a simple script to send an email via the Gmail SMTP server. Here the code:

import smtplib
msg = 'Hello World!'
server = smtplib.SMTP('smtp.gmail.com', 587) #port 465 or 587
server.ehlo()
server.starttls()
server.ehlo()
server.login('username@googlemail.com','password')
server.sendmail('username@googlemail.com','username@googlemail.com', msg)
server.close()

Sometimes it works and sometimes I get an error. What is the reason for it? I have configured Gmail for accepting less secure apps. If the error occurs I get the following message:

Traceback (most recent call last):
  File "email.py", line 31, in <module>
    server.login('username@googlemail.com','password')
  File "/usr/lib/python3.5/smtplib.py", line 729, in login
    raise last_exception
  File "/usr/lib/python3.5/smtplib.py", line 720, in login
    initial_response_ok=initial_response_ok)
  File "/usr/lib/python3.5/smtplib.py", line 641, in auth
    raise SMTPAuthenticationError(code, resp)
smtplib.SMTPAuthenticationError: (534, b'5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbvQ\n5.7.14 DBMYWMukfjghdjfkghfjkhjkfhgjkdhgdfjkghekjghekjgndjkSm5lAOfEpP2Nt\n5.7.14 QihtNp5izjfghjjkjhgbhjbGHJVHJVjhvhjbhjbhjDZwhJFV-FiyvI-OGW\n5.7.14 jcpmHcQAcOR8e8G0zOfdugjhfdjd-sdfiugdsjfdsrthdfewrzjhg-shgv2HxmgWpZg3Z\n5.7.14 4G1ENiAlgiEnrkXyRbTG3frjZZdPg> Please log in via your web browser and\n5.7.14 then try again.\n5.7.14  Learn more at\n5.7.14  https://support.google.com/mail/answer/78754 g40sm24698383wrg.19 - gsmtp')

I couldn't find any help on the internet. Many thanks in advance for every advice.


回答1:


The answer recommends enabling access for less secure apps first. If that does not work you could try visiting the link, which is supplied on that page: https://www.google.com/accounts/DisplayUnlockCaptcha

This may enable access.



来源:https://stackoverflow.com/questions/41945919/python-smtplib-gmail-works-but-only-sometimes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!