sending emails using python

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • sridhar

    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 = 'sridhar_kastur i@satyam.com'
    toAddress = 'sridhar_kastur i@satyam.com'
    msg = "Subject: Hello\n\nThis is the body of the message."
    import smtplib
    server = smtplib.SMTP("h stmsg002",25)
    server.sendmail (fromAddress, toAddress, msg)

    error:

    Traceback (most recent call last):
    File
    "C:\sridhar\Beg inning_Python\B eginning_Python \Chapter16\tryi tout\InitialMai lExample.py",
    line 5, in ?
    server = smtplib.SMTP("h stmsg002",25)
    File "C:\Python24\li b\smtplib.py", line 244, in __init__
    (code, msg) = self.connect(ho st, port)
    File "C:\Python24\li b\smtplib.py", line 307, in connect
    (code, msg) = self.getreply()
    File "C:\Python24\li b\smtplib.py", line 351, in getreply
    raise SMTPServerDisco nnected("Connec tion unexpectedly closed")
    SMTPServerDisco nnected: Connection unexpectedly closed

  • Sybren Stuvel

    #2
    Re: sending emails using python

    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 = 'sridhar_kastur i@satyam.com'
    toAddress = 'sridhar_kastur i@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\Beg inning_Python\B eginning_Python \Chapter16\tryi tout\InitialMai lExample.py",
    line 5, in ?
    server = smtplib.SMTP("h stmsg002",25)
    File "C:\Python24\li b\smtplib.py", line 244, in __init__
    (code, msg) = self.connect(ho st, port)
    File "C:\Python24\li b\smtplib.py", line 307, in connect
    (code, msg) = self.getreply()
    File "C:\Python24\li b\smtplib.py", line 351, in getreply
    raise SMTPServerDisco nnected("Connec tion unexpectedly closed")
    SMTPServerDisco nnected: 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/

    Comment

    • Tim Williams

      #3
      Re: sending emails using python

      On 07/09/06, Sybren Stuvel <sybrenUSE@your thirdtower.com. imaginationwrot e:
      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\Beg inning_Python\B eginning_Python \Chapter16\tryi tout\InitialMai lExample.py",
      line 5, in ?
      server = smtplib.SMTP("h stmsg002",25)
      File "C:\Python24\li b\smtplib.py", line 244, in __init__
      (code, msg) = self.connect(ho st, port)
      File "C:\Python24\li b\smtplib.py", line 307, in connect
      (code, msg) = self.getreply()
      File "C:\Python24\li b\smtplib.py", line 351, in getreply
      raise SMTPServerDisco nnected("Connec tion unexpectedly closed")
      SMTPServerDisco nnected: 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?

      :)

      Comment

      • Steve Holden

        #4
        Re: sending emails using python

        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 = 'sridhar_kastur i@satyam.com'
        >>toAddress = 'sridhar_kastur i@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\B eginning_Python \Beginning_Pyth on\Chapter16\tr yitout\InitialM ailExample.py",
        >>line 5, in ?
        > server = smtplib.SMTP("h stmsg002",25)
        > File "C:\Python24\li b\smtplib.py", line 244, in __init__
        > (code, msg) = self.connect(ho st, port)
        > File "C:\Python24\li b\smtplib.py", line 307, in connect
        > (code, msg) = self.getreply()
        > File "C:\Python24\li b\smtplib.py", line 351, in getreply
        > raise SMTPServerDisco nnected("Connec tion unexpectedly closed")
        >>SMTPServerDis connected: 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

        Comment

        • Max M

          #5
          Re: sending emails using python

          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 = 'sridhar_kastur i@satyam.com'
          toAddress = 'sridhar_kastur i@satyam.com'
          msg = "Subject: Hello\n\nThis is the body of the message."
          import smtplib
          server = smtplib.SMTP("h stmsg002",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


          IT's Mad Science

          Phone: +45 66 11 84 94
          Mobile: +45 29 93 42 96

          Comment

          • Steve Holden

            #6
            Re: sending emails using python

            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 = 'sridhar_kastur i@satyam.com'
            >>toAddress = 'sridhar_kastur i@satyam.com'
            >>msg = "Subject: Hello\n\nThis is the body of the message."
            >>import smtplib
            >>server = smtplib.SMTP("h stmsg002",25)
            >>server.sendma il(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

            Comment

            • Sybren Stuvel

              #7
              Re: sending emails using python

              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/

              Comment

              • Sybren Stuvel

                #8
                Re: sending emails using python

                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/

                Comment

                • Tim Williams

                  #9
                  Re: sending emails using python

                  On 07/09/06, Sybren Stuvel <sybrenUSE@your thirdtower.com. imaginationwrot e:
                  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.

                  :)

                  Comment

                  • Grant Edwards

                    #10
                    Re: sending emails using python

                    On 2006-09-07, Sybren Stuvel <sybrenUSE@YOUR thirdtower.com. imaginationwrot e:
                    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!!

                    Comment

                    • Tim Williams

                      #11
                      Re: sending emails using python

                      On 07/09/06, Hari Sekhon <hpsekhon@googl email.comwrote:
                      >
                      Grant Edwards wrote:
                      On 2006-09-07, Sybren Stuvel
                      <sybrenUSE@YOUR thirdtower.com. imaginationwrot e:
                      >
                      >
                      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 :)

                      Comment

                      • Grant Edwards

                        #12
                        Re: sending emails using python

                        On 2006-09-07, Tim Williams <tim@tdw.netwro te:
                        > 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!!

                        Comment

                        • Steve Holden

                          #13
                          Re: sending emails using python

                          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

                          Comment

                          • sridhar

                            #14
                            Re: sending emails using python


                            Tim Williams wrote:
                            On 07/09/06, Sybren Stuvel <sybrenUSE@your thirdtower.com. imaginationwrot e:
                            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\Beg inning_Python\B eginning_Python \Chapter16\tryi tout\InitialMai lExample.py",
                            line 5, in ?
                            server = smtplib.SMTP("h stmsg002",25)
                            File "C:\Python24\li b\smtplib.py", line 244, in __init__
                            (code, msg) = self.connect(ho st, port)
                            File "C:\Python24\li b\smtplib.py", line 307, in connect
                            (code, msg) = self.getreply()
                            File "C:\Python24\li b\smtplib.py", line 351, in getreply
                            raise SMTPServerDisco nnected("Connec tion unexpectedly closed")
                            SMTPServerDisco nnected: 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

                            Comment

                            • Tim Williams

                              #15
                              Re: sending emails using python

                              On 14 Sep 2006 00:11:05 -0700, sridhar <ramasridhar.ka sturi@gmail.com wrote:
                              >
                              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 :)

                              Comment

                              Working...