尝试使用python smtplib发送电子邮件时出错

时间:2017-09-01 15:00:30

标签: python email smtplib

import smtplib

host = 'smtp.gmail.com'
port = 587
username = 'rajinirajadev@gmail.com'
password = '*************'
from_email = username
to_list = ['rajiniraja00@gmail.com']

email_conn = smtplib.SMTP(host,port)
email_conn.ehlo()
email_conn.starttls()
email_conn.login(username, password)
email_conn.sendmail(from_email, to_list, 'that is send via smtplib')
email_conn.quit()

这是我得到的错误。我关闭了两步验证并启用了允许gmail上的安全应用。

Traceback (most recent call last):
  File "g:/python files/sending_mail.py", line 13, in <module>
    email_conn.login(username, password)
  File "C:\Users\rajini\AppData\Local\Programs\Python\Python36\lib\smtplib.py", line 730, in login
    raise last_exception
  File "C:\Users\rajini\AppData\Local\Programs\Python\Python36\lib\smtplib.py", line 721, in login
initial_response_ok=initial_response_ok)
  File "C:\Users\rajini\AppData\Local\Programs\Python\Python36\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=AKgnsbtt\n5.7.14 g2A-ALn3B03_YHSMdoFJs18v5jyKCwzi2BirMS2zKuE-fitmN8pDinUQ0-GET0D6hx_SRv\n5.7.14 -6SsaoSCB5IMf6qdHbpG3
79Cm00CyozXK-Ece_1NiViBAQDtSvUmSiJuEhKADmR8xNzsek\n5.7.14 H_Ddosp0mUfQLX2mVl5GmgmGVDaQEZJPAv4YSR8fAZyGydbg1QzFsbHhay2eKo5ttQL6nM\n5.7.14 sgSIHt_sIB4p9AHEWXQU-2J4F-Q5I> Please log in via your web browser and\n5.7.14 then try aga
in.\n5.7.14  Learn more at\n5.7.14  https://support.google.com/mail/answer/78754 186sm583041pgi.11 - gsmtp')

1 个答案:

答案 0 :(得分:0)

当我使用我的gmail帐户发送电子邮件时,我发生了同样的问题。即通过host ='smtp.gmail.com'

但是当我使用Outlook帐户(即,通过host ='smtp.outlook.com')时,此问题已解决,并且收到的消息为“((235,b'2.7.0身份验证成功”))

结论-Gmail垃圾邮件安全性被阻止,因为该脚本将该脚本视为某些不受信任的来源,并向我各自的帐户发送了一封邮件,该邮件已阻止登录免税