Test SMTP Server Connection
Sometimes you need to test to see if your SMTP server is even listening to you, especially if it requires authentication! Use either this python script, or these PowerShell commands to find out!
PowerShell Method
First, set some credentials
$credz = get-credential
Then send the messageSend-MailMessage -From from@addr.com -To email@addr.com -Subject "Test" -Body "Testing Email via PowerShell" -SmtpServer smtp.office365.com -Credential $credz -UseSSL -Port 587
Python Method
I tend to run the python code in a Jupyter Lab session, but you can run it however works best for you.
import smtplib, ssl
from email.mime.text import MIMEText
smtp_server = "smtp.office365.com"
port = 587 # For starttls
sender_email = "email-at-domain-dot-com"
password = "hellalongpassword"
msg = MIMEText("Test")
# Create a secure SSL context
context = ssl.create_default_context()
# Try to log in to server and send email
try:
server = smtplib.SMTP(smtp_server,port)
server.ehlo() # Can be omitted
server.starttls(context=context) # Secure the connection
server.ehlo() # Can be omitted
server.login(sender_email, password)
# Send email here
me = "from@domain.com"
you = "targetRecipient@domain.com"
# me == the sender's email address
# you == the recipient's email address
msg['Subject'] = 'Test'
msg['From'] = me
msg['To'] = you
server.sendmail(me, [you], msg.as_string())
except Exception as e:
# Print any error messages to stdout
print(e)
finally:
server.quit()
That's all for now.