471,337 Members | 1,019 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,337 software developers and data experts.

sending emails using python

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

Sep 7 '06 #1
14 9645
sridhar enlightened us with:
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."
^^^^

You need \r\n\r\n there. Line-ends in email messages should be DOS
line-ends.
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
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/
Sep 7 '06 #2
On 07/09/06, Sybren Stuvel <sy*******@yourthirdtower.com.imaginationwrote:
sridhar enlightened us with:
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

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.
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?

:)
Sep 7 '06 #3
Sybren Stuvel wrote:
sridhar enlightened us with:
>>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."

^^^^

You need \r\n\r\n there. Line-ends in email messages should be DOS
line-ends.
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 (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


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.
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

Sep 7 '06 #4
sridhar wrote:
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)

Do yourself a favor and use the email module to construct your messages
with. Constructing messages by hand can lead you into so many traps.
Especially if you are using international characters in you messages.
--

hilsen/regards Max M, Denmark

http://www.mxm.dk/
IT's Mad Science

Phone: +45 66 11 84 94
Mobile: +45 29 93 42 96
Sep 7 '06 #5
Max M wrote:
sridhar wrote:
>>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)

Do yourself a favor and use the email module to construct your messages
with. Constructing messages by hand can lead you into so many traps.
Especially if you are using international characters in you messages.

Good advice. However, since the error is occurring in the SMTP() call a
malformed message is clearly not the cause of this particular problem.

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

Sep 7 '06 #6
Steve Holden enlightened us with:
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".
Ah, wonderful.
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.
I agree with you. My remark might indeed have sounded harsher than
intended. It referred to the traceback itself, not the posting of the
traceback. That was rather useful.
Ethereal trace will provide little more data than the traceback.
I don't know until I see it. Perhaps the socket is closed in response
to something the client sends. That can be seen in the Ethereal trace.
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.
A quick peek in the SMTP logfiles should tell you that.
Perhaps the SMTP server is strapped down to accepting connections from
specific IP addresses by some firewall?
In that case I'd expect the SMTP server to simply ignore the
connection attempt and cause a timeout in the connecting phase. I
wouldn't expect a connection to be created and then closed again,
which is what I read in the posted traceback.
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.
Good idea indeed. Another idea would be to use something like netcat
or telnet to connect to the port and see what happens if you manually
type an SMTP connection.

Sybren
--
Sybren Stüvel
Stüvel IT - http://www.stuvel.eu/
Sep 7 '06 #7
Tim Williams enlightened us with:
Can you send email via it using outlook express or a similar
POP3/IMAP mail client?
Wouldn't you use a SMTP client to send email?

Sybren
--
Sybren Stüvel
Stüvel IT - http://www.stuvel.eu/
Sep 7 '06 #8
On 07/09/06, Sybren Stuvel <sy*******@yourthirdtower.com.imaginationwrote:
Tim Williams enlightened us with:
Can you send email via it using outlook express or a similar
POP3/IMAP mail client?

Wouldn't you use a SMTP client to send email?
Outlook Express *is* a mail client that uses SMTP as the outbound protocol.

:)
Sep 7 '06 #9
On 2006-09-07, Sybren Stuvel <sy*******@YOURthirdtower.com.imaginationwrote:
Tim Williams enlightened us with:
>Can you send email via it using outlook express or a similar
POP3/IMAP mail client?

Wouldn't you use a SMTP client to send email?
I would, but I don't use exchange server. :)

The one exchange server I used in the past didn't accept SMTP
mail.

--
Grant Edwards grante Yow! I have the power
at to HALT PRODUCTION on all
visi.com TEENAGE SEX COMEDIES!!
Sep 7 '06 #10
On 07/09/06, Hari Sekhon <hp******@googlemail.comwrote:
>
Grant Edwards wrote:
On 2006-09-07, Sybren Stuvel
<sy*******@YOURthirdtower.com.imaginationwrote:
Tim Williams enlightened us with:
Can you send email via it using outlook express or a similar
POP3/IMAP mail client?

Wouldn't you use a SMTP client to send email?

I would, but I don't use exchange server. :)

The one exchange server I used in the past didn't accept SMTP
mail.
errr, I used to admin Exchange, if it does accept SMTP then how could it
function as a live mail server?
Did you mean *doesn't accept* ??

There are a couple of possibilities at least, of the top of my head:

* The IMC / IMS (smtp connector) is located on a different server
within its Exchange organisation, or a DMZ'ed exchange server that is
only used for SMTP.

* The server doesn't accept SMTP from local IP ranges, only from
external (ie inbound email)

Outlook / Exchange clients use MAPI, internal SMTP is only a
requirement if you have non-MAPI clients sending email through the
server.

HTH :)
Sep 7 '06 #11
On 2006-09-07, Tim Williams <ti*@tdw.netwrote:
> Wouldn't you use a SMTP client to send email?

I would, but I don't use exchange server. :)

The one exchange server I used in the past didn't accept SMTP
mail.

errr, I used to admin Exchange, if it does accept SMTP then
how could it function as a live mail server?

Did you mean *doesn't accept* ??
One presumes he did.
There are a couple of possibilities at least, of the top of my
head:

* The IMC / IMS (smtp connector) is located on a different
server within its Exchange organisation, or a DMZ'ed
exchange server that is only used for SMTP.

* The server doesn't accept SMTP from local IP ranges, only
from external (ie inbound email)
I believe it was the latter. I'm pretty sure there was only
one server. When I first started at that company it did accept
SMTP connections on it's internal network interface.
Outlook / Exchange clients use MAPI, internal SMTP is only a
requirement if you have non-MAPI clients sending email through
the server.
And BOFH was horrified by non-MS software, so he shut off IMAP
support and SMTP support on the internal network as a way to
force everybody to switch to Outlook.

--
Grant Edwards grante Yow! This ASEXUAL
at PIG really BOILS
visi.com my BLOOD... He's
so... so... URGENT!!
Sep 7 '06 #12
Hari Sekhon wrote:
[...]
>
BOFH?
Bas**rd Operator From Hell
lol
Indeed.

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

Sep 7 '06 #13

Tim Williams wrote:
On 07/09/06, Sybren Stuvel <sy*******@yourthirdtower.com.imaginationwrote:
sridhar enlightened us with:
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
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.

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?

:)

yes , because iam having account on that server

Sep 14 '06 #14
On 14 Sep 2006 00:11:05 -0700, sridhar <ra*****************@gmail.comwrote:
>
Tim Williams wrote:
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?

:)


yes , because iam having account on that server
Having an account on an Exchange server doesn't automatically mean you
have SMTP access to that server. Some exchange servers don't have
SMTP running at all, and others will not allow SMTP access from local
IP addresses - even with authentication.

Can you telnet to your Exchange server on port 25 and start an SMTP
conversation, using the EHLO or HELO, MAIL and RCPT commands, but
quitting before the DATA command? If you can successfully specify a
recipient, then your script should work, if not you will need your
script to work around the problem based on what you do or don't get
back from the server.

HTH :)
Sep 14 '06 #15

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Arek | last post: by
3 posts views Thread by A | last post: by
4 posts views Thread by aroraamit81 | last post: by
2 posts views Thread by Maxelcat | last post: by
3 posts views Thread by lizii | last post: by
reply views Thread by Bernhard Walle | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.