简体   繁体   中英

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

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

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM