Good afternoon:
I have a Python program that what it does is read a Telegram message and send the same message but by email through the SMPT library. The problem is that by chance I send two messages from Telegram at the same time the program tries to send two emails and it gives me an error. To solve this I have put a try / exception, like this:
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login("[email protected]", "xxxxxxxx")
#mas codigo...
try:
server.sendmail(emisor, receptor, mensaje.as_string())
print "Try"
except Exception:
print "Exception"
#pass
sys.exc_clear()
The problem is that when the error is skipped the SMTP connection is disconnected and you can not send more email messages:
SMTPServerDisconnected: please run connect () first
To solve this I tried to add to the code what the error tells me, staying like this:
try:
server.sendmail(emisor, receptor, mensaje.as_string())
print "Try"
except Exception:
server.connect()
print "Exception"
#pass
sys.exc_clear()
But this in turn returns an error:
error: [Errno 111] Connection refused
I'm pretty lost, and I do not know how to get out of this. Any help is welcome. Thanks in advance.