Resend email if Send-MailMessage returns an error

Using Send-MailMessage command is pretty simple. However there are some situations where it might cause you some problems. I recently noticed that it sometimes returns timeout error – Error in processing. The server response was: 4.7.0 Timeout waiting for client input . To fix it I created simple loop to make 5 resend attempts – it can be useful if you had to add script to a task scheduler.

Send-MailMessage

If you prepared already some output ($Body) and you want to send it to your mailbox you can just run the following lines:


$Date = (Get-Date).ToString('MMMM-dd')
$subject = ”[PowerShellBros] Report - ” + $Date
$priority = ”Normal”
$smtpServer = "smtp.powershellbros.com"
$emailFrom = ”Reports@powershellbros.com”
$emailTo = "pawel.janowicz@powershellbros.com"
$port = 25 
$Body = "?"

# Send email
Send-MailMessage -To $emailTo -Subject $subject -BodyAsHtml $body -SmtpServer $smtpServer -port $port -From $emailFrom -Priority $priority

For more information you can check Microsoft docs page.

To resend an email in case of failure we can use simple do/until loop:

	
  $n=1
  Do 
  {
      Write-Host "Attempt $n - Sending email to $emailto"            
      #Send email
      Send-MailMessage -ErrorAction SilentlyContinue -ErrorVariable SendError -To $emailTo -Subject $subject -BodyAsHtml $body -SmtpServer $smtpServer -port $port -From $emailFrom -Priority $priority

      $n++
  } 
  Until ( $SendError.exception.message -eq $null -or $n -eq 6 )
	

Output:
Output in console should look like this:

Send-MailMessage
Send-MailMessage

Final script


    # Setup email parameters -------------------------------------------------
    $Date = (Get-Date).ToString('MMMM-dd')
    $subject = ”[PowerShellBros] Report - ” + $Date
    $priority = ”Normal”
    $smtpServer = "smtp.powershellbros.com"
    $emailFrom = ”Reports@powershellbros.com”
    $emailTo = "pawel.janowicz@powershellbros.com"
    $port = 25 
    # -----------------------------------------------------------------------                

        # Use loop to do 5 attempts in case of failure or timeout
        $n=1
        Do 
        {
            Write-Host "Attempt $n - Sending email to $emailto"            
            $ErrorMessage = $null
          
            #Send email
            Send-MailMessage -ErrorAction SilentlyContinue -ErrorVariable SendError -To $emailTo -Subject $subject -BodyAsHtml $body -SmtpServer $smtpServer -port $port -From $emailFrom -Priority $priority
                        
            $ErrorMessage = $SendError.exception.message
            
            If($ErrorMessage)
            {
                Write-Host "Failure - $ErrorMessage"
                Start-Sleep -Seconds 5
            }

            $n++
        } 
        Until ( $SendError.exception.message -eq $null -or $n -eq 6 )

        If($SendError.exception.message -eq $null)
        {
            Write-Host "`nEmail has been sent to $emailto" -ForegroundColor Yellow
        }
        Else
        {
            Write-Host "`nFailed to sent email to $emailto" -ForegroundColor Yellow
        }

Leave a Reply

Your email address will not be published. Required fields are marked *