SMTP AUTH扩展问题与Python
我正在尝试编写一个简单的Python脚本,以通过我公司的SMTP服务器发送电子邮件。我正在使用以下代码。
#! /usr/local/bin/python
import sys,re,os,datetime
from smtplib import SMTP
#Email function
def sendEmail(message):
sender="SENDERID@COMPANY.com"
receivers=['REVEIVER1@COMPANY.com','RECEIVER2@COMPANY.com']
subject="Daily Report - " + datetime.datetime.now().strftime("%d %b %y")
header="""\
From: %s
To: %s
Subject: %s
%s""" % (sender, ", ".join(receivers), subject, message)
smtp = SMTP()
smtp.set_debuglevel(1)
smtp.connect('X.X.X.X')
smtp.ehlo()
smtp.starttls()
smtp.ehlo()
try:
smtp.login('SENDERID@COMPANY.com', '********')
smtp.sendmail(sender,receivers,header)
smtp.quit()
except Exception, e:
print e
#MAIN
sendEmail("HAHHAHAHAHAH!!!")
运行该程序将产生此结果。
connect: ('X.X.X.X', 25)
connect: ('X.X.X.X', 25)
reply: '220 COMPANY.com [ESMTP Server] service ready;ESMTP Server; 05/25/11 15:59:27\r\n'
reply: retcode (220); Msg: COMPANY.com [ESMTP Server] service ready;ESMTP Server; 05/25/11 15:59:27
connect: COMPANY.com [ESMTP Server] service ready;ESMTP Server; 05/25/11 15:59:27
send: 'ehlo SERVER1.COMPANY.com\r\n'
reply: '250-COMPANY.com\r\n'
reply: '250-SIZE 15728640\r\n'
reply: '250-8BITMIME\r\n'
reply: '250 STARTTLS\r\n'
reply: retcode (250); Msg: COMPANY.com
SIZE 15728640
8BITMIME
STARTTLS
send: 'STARTTLS\r\n'
reply: '220 Ready to start TLS\r\n'
reply: retcode (220); Msg: Ready to start TLS
send: 'ehlo SERVER2.COMPANY.com\r\n'
reply: '250-COMPANY.com\r\n'
reply: '250-SIZE 15728640\r\n'
reply: '250 8BITMIME\r\n'
reply: retcode (250); Msg: COMPANY.com
SIZE 15728640
8BITMIME
send: 'quit\r\n'
reply: '221 [ESMTP Server] service closing transmission channel\r\n'
reply: retcode (221); Msg: [ESMTP Server] service closing transmission channel
ERROR: Could not send email! Check the reason below.
SMTP AUTH extension not supported by server.
如何开始调试此“服务器不支持的SMTP AUTH扩展名”。错误?
PS:我知道SMTP详细信息和凭据是正确的,因为我有一个工作正常的Java类,具有确切的详细信息。
-
您收到的错误意味着正在与之交谈的SMTP服务器不声称支持身份验证。如果您查看调试输出,将会发现对的响应均不
EHLO
包含的必要声明AUTH
。如果它(正确地)支持身份验证,则响应之一将是:250 AUTH GSSAPI DIGEST-MD5 PLAIN
(至少是
EHLO
对STARTTLS
。之后的响应。)由于未包含该响应,因此smtplib假定服务器将无法处理该AUTH
命令,并将拒绝发送该命令。如果确定SMTP服务器AUTH
即使不发布该命令也支持该命令,则可以通过将SMTP服务器显式添加到功能集中
来 暗中说服smtplib支持该命令AUTH
。您需要知道支持哪种身份验证方案,然后可以执行以下操作:smtp.starttls() smtp.ehlo() # Pretend the SMTP server supports some forms of authentication. smtp.esmtp_features['auth'] = 'LOGIN DIGEST-MD5 PLAIN'
…但是当然要使SMTP服务器按照规范运行是一个更好的主意:)