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

标签 python smtp smtplib

我在我的大学有一个类似域的电子邮件帐户。我有一个网络界面 (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 帐户而不是大学帐户时没有问题,所以我认为代码很好。

我尝试使用 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]

邮件已发送!就在我 friend 的邮箱里,非常非常感谢您的帮助! :)

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

最佳答案

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

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

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

关于python - 使用 Python 的 smtplib 发送电子邮件 - 如何获得适当的登录凭据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30012563/

相关文章:

python - 可以使用字符串列创建稀疏的 Pandas DataFrame 吗?

python - 我可以将数据暂存到内存中选择,然后选择回滚或在sqlite3中提交吗? python 2.7

node.js - Mandrill 返回 reject_reason : 'invalid-sender'

java - 无法使用Java程序发送邮件到gmail

python - smtplib、unicode 和 OpenERP 核心的问题

python - 插入空标签时如何防止 Tkinter labelframe 大小发生变化

python /docker : FileNotFoundError: [Errno 2] No such file or directory:

php - fsockopen() : unable to connect not work with PHP (Connection timed out)

python - 使用 smtplib 发送电子邮件不再起作用

python - 无法在使用 python smtplib/email 发送的 outlook 2013 电子邮件中显示嵌入图像