简体   繁体   中英

Why am I getting this error: "TimeoutError: [Errno 110] Connection timed out" using python code in Pycharm?

import smtplib

my_email = "*******@gmail.com"
password = "***********"

with smtplib.SMTP("smtp.gmail.com") as connection:
    connection.starttls()
    connection.login(user=my_email, password=password)
    connection.sendmail(from_addr=my_email, to_addrs="*******@yahoo.com",
                        msg="Subject:Hello\n\n This is the body of my email.")

In gmail the "less secure apps" feature was enabled. Conversely, in Yahoo, the "Generate App Password" feature was used and still got the same response for both instances.

There can be many reasons for this error - typically it means something has gone wrong in setting up the smtplib connection and your email hasn't set.

To get a more descriptive and helpful error message, you need to enable debugging.

To do that add, add this line of code:

connection.set_debuglevel(1)

That will print out much more descriptive debugger messages to the console.

try to change your transfer security from starttls() to SMTP_SSL , so your code look like this:

import smtplib, ssl #import ssl module

port = 465  # For SSL

# Create a secure SSL context
context = ssl.create_default_context()

with smtplib.SMTP_SSL("smtp.gmail.com", port, context=context) as server:
    server.login(my_email, password)
    server.sendmail(from_addr=my_email, to_addrs=reciver_email, msg=message)

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