使用python发送电子邮件 [英] sending emails using python

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

问题描述

我在交换服务器上拥有用户帐户。

我可以使用python发送电子邮件吗?


如果iam使用以下代码我收到错误

fromAddress =''s**************@satyam.com''

toAddress =''s **** **********@satyam.com''

msg ="主题:你好\ n \ n这是邮件正文。

import smtplib

server = smtplib.SMTP(" hstmsg002",25)

server.sendmail(fromAddress,toAddress,msg)


错误:


回溯(最近一次调用最后一次):

文件

" C: \ sridhar \Beginning_Python \Beginning_Python \Chap ter16\tryitout \InitialMailExample.py",

第5行,在?

server = smtplib.SMTP( hstmsg002,25)

文件C:\Python24 \lib \ smtplib.py,第244行,在__init__

(代码,消息) )= self.connect(主机,端口)

文件C:\Python24 \lib \ smtplib.py,第307行,在连接中

(代码,msg)= self.getreply()

文件C:\Python24 \lib \ smtplib.py,第351行,在getreply中

提升SMTPServerDisconnected(连接意外关闭)

SMTPServerDisconnected:连接意外关闭

iam having user account on an exchangeserver.
with that can i send an email using python?

if iam using the following code iam getting error
fromAddress = ''s**************@satyam.com''
toAddress = ''s**************@satyam.com''
msg = "Subject: Hello\n\nThis is the body of the message."
import smtplib
server = smtplib.SMTP("hstmsg002",25)
server.sendmail(fromAddress, toAddress, msg)

error:

Traceback (most recent call last):
File
"C:\sridhar\Beginning_Python\Beginning_Python\Chap ter16\tryitout\InitialMailExample.py",
line 5, in ?
server = smtplib.SMTP("hstmsg002",25)
File "C:\Python24\lib\smtplib.py", line 244, in __init__
(code, msg) = self.connect(host, port)
File "C:\Python24\lib\smtplib.py", line 307, in connect
(code, msg) = self.getreply()
File "C:\Python24\lib\smtplib.py", line 351, in getreply
raise SMTPServerDisconnected("Connection unexpectedly closed")
SMTPServerDisconnected: Connection unexpectedly closed

推荐答案

sridhar启发我们:
sridhar enlightened us with:

我在交换服务器上拥有用户帐户。

我可以使用python发送电子邮件吗?


如果我使用以下代码我收到错误


fromAddress =''s**************@satyam.com''

toAddress =''s ** ************@satyam.com''

msg ="主题:Hello \ n \ n这是邮件正文。
iam having user account on an exchangeserver.
with that can i send an email using python?

if iam using the following code iam getting error
fromAddress = ''s**************@satyam.com''
toAddress = ''s**************@satyam.com''
msg = "Subject: Hello\n\nThis is the body of the message."



^^^^


你需要\\\\\\\ n。电子邮件中的行尾应该是DOS

line-ends。

^^^^

You need \r\n\r\n there. Line-ends in email messages should be DOS
line-ends.


Traceback(最近一次调用最后一次):

文件

" C:\sridhar\Beginning_Python \Beginning_Python \Chap ter16\tryitout \InitialMailExample.py",

第5行, in?

server = smtplib.SMTP(" hstmsg002",25)

文件" C:\Python24 \lib \ smtplib.py",line 244,in __init__

(code,msg)= self.connect(host,port)

File" C:\Python24 \lib \ smtplib.py" ;,第307行,在连接中

(代码,msg)= self.getreply()

文件C:\Python24 \lib \ smtplib.py ;,第351行,在getreply中

提升SMTPServerDisconnected(连接意外关闭)

SMTPServerDisconnected:连接意外关闭
Traceback (most recent call last):
File
"C:\sridhar\Beginning_Python\Beginning_Python\Chap ter16\tryitout\InitialMailExample.py",
line 5, in ?
server = smtplib.SMTP("hstmsg002",25)
File "C:\Python24\lib\smtplib.py", line 244, in __init__
(code, msg) = self.connect(host, port)
File "C:\Python24\lib\smtplib.py", line 307, in connect
(code, msg) = self.getreply()
File "C:\Python24\lib\smtplib.py", line 351, in getreply
raise SMTPServerDisconnected("Connection unexpectedly closed")
SMTPServerDisconnected: Connection unexpectedly closed



嗯,这没用。您可以安装网络嗅探器

http://www.ethereal。 com / )并找出通过

网络连接实际发送的内容,以及出现问题的地方。


Sybren

-

Sybren St ?? vel

St ?? vel IT - http://www.stuvel.eu/

Well that''s useless. You could install a network sniffer
(http://www.ethereal.com/) and find out what''s actually sent over the
network connection, and where things go wrong.

Sybren
--
Sybren St??vel
St??vel IT - http://www.stuvel.eu/


在07/09/06,Sybren Stuvel < sy ******* @ yourthirdtower.com.imaginationwrote:
On 07/09/06, Sybren Stuvel <sy*******@yourthirdtower.com.imaginationwrote:

sridhar启发我们:
sridhar enlightened us with:

我在交换服务器上拥有用户帐户。

我可以使用python发送电子邮件吗?


如果iam使用以下代码我将获得错误


回溯(最近一次调用最后一次):

文件

" C:\\\ xridhar \ Beeningning_Python \ Beginning_Python\Chap ter16\tryitout\InitialMailExample.py",

第5行,在?

server = smtplib.SMTP(" hstmsg002",25)

文件C:\Python24 \lib \ smtplib.py,第244行,在__init__

(代码,msg)= self.connect(主机,端口)

文件C:\Python24 \lib \ smtplib.py,第307行,在连接中

(代码,msg)= self.getreply()

文件C:\Python24 \lib \ smtplib.py,第351行,在getreply中

提升SMTPServerDisconnected(连接意外关闭)

SMTPServerDisconnected:连接意外关闭
iam having user account on an exchangeserver.
with that can i send an email using python?

if iam using the following code iam getting error

Traceback (most recent call last):
File
"C:\sridhar\Beginning_Python\Beginning_Python\Chap ter16\tryitout\InitialMailExample.py",
line 5, in ?
server = smtplib.SMTP("hstmsg002",25)
File "C:\Python24\lib\smtplib.py", line 244, in __init__
(code, msg) = self.connect(host, port)
File "C:\Python24\lib\smtplib.py", line 307, in connect
(code, msg) = self.getreply()
File "C:\Python24\lib\smtplib.py", line 351, in getreply
raise SMTPServerDisconnected("Connection unexpectedly closed")
SMTPServerDisconnected: Connection unexpectedly closed



嗯,这没用。您可以安装网络嗅探器

http://www.ethereal。 com / )并找出通过

网络连接实际发送的内容以及出错的地方。


Well that''s useless. You could install a network sniffer
(http://www.ethereal.com/) and find out what''s actually sent over the
network connection, and where things go wrong.



您是否确认可以在此服务器上使用SMTP?

您可以使用outlook express或类似设备通过它发送电子邮件吗? POP3 / IMAP

邮件客户端?


:)

Have you verified that you are allowed to use SMTP on this server ?
Can you send email via it using outlook express or a similar POP3/IMAP
mail client?

:)


Sybren Stuvel写道:
Sybren Stuvel wrote:

sridhar启发我们:
sridhar enlightened us with:

>>我在交换服务器上拥有用户帐户。
我可以使用python发送电子邮件吗?

如果我使用以下代码我收到错误

fromAddress =''s ********** ****@satyam.com''
toAddress =''s**************@satyam.com''
msg ="主题:你好\ n \\ n这是信息的主体。
>>iam having user account on an exchangeserver.
with that can i send an email using python?

if iam using the following code iam getting error
fromAddress = ''s**************@satyam.com''
toAddress = ''s**************@satyam.com''
msg = "Subject: Hello\n\nThis is the body of the message."



^^^^


你需要\\\\\\\ n。电子邮件中的行尾应为DOS

行尾。


^^^^

You need \r\n\r\n there. Line-ends in email messages should be DOS
line-ends.



这对于Python smtplib来说是不正确的,尽管根据

RFC。 SMTP.data()方法使用一个名为

quotedata()的本地声明函数来确保正确的行结尾,因此使用\ n将

导致与使用\\\\ nn相同的消息。

This is untrue for the Python smtplib, though correct according to the
RFCs. The SMTP.data() method uses a locally-declared function called
quotedata() to ensure the correct line endings, so using "\n" will
result in the same message as using "\r\n".


>
>

>> Traceback(最近一次调用最后一次):
文件
" C:\ sridhar \ Beginning_Python \Beginning_Python \Ch apter16\tryitout\InitialMailExample.py",
第5行,在?
server = smtplib.SMTP(" hstmsg002",25)
文件C:\\ \\ _Python24 \lib \ smtplib.py",第244行,在__init__
(代码,msg)= self.connect(主机,端口)
文件C:\Python24 \ lib \ smtplib.py",第307行,在连接中
(代码,msg)= self.getreply()
文件C:\Python24 \lib \ smtplib.py,行351,在getreply
提升SMTPServerDisconnected(连接意外关闭)
SMTPServerDisconnected:连接意外关闭
>>Traceback (most recent call last):
File
"C:\sridhar\Beginning_Python\Beginning_Python\Ch apter16\tryitout\InitialMailExample.py",
line 5, in ?
server = smtplib.SMTP("hstmsg002",25)
File "C:\Python24\lib\smtplib.py", line 244, in __init__
(code, msg) = self.connect(host, port)
File "C:\Python24\lib\smtplib.py", line 307, in connect
(code, msg) = self.getreply()
File "C:\Python24\lib\smtplib.py", line 351, in getreply
raise SMTPServerDisconnected("Connection unexpectedly closed")
SMTPServerDisconnected: Connection unexpectedly closed




那么'没用了。您可以安装网络嗅探器

http://www.ethereal。 com / )并找出通过

网络连接实际发送的内容以及出错的地方。



Well that''s useless. You could install a network sniffer
(http://www.ethereal.com/) and find out what''s actually sent over the
network connection, and where things go wrong.



它可能没用,但它比我们看到的一些问题更有意义,所以看起来有点不屑一顾说它没用。 - OP

提供了回溯,这在

新手的问题中经常丢失,并且已经将错误正确地本地化为一小块代码。


是的,错误信息在这些

的情况下并不是特别有用,但它可能代表了一个准确的描述(来自

客户的观点)关于发生了什么,在这种情况下,

Ethereal跟踪将提供比回溯更多的数据。我想
假设它会告诉你服务器是否正在发送RST或FIN来终止连接,但是它不会很清楚为什么。


我们可能会猜测连接正在关闭,因为

命名主机上没有运行SMTP服务器,或者(不太可能)它

需要身份验证。不幸的是,当我尝试在现有主机上连接到一个不存在的SMTP服务器时,我看到


socket.error:(113,''软件导致连接中止'')

2.4.3和2.5b2下的
。唉,我没有准备好访问需要身份验证的服务器

(我的服务器通过POP验证发件人

访问IIRC)。所以我无法重现这个错误。


也许SMTP服务器被某些防火墙接受来自

特定IP地址的连接?与服务器管理员聊天可能是一个好主意

,看看他们是否可以建议可能会出现问题。


问候

Steve

-

Steve Holden +44 150 684 7255 +1 800 494 3119

Holden Web LLC / Ltd http://www.holdenweb.com

Skype:holdenweb http://holdenweb.blogspot.com

最近的Ramblings http://del.icio.us/ steve.holden

Useless it might be, but it''s a lot more clueful than some questions we
see, so it seems a little dismissive just to say "it''s useless" - the OP
has provided the traceback, which is so often missing in questions from
newbies, and has correctly localized the error to a small chunk of code.

True, the error message isn''t particularly helpful in these
circumstances, but it probably represents an accurate description (from
the client''s point of view) about what''s happening, in which case an
Ethereal trace will provide little more data than the traceback. I
suppose it''ll tell you whether the server is sending RST or FIN to
terminate the connection, but it won''t give much insight into why.

We might hazard a guess that the connection is being closed because
there is no SMTP server running on the named host, or (less likely) it
requires authentication. Unfortunately when I try to connect to a
non-existent SMTP server on an existent host I see

socket.error: (113, ''Software caused connection abort'')

under both 2.4.3 and 2.5b2. Alas I don''t have ready access to a server
that requires authentication (my servers authenticate senders by POP
access IIRC). So I can''t reproduce this error.

Perhaps the SMTP server is strapped down to accepting connections from
specific IP addresses by some firewall? It would probably be a good idea
to have a chat with the server''s administrator to see if they can
suggest what might be going wrong.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden


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

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