Discussion:
[Tutor] sending email via smtplib
Saad Javed
2012-11-18 05:23:28 UTC
Permalink
import smtplib

from_addr = "***@hotmail.com"
to_addr = "***@gmail.com"
smtp_srv = "smtp.live.com"

subject = "Test"
message = "Test"

msg = "To:%s\nFrom:%s\nSubject: %s\n\n%s" % (to_addr, from_addr, subject,
message)

smtp = smtplib.SMTP(smtp_srv, 587)
smtp.set_debuglevel(1)
smtp.ehlo()
smtp.starttls()
smtp.ehlo()
smtp.login(user, passwd)
smtp.sendmail(from_addr, to_addr, msg)
smtp.quit()

When I run this code, I get this output:
send: 'ehlo [127.0.1.1]\r\n'
reply: '250-BLU0-SMTP190.blu0.hotmail.com Hello [my-ip-address]\r\n'
reply: '250-TURN\r\n'
reply: '250-SIZE 41943040\r\n'
reply: '250-ETRN\r\n'
reply: '250-PIPELINING\r\n'
reply: '250-DSN\r\n'
reply: '250-ENHANCEDSTATUSCODES\r\n'
reply: '250-8bitmime\r\n'
reply: '250-BINARYMIME\r\n'
reply: '250-CHUNKING\r\n'
reply: '250-VRFY\r\n'
reply: '250-TLS\r\n'
reply: '250-STARTTLS\r\n'
reply: '250 OK\r\n'
reply: retcode (250); Msg: BLU0-SMTP190.blu0.hotmail.com Hello
[my-ip-address]
TURN
SIZE 41943040
ETRN
PIPELINING
DSN
ENHANCEDSTATUSCODES
8bitmime
BINARYMIME
CHUNKING
VRFY
TLS
STARTTLS
OK
send: 'STARTTLS\r\n'
Traceback (most recent call last):
File "sendemail.py", line 24, in <module>
smtp.starttls()
File "/usr/lib/python2.7/smtplib.py", line 636, in starttls
(resp, reply) = self.docmd("STARTTLS")
File "/usr/lib/python2.7/smtplib.py", line 385, in docmd
return self.getreply()
File "/usr/lib/python2.7/smtplib.py", line 358, in getreply
+ str(e))
smtplib.SMTPServerDisconnected: Connection unexpectedly closed: [Errno 104]
Connection reset by peer

I can send email via browser. Why is my authentication being blocked by
hotmail?

P.S: I tried sending from gmail. Same error.

Saad
Alexander
2012-11-18 23:42:00 UTC
Permalink
Post by Saad Javed
import smtplib
smtp_srv = "smtp.live.com"
subject = "Test"
message = "Test"
msg = "To:%s\nFrom:%s\nSubject: %s\n\n%s" % (to_addr, from_addr, subject,
message)
smtp = smtplib.SMTP(smtp_srv, 587)
smtp.set_debuglevel(1)
smtp.ehlo()
smtp.starttls()
smtp.ehlo()
smtp.login(user, passwd)
smtp.sendmail(from_addr, to_addr, msg)
smtp.quit()
send: 'ehlo [127.0.1.1]\r\n'
reply: '250-BLU0-SMTP190.blu0.hotmail.com Hello [my-ip-address]\r\n'
reply: '250-TURN\r\n'
reply: '250-SIZE 41943040\r\n'
reply: '250-ETRN\r\n'
reply: '250-PIPELINING\r\n'
reply: '250-DSN\r\n'
reply: '250-ENHANCEDSTATUSCODES\r\n'
reply: '250-8bitmime\r\n'
reply: '250-BINARYMIME\r\n'
reply: '250-CHUNKING\r\n'
reply: '250-VRFY\r\n'
reply: '250-TLS\r\n'
reply: '250-STARTTLS\r\n'
reply: '250 OK\r\n'
reply: retcode (250); Msg: BLU0-SMTP190.blu0.hotmail.com Hello
[my-ip-address]
TURN
SIZE 41943040
ETRN
PIPELINING
DSN
ENHANCEDSTATUSCODES
8bitmime
BINARYMIME
CHUNKING
VRFY
TLS
STARTTLS
OK
send: 'STARTTLS\r\n'
File "sendemail.py", line 24, in <module>
smtp.starttls()
File "/usr/lib/python2.7/smtplib.py", line 636, in starttls
(resp, reply) = self.docmd("STARTTLS")
File "/usr/lib/python2.7/smtplib.py", line 385, in docmd
return self.getreply()
File "/usr/lib/python2.7/smtplib.py", line 358, in getreply
+ str(e))
smtplib.SMTPServerDisconnected: Connection unexpectedly closed: [Errno
104] Connection reset by peer
I can send email via browser. Why is my authentication being blocked by
hotmail?
P.S: I tried sending from gmail. Same error.
Saad
You start TLS but is the connection to the server secured using SSL?
Usually email providers have particular ports and types of encryption that
must be specified to authenticate before you can DL an entire inbox or send
a message from the address.
Alexander
Saad Javed
2012-11-19 05:32:25 UTC
Permalink
I don't think using SSL works with hotmail. I tried using:

smtplib.*SMTP_SSL*("smtp.live.com", 587)
smtplib.login(user, passwd)
...

That gave this error:

Traceback (most recent call last):
File "sendemail.py", line 22, in <module>
smtp = smtplib.SMTP_SSL(smtp_srv, 587)
File "/usr/lib/python2.7/smtplib.py", line 776, in __init__
SMTP.__init__(self, host, port, local_hostname, timeout)
File "/usr/lib/python2.7/smtplib.py", line 249, in __init__
(code, msg) = self.connect(host, port)
File "/usr/lib/python2.7/smtplib.py", line 309, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/usr/lib/python2.7/smtplib.py", line 782, in _get_socket
new_socket = ssl.wrap_socket(new_socket, self.keyfile, self.certfile)
File "/usr/lib/python2.7/ssl.py", line 381, in wrap_socket
ciphers=ciphers)
File "/usr/lib/python2.7/ssl.py", line 143, in __init__
self.do_handshake()
File "/usr/lib/python2.7/ssl.py", line 305, in do_handshake
self._sslobj.do_handshake()
ssl.SSLError: [Errno 1] _ssl.c:504: error:140770FC:SSL
routines:SSL23_GET_SERVER_HELLO:unknown protocol
Prasad, Ramit
2012-11-19 18:14:07 UTC
Permalink
smtplib.SMTP_SSL("smtp.live.com", 587)
You need to use port 25 not 587.
http://windows.microsoft.com/en-US/hotmail/send-receive-email-from-mail-client
smtplib.login(user, passwd)
...
  File "sendemail.py", line 22, in <module>
    smtp = smtplib.SMTP_SSL(smtp_srv, 587)
  File "/usr/lib/python2.7/smtplib.py", line 776, in __init__
    SMTP.__init__(self, host, port, local_hostname, timeout)
  File "/usr/lib/python2.7/smtplib.py", line 249, in __init__
    (code, msg) = self.connect(host, port)
  File "/usr/lib/python2.7/smtplib.py", line 309, in connect
    self.sock = self._get_socket(host, port, self.timeout)
  File "/usr/lib/python2.7/smtplib.py", line 782, in _get_socket
    new_socket = ssl.wrap_socket(new_socket, self.keyfile, self.certfile)
  File "/usr/lib/python2.7/ssl.py", line 381, in wrap_socket
    ciphers=ciphers)
  File "/usr/lib/python2.7/ssl.py", line 143, in __init__
    self.do_handshake()
  File "/usr/lib/python2.7/ssl.py", line 305, in do_handshake
    self._sslobj.do_handshake()
ssl.SSLError: [Errno 1] _ssl.c:504: error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol
~Ramit


This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.
Saad Javed
2012-11-20 04:30:55 UTC
Permalink
Using port 25 with SMTP_SSL gives:

Traceback (most recent call last):
File "sendemail.py", line 22, in <module>
smtp = smtplib.SMTP_SSL(smtp_srv, 25)
File "/usr/lib/python2.7/smtplib.py", line 776, in __init__
SMTP.__init__(self, host, port, local_hostname, timeout)
File "/usr/lib/python2.7/smtplib.py", line 249, in __init__
(code, msg) = self.connect(host, port)
File "/usr/lib/python2.7/smtplib.py", line 309, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/usr/lib/python2.7/smtplib.py", line 782, in _get_socket
new_socket = ssl.wrap_socket(new_socket, self.keyfile, self.certfile)
File "/usr/lib/python2.7/ssl.py", line 381, in wrap_socket
ciphers=ciphers)
File "/usr/lib/python2.7/ssl.py", line 143, in __init__
self.do_handshake()
File "/usr/lib/python2.7/ssl.py", line 305, in do_handshake
self._sslobj.do_handshake()
socket.error: [Errno 104] Connection reset by peer

Loading...