使用 Python 示例发送电子邮件失败 [英] Failing to send email with the Python example

查看:81
本文介绍了使用 Python 示例发送电子邮件失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试(但没有成功)弄清楚如何通过 Python 发送电子邮件.

I've been trying (and failing) to figure out how to send email via Python.

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

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

but added the line server = smtplib.SMTP_SSL('smtp.gmail.com', 465) after I got a bounceback about not having an SSL connection.

现在我明白了:

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:Python26libsmtplib.py", line 239, in __init__
    (code, msg) = self.connect(host, port)
  File "C:Python26libsmtplib.py", line 295, in connect
    self.sock = self._get_socket(host, port, self.timeout)
  File "C:Python26libsmtplib.py", line 273, in _get_socket
    return socket.create_connection((port, host), timeout)
  File "C:Python26libsocket.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:SSLroutines:SSL23_GET_SERVER_HELLO:unknown protocol"(见下文).

server = smtplib.SMTP("smtp.google.com", 495) gives me a timeout error. just smtplib.smtp("smtp.google.com", 495) gives me "SSLError: [Errno 1] _ssl.c:480: error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol" (see below).

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

I'm trying different ports and now I'm getting a completely new error. I'll just post the whole bit of code, I'm probably making some rookie mistake.

"

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()

"

然后我收到此错误消息:

and then I get this error message: "

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:Python26libsmtplib.py", line 749, in __init__
    SMTP.__init__(self, host, port, local_hostname, timeout)
  File "C:Python26libsmtplib.py", line 239, in __init__
    (code, msg) = self.connect(host, port)
  File "C:Python26libsmtplib.py", line 295, in connect
    self.sock = self._get_socket(host, port, self.timeout)
  File "C:Python26libsmtplib.py", line 755, in _get_socket
    self.sock = ssl.wrap_socket(self.sock, self.keyfile, self.certfile)
  File "C:Python26libssl.py", line 350, in wrap_socket
    suppress_ragged_eofs=suppress_ragged_eofs)
  File "C:Python26libssl.py", line 118, in __init__
    self.do_handshake()
  File "C:Python26libssl.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",还有这里正在进行某种格式化.

note that actually the which looks like "server = smtplib.SMTPSSLPORT=587" is actually "server = smtplib.SMTP underscore SSL underscore PORT=587", there's some sort of formatting thing going on here.

推荐答案

以下代码对我有用:

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
To: %s
Subject: %s

"
       % (FROMADDR, ", ".join(TOADDRS), SUBJECT) )
msg += "some text
"

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.

I'm using Python 2.5.2.

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

Edit: changed port from 25 to 587 as suggested by ΤΖΩΤΖΙΟΥ, and dropped the second ehlo(). Now I would love to know why port 25 works perfectly from my machine (and port 465 does not).

这篇关于使用 Python 示例发送电子邮件失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆