当尝试在 Python 2.6.5 中使用smtplib通过 Outlook 2010 发送邮件时,我至少遇到成功发送邮件,但s.sendmail(FROM, TO, message)没有将信息放入 From:, To:, 和主题:给我的线条。
我需要知道如何在 Outlook 2010 中正确使用 smtplib 发送电子邮件,以便收到电子邮件时填写邮件上的 From:、To: 和 Subject: 字段。
这是我的代码。我用谷歌搜索找到这个框架:
import smtplib
SERVER = 'mail.company.com'
FROM = 'jdoe@company.com'
TO = ['receiver1@company.com']
SUBJECT = "Test Subject SMTP"
TEXT = "If this is in the body of the email, test is a success!"
message = """From: %s\r\nTo: %s\r\nSubject: %s\r\n
%s
""" % (FROM, ", ".join(TO), SUBJECT, TEXT)
try:
s = smtplib.SMTP(SERVER)
s.sendmail(FROM, TO, message) # this line is not correctly putting info in proper fields for Outlook 2010
s.quit()
print "Successfully sent email."
except:
import sys, traceback
tb = sys.exc_info()[2]
print "An error occurred on line " + str(tb.tb_lineno)
print "Error: unable to send email"
输出是电子邮件已成功发送。它是。只是不太对。