Smtplib无法从mail.ru发送电子邮件

时间:2017-09-17 04:36:09

标签: python email smtplib

我使用smtplib使用此代码发送电子邮件:

file = "path_to_a_file"
part = MIMEBase('application', "octet-stream")
part.set_payload(open(file, "rb").read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(file_book))
msg = MIMEMultipart()
msg['From'] = from_email@example.com
msg['To'] = to_email@example.com
msg.attach(part)
smtp = SMTP_SSL()
smtp.connect('smtp.yandex.ru')
smtp.ehlo()
smtp.login('my_email@yandex.ru', 'pass')
try:
    smtp.sendmail('my_email@yandex.ru', 'to_email@example.com', msg.as_string())
except SMTPException as e:
    print("Error: unable to send email: "+str(e))
smtp.quit()

如果我使用*@yandex.ru*@gmail.com*@my_domain.info作为msg['From'],则效果很好。不要改变任何东西,只是假装从指定的地址发送电子邮件。但是,如果我正在尝试使用*@mail.ru(一些糟糕的电子邮件服务),我会因此错误获得smtplib异常:

 (554, b'5.7.0 Failed to authorize the sender 1505621397-z6HZZunef2-9ulCCxTQ 1505621397-ZhCGNIKkSZ-9uPOg8fd')

如果没有进行授权凭据更改,为什么smtplib的行为会有所不同?

0 个答案:

没有答案