7

I want to send an email from my server. I'm trying to send using a hotmail account that I already have.

user = "myaddress@hotmail.com"
passwd = "xxxxxxxxx"

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

subject = "Home Server Uptime"
message = "The server has been online for: %s" %(uptime)

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

I get this error:

Traceback (most recent call last):
  File "sendemail.py", line 23, 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 have no idea how to troubleshoot this.

Here is the output of set_debuglevel(1) as Nathan suggested:

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
2
  • Have you tried smtp.set_debuglevel(1)? You might get more useful output. Nov 16, 2012 at 6:31
  • Pasted the output of set_debuglevel(1) in the main post
    – koogee
    Nov 16, 2012 at 7:45

3 Answers 3

17

try this

import email
import smtplib

msg = email.message_from_string('warning')
msg['From'] = "example1@hotmail.fr"
msg['To'] = "example2@hotmail.fr"
msg['Subject'] = "helOoooOo"

s = smtplib.SMTP("smtp.live.com",587)
s.ehlo() # Hostname to send for this command defaults to the fully qualified domain name of the local host.
s.starttls() #Puts connection to SMTP server in TLS mode
s.ehlo()
s.login('example1@hotmail.fr', 'pass')

s.sendmail("example1@hotmail.fr", "example2@hotmail.fr", msg.as_string())

s.quit()
1
  • 10
    You should explain why your code is fixing the questioners problem.
    – morkro
    Aug 9, 2014 at 16:14
1

It looks like your code is failing when trying to start TLS (smtp.starttls()). I just tested smtp.live.com and I was able to get to the line in your code where you attempt to login, so it might be something due to how your network is setup.

This is unrelated, but according to the smtplib docs, you have to add the from and to address headers at the beginning of the message:

parts = ("From: " + from_addr,
         "To: " + to_addr,
         "Subject: " + subject,
         "",
         message)    
msg = '\r\n'.join(parts)
2
  • Added msg = "To:%s\nFrom:%s\nSubject: %s\n\n%s" % (to_addr, from_addr, subject, message) and changed smtp.sendmail(from_addr, to_addr, msg) . Still the same error :(
    – koogee
    Nov 16, 2012 at 7:42
  • Its a basically a Ubuntu file server running samba connected to the router. I haven't set up any mail server or any special changes to the network. I turned the firewall down to see if that was causing the problem. Still can't authenticate with Hotmail. Able to do so via the web browser though.
    – koogee
    Nov 16, 2012 at 7:54
1

Use smtp-mail.outlook.com as mail server. I tested and it worked not smtp.live.com.Otherwise the rest of the code is fine. Although gmail does not allow sending this way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.