I have been working on email stuff on and off for the last… forever.

One of the very handy and easy to use tools to have in one’s pocket for testing email functionality with a particular SMTP server is the ability to quickly send email through a selected server from the command line. Just like using telnet to test ports makes day-to-day IT life easier vs. having to grab and install some extra tool, so also does the ability to shoot emails off from a CLI.

So without much further discussion, here is a block of commands I came across to do just that from Powershell:

$EmailFrom = “bobdog@somedomain.com”
$EmailTo = “jdoe@someotherdomain.com”
$Subject = “This is an email test from powershell”
$Body = “This is a test message that should be the body of my email.”
$SMTPServer = “emailserver.somedomain.com”

$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 25)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential(“myusername”, “mypassword”);
$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)

The first 5 lines are all just setting variables. The SMTPServer can be a domain name or an IP address.

The next 4 lines are calling up the Net.Mail.SmtpClient “class” and doing stuff with it. Not having done powershell programming within the last few months that about as much as I can tell you but the syntax should be plain enough that you can get by with knowing that much for basic testing.

The first line for the SMTPClient is also where you specify a port. In this case we are using the default SMTP port of 25.

The next line enables connection encryption. If your server supports it, then use it. If not, then don’t. If you don’t know, then try both (change $true to $false).

The second to last line is where you put in your credentials (username and password) if the server requires them. The final puts it all together and sends your email.

I did notice that if you are using connection encryption and your email users is using a self-signed certificate that this will not work. I am sure there is a way to override that protection but I don’t know what it is.

Anyhow, I have used this quite a bit for testing. It saves a lot of time and is easy to manipulate vs. having to install and configure a client.

Happy new year!

All credit goes to HowToGeek.com for having the most concise article on the topic, from which I stole the commands above:

1 of 1

This post has no comments. Be the first to leave one!

Leave a Reply