使用python尝试使用SMTP将客户端邮件中的邮件发送到gmail

时间:2018-10-24 09:52:17

标签: python python-2.7

python版本2.7

我正在尝试使用SMTP_SSL服务器将邮件从客户端电子邮件(abc@xyz.com)发送到Gmail(qwe@gmail.com)
即使我的凭据正确,也会收到如下所示的身份验证错误

(535, '5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials o194-v6sm2337288ito.11 - gsmtp')

但是从Gmail发送到客户端邮件使用相同的代码
这是我的代码

from smtplib import SMTPAuthenticationError,SMTP_SSL as SMTP,SMTPException
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.mime.application import MIMEApplication

From_mail = 'support@xyz.com'
To_mail = ['qwe@gmail.com','abc@gmail.com']

msg = MIMEMultipart()
msg['Subject'] = 'test email'
msg['From'] = From_mail
msg['To'] = ','.join(To_mail)
message = '''
Test mail.
Please find the attached file.
'''
msg.attach(MIMEText(message))

part = MIMEApplication(open('E:\abc.csv',"rb").read())
part.add_header('Content-Disposition', 'attachment', filename='abc.csv')
msg.attach(part)

try:
    smtp_server = SMTP('smtp.gmail.com', '465')
    # smtp_server.ehlo()
    smtp_server.login(From_mail, 'xxxx') ##(sender mail 
    id,password)
    for i in To_mail:
        smtp_server.sendmail(From_mail, str(i), msg.as_string())
        print "Mail sent to:- " +str(i)
except SMTPAuthenticationError as x:
    print x
    print "Authentication Failed :- Incorrect username/password."
finally:
 smtp_server.close() 

以前的问题对我没有帮助!
FYI凭证和端口号正确,还是我在这里遗漏了什么?

0 个答案:

没有答案