python smtp gmail authentication error (sending email through gmail smtp server)

蓝咒 提交于 2019-12-22 10:43:26

问题


I have the following code

import smtplib
from email.mime.text import MIMEText



smtpserver = 'smtp.gmail.com'
AUTHREQUIRED = 1 # if you need to use SMTP AUTH set to 1
smtpuser = 'admin@myhost.com'  # for SMTP AUTH, set SMTP username here
smtppass = '123456'  # for SMTP AUTH, set SMTP password here

RECIPIENTS = ['online8@gmail.com']
SENDER = 'admin@myhost.com'

msg = MIMEText('dsdsdsdsds\n')
msg['Subject'] = 'The contents of iii'
msg['From'] = 'admin@myhost.com'
msg['To'] = ''online8@gmail.com''

mailServer = smtplib.SMTP('smtp.gmail.com',587)
mailServer.ehlo()
mailServer.starttls()
mailServer.ehlo()
mailServer.login(smtpuser, smtppass)
mailServer.sendmail(smtpuser,RECIPIENTS,msg.as_string())
mailServer.close()

this code works fine on my desktop. but it failed with this error

smtplib.SMTPAuthenticationError: (535, '5.7.1 Username and Password not accepted. Learn more at\n5.7.1 http://mail.google.com/support/bin/answer.py?answer=14257 21sm4713429agd.11')

on my linux server.

Not sure what went wrong, should i open some port on my linux server?


回答1:


Port 587 obviously needs to be open, but it probably is (or you wouldn't have gotten the detailed error msg in question). Python 2.5 vs 2.6 should make no difference. I think the issue has to do with "solving a captcha" once on the computer for which logins are currently getting rejected; follow the detailed instructions at the URL in the error message, i.e., http://mail.google.com/support/bin/answer.py?answer=14257




回答2:


import random,time
for i in range(1,100):
    y=random.randint(30,300)
    time.sleep(y)
    print ("Mailing for fun, Mail No: " + str(i))
    msg = MIMEText('Testing mailing \n Mail No:' + str(i))
    msg['Subject'] = 'Mail Number: ' + str(i)

Randomizing the mail interval to check smtp behavior :)

With a bit addition n modification, I got this to work to check our intermittent mail bouncing.



来源:https://stackoverflow.com/questions/1384535/python-smtp-gmail-authentication-error-sending-email-through-gmail-smtp-server

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