问题
When trying to send mail through Outlook 2010 with smtplib in Python 2.6.5, I at least encounter success sending the message, but s.sendmail(FROM, TO, message) is not putting the information in the From:, To:, and Subject: lines for me.
I need to know how to send email using smtplib in Outlook 2010 properly so that the email is received with the From:, To:, and Subject: fields on the message filled in.
Here is my code. I googled to find this framework:
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"
Output is that the email is successfully sent. And it is. Just not quite right.
回答1:
Try s.set_debuglevel(1) to check what is going on.
The code looks ok.
You may be rejected on SMTP:
smtplib.SMTPRecipientsRefused: {'example@amail.com': (550, b'5.7.1 Unable to relay')}
来源:https://stackoverflow.com/questions/18273198/outlook-2010-and-python-email-smtplib-info-in-proper-fields