How to send an email with PowerShell

How to send an email with PowerShell

emailThe asnwer to How to send an email with PowerShell is very simple: Send-MailMessage! I love this command! It’s simple and it works very well.
I have tested this only on PS 3.0, but the command is available for 4.0 and 5.0 as well. I’m not going to write down the whole lot of informations, if you want to have a deeper knoledge about this command, have a look at Microsoft’s Send-MailMessage CMDLET.

If you’re looking for a simple explaination including examples and issues I found with this command, keep reading.
It is important to understand the fact that you will require an SMTP server to connect to! I’ve only tested this with Microsoft Exchange 2010 so far.

Let’s say we want to send an email from myemail@contoso.com, to colleague@contoso.com, with Test001 as a subject, using mysmtp01.contoso.com as smtp server and an HTML body (Hello,<BR /> this is a test.<BR />Kind Regards,<BR />PowerShell).

Send-MailMessage -To "colleague@contoso.com" -Subject "Test001", -Body "Hello,<BR /> this is a test.<BR />Kind Regards,<BR />PowerShell" -From "myemail@contoso.com" -BodyAsHtml -SmtpServer "mysmtp01.contoso.com"

That simple! You can also attach a file with -Attachments “data.csv”. As I said, I won’t be covering all of the options here but this is how to send an email with PowerShell!

I have had issues with our Exchange Environment to send emails every now and then, they would fail with this error: Unable to read data from the transport connection: net_io_connectionclosed.
The weird thing is that if I am to try to re-send this command a second later, it would get the email sent.

I found somebody on the internet having the same issue and the way he resolved it was better than the way I was working on. What this piece of code below does is very simple: it tries to send the email 5 times and if it fails 5 times then it’ll quit.

$emailFrom="myemail@contoso.com"
$emailTo="colleague@contoso.com"
$emailSubject="Test001"
$emailBody="Hello,<BR /> this is a test.<BR />Kind Regards,<BR />PowerShell"
$emailSMTP="mysmtp01.contoso.com"
	
$StopEmailLoop=$false
[int]$RetryCount=0
	

Do {
	Try {
			Send-MailMessage -From $emailFrom -To $emailTo -Subject $emailSubject -Body $emailBody -SmtpServer $emailSMTP -BodyAsHTML -ErrorAction Stop;
			Write-Host "Email has been sent."
			$StopEmailLoop = $true
		}
		Catch {
				If ($RetryCount -gt 4){
					Write-Host "Cannot send email. The script will exit."
					$StopEmailLoop = $true
					}
				Else {
						Write-Host "Cannot send email Trying again in 3 seconds."
					Start-Sleep -Seconds 3
					$RetryCount = $RetryCount+1
					}
		}
	}
While ($StopEmailLoop -eq $false)

The logic behind the script is to use a Do/While and a Try/Catch. It’ll try to send the email (and catch the error, if there’s any) until $StopEmailLoop is no longer $false. It’ll equal false either after 5 tentatives or after the email has been completed. $RetryCount is the counter keeping the record of how many times the command has been tried.

This was the answer to How to send an email with PowerShell with Send-MailMessage, have fun with it!

IT Droplets

IT Droplets