用Python的smtplib发送电子邮件如何获得适当的登录凭据?

2024-06-28 14:36:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我在我的大学里有一个类似域名的电子邮件帐户。我有一个网络界面(cpanel)来登录和查看我的邮件。但是,我想写一个python脚本来使用我的大学帐户发送电子邮件。在

当我使用cPanel登录时:

Web address: infromatics.uniname.ny.us
Login: bbrown

我可以成功登录我的帐户。但是,当我尝试:

^{pr2}$

我知道我的登录尝试失败了。在

所以,我的SMTP请求也有问题,至少我不知道如何正确处理它。请注意,使用下面的代码,我使用gmail帐户而不是大学帐户没有问题,所以我认为代码很好。在

我试着用https://pingability.com/smtptest.jsp检查是否正确,其输出如下:

1。具有以下证书:

SMTP Server: unismtpserv.uniname.ny.us
From Email:smtptester@pingability.com  
SMTP Username: bbrown@infromatics.uniname.ny.us
SMTP Password: secretpassword

我有输出:

DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "unismtpserv.uniname.ny.us", port 25, isSSL false
220-unismtpserv.uniname.ny.us ESMTP Exim 4.69 #1 Sun, 03 May 2015 11:49:25 +0200 
220-We do not authorize the use of this system to transport unsolicited, 
220 and/or bulk e-mail.
DEBUG SMTP: connected to host "unismtpserv.uniname.ny.us", port: 25

EHLO localhost
250-unismtpserv.uniname.ny.us Hello pingability.com [x.x.x.x]
250-SIZE 78643200
250-PIPELINING
250-AUTH PLAIN LOGIN
250-STARTTLS
250 HELP
DEBUG SMTP: Found extension "SIZE", arg "78643200"
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "AUTH", arg "PLAIN LOGIN"
DEBUG SMTP: Found extension "STARTTLS", arg ""
DEBUG SMTP: Found extension "HELP", arg ""
DEBUG SMTP: Attempt to authenticate
DEBUG SMTP: check mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM 
AUTH LOGIN
334 VXNlcm5hbWU6
YWlAaW5mb3JtYXRpY2EudW1jcy5sdWJsaW4ucGw=
334 UGFzc3dvcmQ6
YW5uYWxlc3VtY3MyMDE1
535 Incorrect authentication data

Authentication Failed

然而,2。具有以下证书:

SMTP Server: unismtpserv.uniname.ny.us
From Email:smtptester@pingability.com  
SMTP Username: bbrow
SMTP Password: secretpassword

一切似乎都很好:

DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "unismtpserv.uniname.ny.us", port 25, isSSL false
220-unismtpserv.uniname.ny.us ESMTP Exim 4.69 #1 Sun, 03 May 2015 11:53:54 +0200 
220-We do not authorize the use of this system to transport unsolicited, 
220 and/or bulk e-mail.
DEBUG SMTP: connected to host "unismtpserv.uniname.ny.us", port: 25

EHLO localhost
250-unismtpserv.uniname.ny.us Hello pingability.com [x.x.x.x]
250-SIZE 78643200
250-PIPELINING
250-AUTH PLAIN LOGIN
250-STARTTLS
250 HELP
DEBUG SMTP: Found extension "SIZE", arg "78643200"
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "AUTH", arg "PLAIN LOGIN"
DEBUG SMTP: Found extension "STARTTLS", arg ""
DEBUG SMTP: Found extension "HELP", arg ""
DEBUG SMTP: Attempt to authenticate
DEBUG SMTP: check mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM 
AUTH LOGIN
334 VXNlcm5hbWU6
YWk=
334 UGFzc3dvcmQ6
YW5uYWxlc3VtY3MyMDE1
235 Authentication succeeded
DEBUG SMTP: use8bit false
MAIL FROM:<smtptester@pingability.com>
250 OK
RCPT TO:<smtptester@pingability.com>
250 Accepted
DEBUG SMTP: Verified Addresses
DEBUG SMTP:   smtptester@pingability.com
DATA
354 Enter message, ending with "." on a line by itself
Date: Sun, 3 May 2015 09:53:54 +0000 (UTC)
From: smtptester@pingability.com
To: smtptester@pingability.com
Message-ID: <9543385.63328.1430646834215.JavaMail.tomcat@localhost>
Subject: Pingability Test
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit

Pingability Test
.
250 OK id=1Yoqay-0006kA-19
QUIT
221 unismtpserv.uniname.ny.us closing connection

但是,使用以下代码:

import smtplib
import string

fromaddr = 'bbrown@infromatics.uniname.ny.us'
password = 'secretpassword'
toaddrs  = 'myfriend@gmail.com'
server_smtp = 'unismtpserv.uniname.ny.us'
port_smtp = 465


msg = 'Test message ^^'
BODY = string.join((
        "From: %s" % fromaddr,
        "To: %s" % toaddrs,
        "Subject: %s" % 'Hello!!!' ,
        "",
        'What\'s up? :)'
        ), "\r\n")

try :

    server = smtplib.SMTP_SSL(host=server_smtp, port=port_smtp)
    server.set_debuglevel(True)
    server.ehlo()
    server.starttls()
    server.ehlo()
    server.esmtp_features['auth'] = 'LOGIN PLAIN'
    server.login('bbrown', password)
    server.sendmail(fromaddr, toaddrs, str(BODY))
    server.quit()

except smtplib.SMTPServerDisconnected :
    print "smtplib.SMTPServerDisconnected"
except smtplib.SMTPResponseException, e:
    print "smtplib.SMTPResponseException: " + str(e.smtp_code) + " " + str(e.smtp_error)
except smtplib.SMTPSenderRefused:
    print "smtplib.SMTPSenderRefused"
except smtplib.SMTPRecipientsRefused:
    print "smtplib.SMTPRecipientsRefused"
except smtplib.SMTPDataError:
    print "smtplib.SMTPDataError"
except smtplib.SMTPConnectError:
    print "smtplib.SMTPConnectError"
except smtplib.SMTPHeloError:
    print "smtplib.SMTPHeloError"
except smtplib.SMTPAuthenticationError:
    print "smtplib.SMTPAuthenticationError"
except Exception :
    print "Exception"

我有这样的输出:

send: 'ehlo [127.0.1.1]\r\n'
reply: '250-unismtpserv.uniname.ny.us Hello [127.0.1.1] [x.x.x.x]\r\n'
reply: '250-SIZE 78643200\r\n'
reply: '250-PIPELINING\r\n'
reply: '250-AUTH PLAIN LOGIN\r\n'
reply: '250 HELP\r\n'
reply: retcode (250); Msg: unismtpserv.uniname.ny.us Hello [127.0.1.1] [x.x.x.x]
SIZE 78643200
PIPELINING
AUTH PLAIN LOGIN
HELP
Exception

怎么处理?在

====================================================================

更新:

谢谢@Glueon的帮助!打印出例外情况后我发现:

Exception STARTTLS extension not supported by server.
Traceback (most recent call last):
  File "umcstest_ai.py", line 32, in <module>
    server.starttls()
  File "/usr/lib/python2.7/smtplib.py", line 637, in starttls
    raise SMTPException("STARTTLS extension not supported by server.")
SMTPException: STARTTLS extension not supported by server.

<class 'smtplib.SMTPException'>

所以我编辑了我的代码:

import smtplib
import string
import traceback
import sys

fromaddr = 'bbrown@infromatics.uniname.ny.us'
password = 'secretpassword'
toaddrs  = 'myfriend@gmail.com'
server_smtp = 'unismtpserv.uniname.ny.us'
port_smtp = 465

msg = 'Test message ^^'
BODY = string.join((
        "From: %s" % fromaddr,
        "To: %s" % toaddrs,
        "Subject: %s" % 'Hello!!!' ,
        "",
        'What\'s up? :)'
        ), "\r\n")

try :


    server = smtplib.SMTP_SSL(host=server_smtp, port=port_smtp)
    server.set_debuglevel(True)
    server.esmtp_features['auth'] = 'LOGIN PLAIN'
    server.login('bbrown', password)
    server.sendmail(fromaddr, toaddrs, str(BODY))
    server.quit()

except smtplib.SMTPServerDisconnected :
    print "smtplib.SMTPServerDisconnected"
except smtplib.SMTPResponseException, e:
    print "smtplib.SMTPResponseException: " + str(e.smtp_code) + " " + str(e.smtp_error)
except smtplib.SMTPSenderRefused:
    print "smtplib.SMTPSenderRefused"
except smtplib.SMTPRecipientsRefused:
    print "smtplib.SMTPRecipientsRefused"
except smtplib.SMTPDataError:
    print "smtplib.SMTPDataError"
except smtplib.SMTPConnectError:
    print "smtplib.SMTPConnectError"
except smtplib.SMTPHeloError:
    print "smtplib.SMTPHeloError"
except smtplib.SMTPAuthenticationError:
    print "smtplib.SMTPAuthenticationError"
except Exception, e :
    print "Exception", e
    print traceback.format_exc()
    print sys.exc_info()[0]

邮件已经发出了!就在我朋友的邮箱里,非常感谢你的帮助!:)

再问一个问题:为什么smtpping页面成功地使用了25端口,而当我尝试这样做时,我的尝试失败了?在


Tags: debugcomserverextensionloginsmtpsmtplibus
1条回答
网友
1楼 · 发布于 2024-06-28 14:36:09

如果我没弄错的话,你试着在465和25个端口上使用相同的代码。在

通常25端口同时用于纯模式和加密模式。第二个命令在客户端发出starttls命令时使用。在

虽然465端口只配置了SSL,这就是为什么您尝试执行连接到465端口的starttls时出错。服务器不公布此命令。在

相关问题 更多 >