How to test connection to SMTP server
Use the commands/script below to test the connection to an SMTP server.
Test connection to SMTP server
To connect using the TLS protocol on port 587, use:
$ openssl s_client -starttls smtp -connect <SMTP_SERVER>:587
To use SSL on port 465:
$ openssl s_client -connect <SMTP_SERVER>:465
Test SMTP server credentials and send test email
NOTE: Uses python3
import smtplib, ssl
from getpass import getpass
from email.message import EmailMessage
port = 25 # 465 SSL, or 587 TLS
server = '<SMTP SERVER OR IP>'
sender = '<SMTP EMAIL>'
password = getpass('Type the password and press enter: ')
recipient = '<RECIPIENT EMAIL>'
msg = EmailMessage()
msg['Subject'] = 'SMTP e-mail test'
msg['From'] = sender
msg['To'] = recipient
msg.set_content('This is a test e-mail message.')
context = ssl.create_default_context()
print("------------------------")
print("Test connection with SSL")
print("------------------------")
try:
smtp = smtplib.SMTP_SSL(server, port, context=context)
smtp.login(sender, password)
print("Connection successful")
#smtp.send_message(msg)
#print("Email sent successfully. Check and confirm!")
except Exception as e:
print(e)
print("\n")
print("---------------------------")
print("Test connection without SSL")
print("---------------------------")
try:
smtp = smtplib.SMTP(server, port)
smtp.login(sender, password)
print("Connection successful")
#smtp.send_message(msg)
#print("Email sent successfully. Check and confirm!")
except Exception as e:
print(e)
print("------------------------")
print("Test connection with TLS/SSL")
print("------------------------")
try:
smtp = smtplib.SMTP(server, port)
smtp.starttls(context=context)
smtp.login(sender, password)
print("Connection successful")
#smtp.send_message(msg)
#print("Email sent successfully. Check and confirm!")
except Exception as e:
print(e)