【问题标题】:python - send email locally through smtplib errorpython - 通过 smtplib 错误在本地发送电子邮件
【发布时间】:2021-12-01 05:15:33
【问题描述】:

我正在尝试使用 smtplib 发送电子邮件,而不在本地存储 smtp 凭据。我找到了这篇文章https://www.tutorialspoint.com/python/python_sending_email.htm,但出现错误。我对 smtplib 相当陌生。任何帮助将不胜感激。

#!/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"

错误

NameError: name 'SMTPException' is not defined

【问题讨论】:

标签: python error-handling smtplib


【解决方案1】:

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

【讨论】:

  • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
【解决方案2】:

SMTPException 在模块 smtplib 中。所以你要么需要导入那个名字,要么使用smtplib.SMTPException

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-26
    • 1970-01-01
    • 2023-02-11
    • 2017-10-31
    • 2016-11-04
    • 2018-09-23
    • 2016-12-16
    • 2019-12-04
    相关资源
    最近更新 更多