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

使用Python'发送电子邮件;s smtplib-如何获得适当的登录凭据?,python,smtp,smtplib,Python,Smtp,Smtplib,我在我的大学里有一个类似域名的电子邮件帐户。我有一个web界面(cpanel)来登录和检查我的邮件。然而,我想写一个python脚本来使用我的大学帐户发送电子邮件 当我使用cPanel登录时: Web address: infromatics.uniname.ny.us Login: bbrown 我可以成功登录到我的帐户。但是,当我尝试时: Web address: infromatics.uniname.ny.us Login: bbrown@infromatics.uniname.ny.

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

当我使用cPanel登录时:

Web address: infromatics.uniname.ny.us
Login: bbrown
我可以成功登录到我的帐户。但是,当我尝试时:

Web address: infromatics.uniname.ny.us
Login: bbrown@infromatics.uniname.ny.us
我有信息表明我的登录尝试失败

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

我试图检查我是否正确使用,以下是它的输出:

1。具有以下凭据:

SMTP Server: unismtpserv.uniname.ny.us
From Email:smtptester@pingability.com  
SMTP Username: bbrown@infromatics.uniname.ny.us
SMTP Password: secretpassword
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: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: bbrown@infromatics.uniname.ny.us
SMTP Password: secretpassword
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
我有这样的产出:

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'>
电子邮件已经发送了!就在我朋友的邮箱里,非常非常感谢你的帮助!:)


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

如果我理解正确,您尝试在465和25端口上使用相同的代码

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


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

这不是打字错误吗
Server.login('bbrown',密码)
?硬编码登录。@Glueon:谢谢,用
bbrown@infromatics.uniname.ny.us
但输出是相同的,任何更改都请打印实际异常,而不仅仅是“异常”。因为我看到它的结尾写着“例外”,但到底错在哪里还不清楚。据我所知,
starttls
必须在调用
ehlo
之前执行。您成功的测试使用了端口25。您的代码使用
端口\u smtp=465
。如果尝试
端口\u smtp=25
,会发生什么情况?是的,这可能是问题所在,因为465可能只是SSL,不支持STARTTLS命令。