user7722705
user7722705

Reputation:

TimeoutError trying to send an email with smtplib

I am trying to use Python's smtplib module to send an email but I keep receiving a TimeoutError.

import smtplib

def send():
    try:
        server = smtplib.SMTP('smtp.gmail.com', 587)
        server.ehlo()
        server.starttls()
        server.login(email, password)  # Email and password are already defined
        print('Enter recipient: ')
        recipient = input()
        print('Enter subject:')
        subject = input() + '\n'
        print('Enter body:')
        body = input()
        server.sendmail(email, recipient, body)
        server.quit()

    except TimeoutError as e:
        print (str(e))

This code recieves a:

[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

Upvotes: 2

Views: 5482

Answers (3)

Kakarot
Kakarot

Reputation: 31

Use port 587. I was getting the same error but when I entered 587 as port it worked

smtplib.SMTP("smtp.gmail.com", 587)

Upvotes: 3

John Jeon
John Jeon

Reputation: 31

If you are using a VPN connection, disable it first. It worked for me.

Upvotes: 3

Karmesh Maheshwari
Karmesh Maheshwari

Reputation: 127

It looks like you have not enabled "Allow Less Secure Apps" option in your GMail account.

Here's more officially from Google, https://support.google.com/accounts/answer/6010255?hl=en

Upvotes: 1

Related Questions