简体   繁体   English

python - 通过smtplib错误在本地发送电子邮件

[英]python - send email locally through smtplib error

I'm trying to send an email using smtplib without storing smtp credentials locally.我正在尝试使用 smtplib 发送电子邮件而不在本地存储 smtp 凭据。 I found this article https://www.tutorialspoint.com/python/python_sending_email.htm but get an error.我发现这篇文章https://www.tutorialspoint.com/python/python_sending_email.htm但得到一个错误。 I've fairly new with smtplib.我对 smtplib 相当陌生。 Any help would be appreciated.任何帮助,将不胜感激。

#!/usr/bin/python

import smtplib

message = """From: From Person <from@fromdomain.com>
To: To Person <to@todomain.com>
MIME-Version: 1.0
Content-type: text/html
Subject: SMTP HTML e-mail test

This is an e-mail message to be sent in HTML format

<b>This is HTML message.</b>
<h1>This is headline.</h1>
"""

try:
   smtpObj = smtplib.SMTP('localhost')
   smtpObj.sendmail(sender, receivers, message)         
   print "Successfully sent email"
except SMTPException:
   print "Error: unable to send email"

error错误

NameError: name 'SMTPException' is not defined

SMTPException is in the module smtplib. SMTPException位于 smtplib 模块中。 So you either need to import that name, or use smtplib.SMTPException所以你要么需要导入该名称,要么使用smtplib.SMTPException

你应该做那个 smtplib.SMTPException 或者做这行代码从 smtplib import SMTPException 导入它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM