SMTP行为异常

时间:2018-08-30 11:28:08

标签: python python-3.x smtp smtplib python-3.7

我正在尝试在python中使用SMTPLIB发送消息,但是收到一条错误消息。我肯定所使用的电子邮件和密码正确。我尝试通过或不通过网络浏览器登录来发送此邮件。由于以下stackoverflow文章指出:Error sending email: raise SMTPAuthenticationError(code, resp),所以我不允许来自SENDERS电子邮件的安全性较低的来源。在以下代码中,我用单词代替了实际的电子邮件和密码:

import smtplib

server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login("senderemail", "senderemailpassword")

msg = "Hello!"
server.sendmail("senderemail", "recieveremail", msg)
server.quit()

错误消息:

Traceback (most recent call last):
  File "D:\Hussain\Hussain's computing\Python\emailingtest.py", line 5, in <module>
    server.login("senderemail", "senderpassword")
  File "C:\Users\Hammad\AppData\Local\Programs\Python\Python37-32\lib\smtplib.py", line 730, in login
    raise last_exception
  File "C:\Users\Hammad\AppData\Local\Programs\Python\Python37-32\lib\smtplib.py", line 721, in login
    initial_response_ok=initial_response_ok)
  File "C:\Users\Hammad\AppData\Local\Programs\Python\Python37-32\lib\smtplib.py", line 642, in auth
    raise SMTPAuthenticationError(code, resp)
smtplib.SMTPAuthenticationError: (534, b'5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbvT\n5.7.14 ULHSk5VkFjTMqIX_jl5P4GMOarejb0s0WpOqP5RRrWPijD_2GRXQ--JBo5t2r_5pz-ysoY\n5.7.14 Eqv-0GUXB4v321WRxe2uRR1FlFqSTWy-qwXSDmXkkWqwah1akDIy4iw73_mach6-KntI0A\n5.7.14 HLVsfpyrYaTDdeJd54pUPlCEI5W9kZiv48-Ir2-NhbL_rqHrg3OrC-432Uc90Rskt9qNd0\n5.7.14 vAm9r3GQr8jUxrYIgj9YUP0h8xr8HKFFDQSX_RcuTn2lSrXIuy> Please log in via\n5.7.14 your web browser and then try again.\n5.7.14  Learn more at\n5.7.14  https://support.google.com/mail/answer/78754 h7-v6sm8163217wrs.3 - gsmtp')

如果有人可以修复我的代码,这将非常有帮助。

2 个答案:

答案 0 :(得分:0)

默认情况下,Google会阻止任何不使用现代安全标准的脚本/应用程序登录尝试。但是,您可以在https://www.google.com/settings/security/lesssecureapps上更改该设置。

答案 1 :(得分:0)

this tutorial中所述,可能值得Displaying the unlock captcha