gpt4 book ai didi

python - 使用python smtplib发送邮件错误

转载 作者:太空狗 更新时间:2023-10-30 02:46:35 24 4
gpt4 key购买 nike

我正在尝试使用 python 3.2 SMTPlib.sendmail() 函数在对 SMTP 库(即注释掉抑制错误消息的 rset() 函数)我设法从服务器检索到以下错误消息:

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

雅虎邮件 SMTP 服务器认为我在发送垃圾邮件,该 URL 确实链接到任何东西有用。我认为这与 header 不足有关,我似乎找不到明确的回答什么是合规 header &我读过 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)

最佳答案

以下适用于 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()

关于python - 使用python smtplib发送邮件错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19606778/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com