Articles → POWERSHELL → Try-Catch Block In Powershell
Try-Catch Block In Powershell
Purpose
Example
$EmailFrom = "my@test.com"
$EmailTo = "Test@gmail.com"
$Subject = "Test mail Subject"
$Body = "Test Email Body"
$SMTPServer = "<No_Server>"
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 25)
$SMTPClient.EnableSsl = $true
Try
{
$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)
}
Catch
{
Write-Output($_.Exception.Message)
Break
}
Output
Click to Enlarge