使用python smtplib发送邮件错误

时间:2013-10-26 12:36:56

标签: python email smtp python-3.2 yahoo-mail

我正在尝试使用python 3.2 SMTPlib.sendmail()函数 在对SMTP库进行一些修改之后发送消息(即 注释掉rset()函数,它抑制了错误消息msg) 我设法从服务器检索以下错误消息:

SendMail失败 (554,b'交易失败:由于可能滥用而无法发送消息;请访问http://postmaster.yahoo.com/abuse_smtp.html以获取更多信息')

雅虎邮件SMTP服务器认为我发送垃圾邮件,URL确实链接到任何内容 有用。我认为它与标题不合适有关,我似乎无法找到明确的标题 回答什么是合规的标题&我已经阅读过Gmail的类似问题。 模拟电子邮件已被替换为这篇文章。

任何帮助将不胜感激

我的完整代码如下:

    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)

2 个答案:

答案 0 :(得分:3)

以下适用于Python 2.7和Python 3.2上的microsoft,google,yahoo帐户:

#!/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()

答案 1 :(得分:1)

在登录之前将以下内容添加到您的代码中,然后重试;

try:
        self.smtp.ehlo()
        self.smtp.starttls()
        self.smtp.ehlo
except:
        print "No TLS :("

#do login here