【发布时间】:2016-04-09 01:46:21
【问题描述】:
我正在使用以下代码从本地主机中的 python 程序发送电子邮件,
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
me = "tonyr1291@gmail.com"
you = "testaccount@gmail.com"
msg = MIMEMultipart('alternative')
msg['Subject'] = "Link"
msg['From'] = me
msg['To'] = you
text = "Hi!\nHow are you?\nHere is the link you wanted:\nhttp://www.python.org"
html = """\
<html>
<head></head>
<body>
<p>Hi!<br>
How are you?<br>
Here is the <a href="http://www.python.org">link</a> you wanted.
</p>
</body>
</html>
"""
part1 = MIMEText(text, 'plain')
part2 = MIMEText(html, 'html')
msg.attach(part1)
msg.attach(part2)
s = smtplib.SMTP('localhost',5000)
s.sendmail(me, you, msg.as_string())
s.quit()
此代码来自 python 文档。
当我运行这段代码时,它一直在运行,但没有发送电子邮件。
我想知道,除了这段代码,我是否需要在其他任何地方进行一些其他配置。
我没有看到任何错误。
我正在使用python 2.7
这是Sending HTML email using Python中的解决方案
【问题讨论】:
-
您的代码正在尝试使用 SMTP 服务器在 localhost 的 5000 端口上发送电子邮件。
-
@TimothéeJeannin 抱歉,我对这一切都很陌生,你能告诉我该怎么做吗?