使用python smtplib发送邮件错误 [英] Sending mail error with python smtplib
问题描述
我试图使用python 3.2 SMTPlib.sendmail()函数
发送消息,经过一些修改SMTP库(即
注释掉rset()函数,该函数抑制了错误msg)
我设法从服务器检索以下错误消息:
I am attempting to use the python 3.2 SMTPlib.sendmail() function to send a message, after some modifcation of the SMTP library (namely commenting out the rset() function which was suppressing the error msg) I managed to retrieve the following error message from the server:
SendMail失败
(554,b'Transaction失败:无法发送消息可能被滥用;请访问 http://postmaster.yahoo.com/abuse_smtp.html 更多信息')
SendMail Failed (554, b'Transaction failed : Cannot send message due to possible abuse; please visit http://postmaster.yahoo.com/abuse_smtp.html for more information')
雅虎邮件SMTP服务器认为我正在发送垃圾邮件,该URL确实链接到任何
有用。我认为它与一个不足的标题有关,我似乎找不到一个确定的
答案什么构成一个兼容的标题&我已经阅读了与Gmail相似的问题。
Mock电子邮件已经替代了这篇文章。
The yahoo mail SMTP server thinks I'm sending spam, the URL does link to anything useful. I think it has to do with an inadequate header, I can't seem to find a definitive answer on what constitutes a compliant header & I've read of simmilar issues with Gmail. Mock emails have been substituted for this post.
任何帮助将不胜感激
我的完整代码如下:
self.message = email.message_from_string('''To: <ksmith@yahoo.co.nz>
From: <rwilson@yahoo.co.nz>
Reply-To: <rwilson@yahoo.co.nz>
Subject: Test send mail \n\n Hello''')
fromAddress = 'rwilson@yahoo.co.nz'
toAddress = 'ksmith@yahoo.co.nz'
try:
self.smtp = SMTP()
self.smtp.connect('smtp.mail.yahoo.com')
except Exception:
print('Connection Failed')
print(traceback.format_exc())
try:
self.smtp.login('rwilson','tree22')
except Exception:
print('Login Failed!')
print(traceback.format_exc())
try:
self.smtp.sendmail(fromAddress,toAddress ,self.message.as_string())
print("Message sucessfully sent!")
self.smtp.close()
except Exception as e:
print('SendMail Failed')
print(e)
推荐答案
以下内容适用于微软,谷歌,雅虎账户在Python 2.7和Python 3.2:
The following works for microsoft, google, yahoo accounts on Python 2.7 and Python 3.2:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Send email via smtp_host."""
import smtplib
from email.mime.text import MIMEText
from email.header import Header
####smtp_host = 'smtp.live.com' # microsoft
####smtp_host = 'smtp.gmail.com' # google
smtp_host = 'smtp.mail.yahoo.com' # yahoo
login, password = ...
recipients_emails = [login]
msg = MIMEText('body…', 'plain', 'utf-8')
msg['Subject'] = Header('subject…', 'utf-8')
msg['From'] = login
msg['To'] = ", ".join(recipients_emails)
s = smtplib.SMTP(smtp_host, 587, timeout=10)
s.set_debuglevel(1)
try:
s.starttls()
s.login(login, password)
s.sendmail(msg['From'], recipients_emails, msg.as_string())
finally:
s.quit()
这篇关于使用python smtplib发送邮件错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!