Search code examples
pythonemailoutlookgmailoutlook-2007

python- error sending an email


I am able to send from one gmail account to another gmail account and from one outlook account to another outlook account. But I am facing problem when I am trying to send mail from outlook to gmail.

Here is the code

import smtplib
content ='example email stuff here'
email='[email protected]'
password="password"
fromemail="[email protected]"
receive="[email protected]"
mail=smtplib.SMTP('smtp-mail.outlook.com',587)
mail.ehlo()
mail.starttls()
mail.login(email,password)
mail.sendmail(fromemail,receive,content)
mail.close()

When I am running the script I am getting the following error

Traceback (most recent call last):
  File "smtp_google.py", line 7, in <module>
    mail=smtplib.SMTP('smtp-mail.outlook.com',587)
  File "C:\Python27\lib\smtplib.py", line 256, in __init__
    (code, msg) = self.connect(host, port)
  File "C:\Python27\lib\smtplib.py", line 316, in connect
    self.sock = self._get_socket(host, port, self.timeout)
  File "C:\Python27\lib\smtplib.py", line 291, in _get_socket
    return socket.create_connection((host, port), timeout)
  File "C:\Python27\lib\socket.py", line 575, in create_connection
    raise err
socket.error: [Errno 10060] A connection attempt failed because the connected party did not properly respond after
a period of time, or established connection failed because connected host has failed to respond

Solution

  • Solution 1:

    Try by giving timeout in:

    mail=smtplib.SMTP('smtp-mail.outlook.com',587)
    

    as

    mail=smtplib.SMTP('smtp-mail.outlook.com',587, timeout=120)
    

    or try connecting via SSL instead of TLS/STARTTLS

        mail=smtplib.SMTP_SSL('smtp-mail.outlook.com',587)
    

    solution 2: Your email account must allow smtp, which is not necessarily enabled by default. You can try to change the settings by following the steps given here enable smtp authentication