Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8602921
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T02:12:16+00:00 2026-06-12T02:12:16+00:00

I am using smtplib and I am sending notification emails from my application. However

  • 0

I am using smtplib and I am sending notification emails from my application. However I noticed that sometimes (especially when there is a lot of idle time between mail sending) I get a SMTPServerDisconnected error.

I guess there are 2 solutions for this (know none of them, though)

  1. Increase idle time between sending mails
  2. reconnect when connection is down.

I think 2nd solution seems more elegant. But how can I do that?

edit: I am adding the code

from smtplib import SMTP
smtp = SMTP()
smtp.connect('smtp.server.com')
smtp.login('username','password')

def notifyUser():
    smtp.sendmail(from_email, to_email, msg.as_string())
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T02:12:17+00:00Added an answer on June 12, 2026 at 2:12 am

    If your use case is sending a single message at a time, the solution that seems most correct to me, would be to create a new SMTP session for each message:

    from smtplib import SMTP
    
    smtp = SMTP('smtp.server.com')
    
    def notifyUser(smtp, smtp_user, smtp_password, from_email, to_email, msg):
        smtp.login(smtp_user, smtp_password)
        smtp.sendmail(from_email, to_email, msg.as_string())
        smtp.quit()
    

    If your SMTP server doesn’t required that you authenticate yourself (a common case), this can be further simplified to:

    from smtplib import SMTP
    
    smtp = SMTP('smtp.server.com')
    
    def notifyUser(smtp, from_email, to_email, msg):
        smtp.sendmail(from_email, to_email, msg.as_string())
        smtp.quit()
    

    If it is common to have more than one message to send at once, and you want to optimise this case by reusing the same SMTP session for the group of messages (can be simplified as above if you don’t need to login to the SMTP server):

    from smtplib import SMTP
    
    smtp = SMTP('smtp.server.com')
    
    def notifyUsers(smtp, smtp_user, smtp_password, from_to_msgs):
        """
        :param from_to_msgs: iterable of tuples with `(from_email, to_email, msg)`
        """
        smtp.login(smtp_user, smtp_password)
        for from_email, to_email, msg in from_to_msgs:
            smtp.sendmail(from_email, to_email, msg.as_string())
        smtp.quit()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following script for sending mails using python import smtplib from email.mime.multipart
I am using this code taken from here : import smtplib def prompt(prompt): return
I would like to multithread sending mail (using django's SMTPConnection or perhaps the smtplib
I want to write a program that sends email using Python's smtplib . I
I am using smtplib sendmail and \n (line feed) is being added where there
I am using the following code below: import smtplib import zipfile import tempfile from
Possible Duplicate: Specify a sender when sending mail with Python (smtplib) I'm using smtplib
I'm using smtplib to send emails through Python, everything's well and works fine, but
I have the following code that I am using to send a message from
I'm using smtplib sending out mails, rather frequently. I'm using my own SMTP server

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.