使用python SMTP不收到邮件 [英] mail not being received with python SMTP

查看:314
本文介绍了使用python SMTP不收到邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我开始使用一个简单的smpt服务器:
python -m smtpd - n -c DebuggingServer localhost:1025



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

  import smtplib 

message =
Hello

sender =dancbtalk@yahoo.com
receiver = [dancbtalk@yahoo.com]
try:
smtpObj = smtplib.SMTP('localhost',1025)
smtpObj.sendmail(发件人,接收者,消息)
打印成功发送电子邮件
除了smtplib.SMTPException:
打印错误:无法发送电子邮件

我的输出表示电子邮件已成功发送,但是当我实际检查该电子邮件帐户时,它没有收到任何内容。我已经尝试了几个电子邮件帐户。

解决方案

您的邮件没有标题。或者更准确地说,您的消息仅包含 标题,其中没有一个将被识别为有效。至少你可能想添加主题,从和标题。

$ b

  sender =dancbtalk@yahoo.com
recipients = [dancbtalk@yahoo.com]

headers =From:%s
至:%s
主题:Hello
%(sender,,.join(recipients)

message = headers +\\\
+
您好


Hello I am trying to make python 3 send a simple email from Ubuntu.

I started a simple smpt server with: python -m smtpd -n -c DebuggingServer localhost:1025

The following is the code for my email server:

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"

My output says that the email is send successfully but when I actually check that email account, it has received nothing. I have tried this with several email accounts.

解决方案

Your message does not have any headers. Or more precisely, your message contains only headers, none of which will be recognized as valid. At the very least you probably want to add Subject, From, and To headers. E.g.

sender    = "dancbtalk@yahoo.com"
receivers = ["dancbtalk@yahoo.com"]

headers = """From: %s
To: %s
Subject: Hello
""" % (sender, ", ".join(receivers)

message = headers + "\n" + """
Hello
"""

这篇关于使用python SMTP不收到邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆