如何使用Python向公司电子邮件帐户发送电子邮件?

时间:2014-02-05 01:14:26

标签: python email smtp

我有一个公司电子邮件帐户。现在,我需要使用Python向公司内的公司电子邮件帐户发送电子邮件。我们在公司内部使用Microsoft Exchange和Outlook。

我尝试使用以下代码,但它对我不起作用,我总是得到SOCKET error

#!/usr/bin/python

import smtplib
from smtplib import SMTPException

message = """From: david@company.com
To: ron@company.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"

以下是我得到的错误 -

socket.error: [Errno 111] Connection refused

这里我应该替换localhost或者我应该使用SMTP来做这个或其他什么事情?

1 个答案:

答案 0 :(得分:0)

localhost必须替换为smtp邮件服务器地址。每个企业环境都应该有一个smtp主机。任何IT管理员都应该能够指出它。它看起来类似于'smtphost.corpabc.com'