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


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

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250