Python Forum
SMTP email won't send. Program hangs.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
SMTP email won't send. Program hangs.
#1
Hi!

I'm having problems connecting to and/or sending email through an SMTP server. I've tried everything Google can find, but I'm clearly not understanding something.

When executing the program below, the code executes with no errors, but everything hangs at the end with a message saying the software unexpectedly lost connection with the server. No messages are sent. Here's the code.

Thanks!

Larry

# Version 0.5
 
# import email libraries
import smtplib                  
from email.message import EmailMessage  
 
# Define variables 
email_to = '[email protected]'    #This is obviously a fake address, but using the real thing doesn't work either. It is properly formatted as a string.
email_from = '[email protected]'   # Same thing, fake, but the correct address doesn't work - also formatted as a string.
email_pw = 'password'              # Yup, another string.
 
# Read email body text file - this the text of the email I want to send. All data merges are already stored in this file. It is a pure text file.
with open('/Users/larryj/Desktop/temp.txt','r') as file:
    content=file.read()
     
#  Create a text/plain message
msg = EmailMessage()
msg['Subject'] = 'Thank you '
msg['From'] = email_from
msg['To'] = email_to    
msg.set_content(content)     # Content contains the data read from the text file above.
 
# setup the server
smtp_server = 'secure.emailsrvr.com'      # This is the correct address for Rackspace email
smtp_user = email_from                      # In this case the sender also owns the email account
smtp_password = email_pw
 
# Send the message
with smtplib.SMTP(smtp_server, 465) as server:  #This is the correct port for SMTP messages on Rackspace email
    server.starttls()                                            # Upgrades the connection to a secure encrypted SSL/TLS connection
    server.login(smtp_user, smtp_password)
    server.send_message(msg)
deanhystad write Mar-27-2026, 02:38 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
Try this simple application, and expand as needed:

import smtplib
from email.message import EmailMessage

# Set up the SMTP server
smtp_server = smtplib.SMTP('smtp.example.com', 587)
smtp_server.starttls()
smtp_server.login('[email protected]', 'your_password')

# Create the email
msg = EmailMessage()
msg['Subject'] = 'Your Subject Here'
msg['From'] = '[email protected]'
msg['To'] = '[email protected]'
msg.set_content('This is the body of the email.')

# Send the email
smtp_server.send_message(msg)
smtp_server.quit()
Reply
#3
Larz60+:

Thank you! This was the last step to getting my program to work.

I'll go test this now.

Larry
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Sending email using own smtp server robertkwild 11 11,367 Jul-05-2024, 12:59 AM
Last Post: AdamHensley
  smtplib send email has no timestamp Pedroski55 2 3,011 Jun-11-2024, 04:57 AM
Last Post: Pedroski55
  Send email with smtp without using Mimetext mgallotti 0 1,690 Feb-01-2023, 04:43 AM
Last Post: mgallotti
  fetching exit status hangs in paramiko saisankalpj 3 3,289 Dec-04-2022, 12:21 AM
Last Post: nilamo
  email Library: properly send message as quoted-printable malonn 3 5,355 Nov-14-2022, 09:31 PM
Last Post: malonn
  Unable to send email attachments cosmarchy 7 6,191 Mar-09-2022, 09:29 PM
Last Post: bowlofred
  How to make scraper send email notification just once themech25 0 2,354 Nov-08-2021, 01:51 PM
Last Post: themech25
  send email smtp rwahdan 0 2,765 Jun-19-2021, 01:28 AM
Last Post: rwahdan
  Need Outlook send email code using python srikanthpython 3 13,963 Feb-28-2021, 01:53 PM
Last Post: asyswow64
  How to send email using python? Gigux 2 4,424 Jul-04-2020, 07:53 AM
Last Post: Gigux

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020