如何解决Python的SMTP AUTH扩展问题
| 我正在尝试编写一个简单的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扩展名”错误?
附注:我知道SMTP详细信息和凭据正确无误,因为我有一个工作正常的Java类,其中包含确切的详细信息。
解决方法
您收到的错误意味着您正在与之交谈的SMTP服务器不声称支持身份验证。如果查看调试输出,您将发现对ѭ2的任何响应都不包含对ѭ3的必要声明。如果它(正确地)支持身份验证,则响应之一将是:
250 AUTH GSSAPI DIGEST-MD5 PLAIN
(至少是在STARTTLS
之后对EHLO
的响应。)由于未包含该响应,因此smtplib假定服务器将无法处理AUTH
命令,并将拒绝发送该命令。如果您确定SMTP服务器即使不发布AUTH
命令,也可以支持AUTH
命令,则可以通过将smtplib显式地添加到功能集中,从而使smtplib轻松支持AUTH
命令。您需要知道支持哪种身份验证方案,然后您可以执行以下操作:
smtp.starttls()
smtp.ehlo()
# Pretend the SMTP server supports some forms of authentication.
smtp.esmtp_features[\'auth\'] = \'LOGIN DIGEST-MD5 PLAIN\'
...但是当然要使SMTP服务器按照规范运行是一个更好的主意:)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。