I am sometimes getting TimeoutError: [WinError 10060] when sending an email on python using smtp

53 Views Asked by At

This is the function I have to send an email from one outlook email to another. It works sometimes but sometime I get the 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

def send_email(subject,receiver_email,name):
  
    msg = EmailMessage()
    msg["Subject"] = subject
    msg["From"] = formataddr(("Subject.",f"{sender_email}"))
    msg["To"] = receiver_email
    msg["BCC"] = sender_email
    msg.set_content(
        f"""\
        some message
        """
    )
    mailserver = smtplib.SMTP(email_server,587)
    mailserver.ehlo()
    mailserver.starttls()
    mailserver.ehlo()
    mailserver.login(sender_email, password_email)

    mailserver.sendmail(sender_email,receiver_email,msg.as_string())

    mailserver.quit()

I tried changing the port but to no avail.

1

There are 1 best solutions below

0
Gb Fd On

Turns out I was trying to send an email to frequently and my organizations mail was blocking it. I just decreased the rate at which I send my emails.