#!/usr/bin/python
import smtplib
sender = 'from@fromdomain.com'
receivers = ['to@todomain.com']
message = """From: From Person <from@fromdomain.com>
To: To Person <TEST@yahoo.com>
Subject: SMTP e-mail test
This is a test e-mail message.
"""
try:
smtpObj = smtplib.SMTP('localhost')
smtpObj.sendmail(sender, receivers, message)
print "Successfully sent email"
except SMTPException:
print "Error: unable to send email"
即使我导入所有内容,我仍然会收到以下错误。我正在使用Linux,缺少什么?
File "email.py", line 3, in <module>
import smtplib
File "/usr/lib/python2.7/smtplib.py", line 46, in <module>
import email.utils
File "/home/email.py", line 19, in <module>
except SMTPException:
答案 0 :(得分:7)
唯一不起作用的显而易见的事情是SMTPException
需要smtplib.SMTPException
(或导入它以便from smtplib import SMTPException
无限制地使用。)
否则,在更改为我自己的(有效)地址和我自己的SMTP服务器后,您的代码工作正常。