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


Picture showing the output of try-catch in PowerShell
Click to Enlarge


Posted By  -  Karan Gupta
 
Posted On  -  Friday, April 26, 2019

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250