Skip to main content
 首页 » 编程设计

python之python SMTP 未收到邮件

2024年08月27日35mengfanrong

你好,我正在尝试让 python 3 从 Ubuntu 发送一封简单的电子邮件。

我启动了一个简单的 smpt 服务器: python -m smtpd -n -c DebuggingServer localhost:1025

以下是我的电子邮件服务器的代码:

import smtplib 
 
message = """ 
Hello 
""" 
sender = "dancbtalk@yahoo.com" 
receivers=["dancbtalk@yahoo.com"] 
try: 
   smtpObj = smtplib.SMTP('localhost', 1025) 
   smtpObj.sendmail(sender, receivers, message)          
   print "Successfully sent email" 
except smtplib.SMTPException: 
   print "Error: unable to send email" 

我的输出显示电子邮件已成功发送,但当我实际检查该电子邮件帐户时,它什么也没收到。我已经用几个电子邮件帐户尝试过。

请您参考如下方法:

您的邮件没有任何 header 。或者更准确地说,您的消息仅 header ,其中没有一个会被识别为有效。至少您可能想要添加主题、发件人和收件人标题。例如

sender    = "dancbtalk@yahoo.com" 
receivers = ["dancbtalk@yahoo.com"] 
 
headers = f"""From: {sender} 
To: {", ".join(receivers)} 
Subject: Hello 
""" 
 
message = headers + "\n" + """ 
Hello 
"""