我正在尝试编写一个python代码,它将向我的电子邮件帐户发送错误消息。我使用 smtplib 从本地主机向我的 gmail 帐户发送消息。我正在尝试在 jupyter 笔记本中执行以下操作:
import smtplib
SERVER = "localhost"
LOGIN = "sourabh@localhost.com"
PASSWORD = "sourabh"
FROM = "sourabh@localhost.com"
TO = ["sourabhjbp@gmail.com"] # must be a list
SUBJECT = "Hello!"
TEXT = "This message was sent with Python's smtplib."
# Prepare actual message
message = """
From: %s
To: %s
Subject: %s
%s
""" % (FROM, ", ".join(TO), SUBJECT, TEXT)
server = smtplib.SMTP(SERVER,port=25)
server.set_debuglevel(1)
server.login(LOGIN, PASSWORD)
server.sendmail(FROM, TO, message)
server.quit()
它以消息"(221,'再见')"退出。以下是完整的调试日志,
send: 'ehlo [192.168.56.1]rn'
reply: '250-localhostrn'
reply: '250-SIZE 20480000rn'
reply: '250-AUTH LOGIN PLAINrn'
reply: '250 HELPrn'
reply: retcode (250); Msg: localhost
SIZE 20480000
AUTH LOGIN PLAIN
HELP
send: 'AUTH PLAIN AHNvdXJhYmhAbG9jYWxob3N0LmNvbQBzb3VyYWJorn'
reply: '235 authenticated.rn'
reply: retcode (235); Msg: authenticated.
send: 'mail FROM:<sourabh@localhost.com> size=115rn'
reply: '250 OKrn'
reply: retcode (250); Msg: OK
send: 'rcpt TO:<sourabhjbp@gmail.com>rn'
reply: '250 OKrn'
reply: retcode (250); Msg: OK
send: 'datarn'
reply: '354 OK, send.rn'
reply: retcode (354); Msg: OK, send.
data: (354, 'OK, send.')
send: "From: sourabh@localhost.comrnTo: sourabhjbp@gmail.comrnSubject: Hello!rnrnThis message was sent with Python's smtplib.rn.rn"
reply: '250 Queued (0.032 seconds)rn'
reply: retcode (250); Msg: Queued (0.032 seconds)
data: (250, 'Queued (0.032 seconds)')
send: 'quitrn'
reply: '221 goodbyern'
reply: retcode (221); Msg: goodbye
行回复: 重新编码 (250);消息:已排队(0.032 秒)数据:(250,"排队(0.032 秒)")
表示hMailServer已成功接受您的邮件:
您现在应该在hMailServer DATA文件夹的根目录中看到一个新的*.eml文件(名称类似于 {3F20B09B-2A35-4A16-A7E9-A510ea0F5eae.eml} 的文件其中包含您通过 Python 发送的消息。它将自动删除如果您的电子邮件已到达收件人。
有关详细信息,请运行 hMailAdmin,转到设置并启用所有日志记录复选框并重新启动hMailServer Windows Service/Deamon(或从提升的Windows命令提示符运行 net hmailserver stop & net hmailserver start。
PS:服务重新启动后,需要关闭hMailAdmin(或任何其他COM客户端)并重新启动(只需退出hMailAdmin并重新运行它)。