Search code examples
pythonsmtplib

Why am I getting a timeout [WinError 10060] error when sending an email via smtplib?


I am sending the following email:

# Import smtplib for the actual sending function
import smtplib

# Import the email modules we'll need
from email.message import EmailMessage

# Open the plain text file whose name is in textfile for reading.
with open(r'C:\Users\David\Documents\Hello.txt') as fp:
    # Create a text/plain message
    msg = EmailMessage()
    msg.set_content(fp.read())

# me == the sender's email address
# you == the recipient's email address
msg['Subject'] = 'Enquiry'
msg['From'] = "example1@hotmail.co.uk"
msg['To'] = "example2@hotmail.co.uk"

# Send the message via our own SMTP server.
s =server = smtplib.SMTP('smtp.live.com', 587)
s.send_message(msg)
s.quit()

When I send the message I get the following error:

TimeoutError: [WinError 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

I am not sure why this is. Does anyone have any ideas?

I've masked the original email addresses.


Solution

  • No localhost had been set up. This did the trick!:

    # Import smtplib for the actual sending function
    import smtplib
    
    # Import the email modules we'll need
    from email.message import EmailMessage
    
    # Open the plain text file whose name is in textfile for reading.
    with open(r'C:\Users\David\Documents\Hello.txt') as fp:
        # Create a text/plain message
        msg = EmailMessage()
        msg.set_content(fp.read())
    
    # me == the sender's email address
    # you == the recipient's email address
    msg['Subject'] = 'yes'
    msg['From'] = "example1@hotmail.com"
    msg['To'] = "example2@hotmail.com"
    
    # Login
    s =server = smtplib.SMTP('smtp.office365.com', 587)
    s.starttls()
    s.login('example1@hotmail.com',"password")
    
    # Sending the message
    s.send_message(msg)
    s.quit()