gpt4 book ai didi

python - 使用 Python 示例发送电子邮件失败

转载 作者:IT老高 更新时间:2023-10-28 20:36:57 25 4
gpt4 key购买 nike

我一直在尝试(但失败了)弄清楚如何通过 Python 发送电子邮件。

从这里尝试示例: http://docs.python.org/library/smtplib.html#smtplib.SMTP

但在我收到关于没有 SSL 连接的反弹后添加了 server = smtplib.SMTP_SSL('smtp.gmail.com', 465) 行。

现在我得到了这个:

Traceback (most recent call last):
File "C:/Python26/08_emailconnects/12_29_EmailSendExample_NotWorkingYet.py", line 37, in <module>
server = smtplib.SMTP('smtp.gmail.com', 65)
File "C:\Python26\lib\smtplib.py", line 239, in __init__
(code, msg) = self.connect(host, port)
File "C:\Python26\lib\smtplib.py", line 295, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "C:\Python26\lib\smtplib.py", line 273, in _get_socket
return socket.create_connection((port, host), timeout)
File "C:\Python26\lib\socket.py", line 512, in create_connection
raise error, msg
error: [Errno 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
>>>

想法?


server = smtplib.SMTP("smtp.google.com", 495) 给我一个超时错误。只是 smtplib.smtp("smtp.google.com", 495) 给了我“SSLError: [Errno 1] _ssl.c:480: error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol”(见下文)。

我正在尝试不同的端口,但现在我遇到了一个全新的错误。我只是发布整个代码,我可能会犯一些菜鸟错误。

"

import smtplib

mailuser = 'MYEMAIL@gmail.com'

mailpasswd = 'MYPASSWORD'

fromaddr = 'MYEMAIL@gmail.com'

toaddrs = 'MYEMAIL2@gmail.com'

msg = 'Hooooorah!'

print msg

server = smtplib.SMTP_SSL('smtp.google.com')

server = smtplib.SMTP_SSL_PORT=587

server.user(mailuser)

server.pass_(mailpasswd)

server.set_debuglevel(1)

server.sendmail(fromaddr, toaddrs, msg)

server.quit()

"

然后我收到以下错误消息:“

Traceback (most recent call last):
File "C:/Python26/08_emailconnects/12_29_eMAILSendtryin_stripped.py", line 16, in <module>
server = smtplib.SMTP_SSL('smtp.google.com')
File "C:\Python26\lib\smtplib.py", line 749, in __init__
SMTP.__init__(self, host, port, local_hostname, timeout)
File "C:\Python26\lib\smtplib.py", line 239, in __init__
(code, msg) = self.connect(host, port)
File "C:\Python26\lib\smtplib.py", line 295, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "C:\Python26\lib\smtplib.py", line 755, in _get_socket
self.sock = ssl.wrap_socket(self.sock, self.keyfile, self.certfile)
File "C:\Python26\lib\ssl.py", line 350, in wrap_socket
suppress_ragged_eofs=suppress_ragged_eofs)
File "C:\Python26\lib\ssl.py", line 118, in __init__
self.do_handshake()
File "C:\Python26\lib\ssl.py", line 293, in do_handshake
self._sslobj.do_handshake()
SSLError: [Errno 1] _ssl.c:480: error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol

"

请注意,实际上看起来像“server = smtplib.SMTPSSLPORT=587”实际上是“server = smtplib.SMTP underscore SSL underscore PORT=587”,有这里正在进行某种格式设置。

最佳答案

以下代码适用于我:

import smtplib

FROMADDR = "my.real.address@gmail.com"
LOGIN = FROMADDR
PASSWORD = "my.real.password"
TOADDRS = ["my.real.address@gmail.com"]
SUBJECT = "Test"

msg = ("From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n"
% (FROMADDR, ", ".join(TOADDRS), SUBJECT) )
msg += "some text\r\n"

server = smtplib.SMTP('smtp.gmail.com', 587)
server.set_debuglevel(1)
server.ehlo()
server.starttls()
server.login(LOGIN, PASSWORD)
server.sendmail(FROMADDR, TOADDRS, msg)
server.quit()

我使用的是 Python 2.5.2。

编辑:按照 ΤZΩΤZΙΟΥ 的建议将端口从 25 更改为 587,并删除了第二个 ehlo()。现在我很想知道为什么端口 25 在我的机器上完美工作(而端口 465不是)。

关于python - 使用 Python 示例发送电子邮件失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/399129/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com