![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | Try sending email 5 times? Hello I need to try to send email from powerscript, but sometimes it fails due to server load. Usually, sending the email again immediately fixes it. I tried to use the following code but it would not work. Basically, I want to try sending the email, if it fails, wait 2 seconds, and try again, for a total of 5 attempts, and exit as soon as email is sent successfully, or on the fifth failure. if ($messages -ne $null) { $EmailSuccessfullySent = $False [int] $CurrentEmailAttemptCount = 0 [int] $TotalEmailAttemptCount = 5 Clear-Item "variable:Error" while ($EmailSuccessfullySent -eq $False -and $CurrentEmailAttemptCount -lt $TotalEmailAttemptCount) { write-verbose "Send the email" [String] $EmailTo = "e...@email.com" [String] $EmailFrom = "e...@email.com" [String] $Subject = "Subject" [Bool] $IsBodyHtml = $False [String] $SmtpHost = "smpt.whatever.com" [Int] $Port = 25 $objSmtpClient = new-object System.Net.Mail.SmtpClient $objSmtpClient.Host = $SmtpHost $objSmtpClient.Port = $Port $objMessage = new-object System.Net.Mail.MailMessage $objMessage.From = $EmailFrom $objMessage.To.Add($EmailTo) $objMessage.Subject = $Subject $objMessage.IsBodyHtml = $IsBodyHtml $objMessage.Body = $Output $objSmtpClient.Send($objMessage) if ($Error -ne $Null) { write-verbose "Error while sending email" $EmailSuccessfullySent = $False $CurrentEmailAttemptCount++ Clear-Item "variable:Error" [String] $SmtpHost = "mail.actewagl.net.au" } else { $EmailSuccessfullySent = $True write-verbose "Email sent successfully to ($EmailTo)" write-Output "Email sent successfully to ($EmailTo)" } } } I tried a lot of other things with the Trap block, but nothing worked. Does anyone have any suggestions? Thanks |
My System Specs![]() |
| | #2 (permalink) |
| | RE: Try sending email 5 times? I got it. Here is the way I solved it. if ($messages -ne $null) { $EmailSuccessfullySent = $False [int] $CurrentEmailAttemptCount = 0 [int] $TotalEmailAttemptCount = 5 while ($EmailSuccessfullySent -eq $False -and $CurrentEmailAttemptCount -lt $TotalEmailAttemptCount) { write-verbose "Send the email" [String] $EmailTo = "me@myemaildomain.com" [String] $EmailFrom = "me@myemaildomain.com" [String] $Subject = "My Subject" [Bool] $IsBodyHtml = $False [String] $SmtpHost = "smtp.mysmtpserver.com" [Int] $Port = 25 $objSmtpClient = new-object System.Net.Mail.SmtpClient $objSmtpClient.Host = $SmtpHost $objSmtpClient.Port = $Port $objMessage = new-object System.Net.Mail.MailMessage $objMessage.From = $EmailFrom $objMessage.To.Add($EmailTo) $objMessage.Subject = $Subject $objMessage.IsBodyHtml = $IsBodyHtml $objMessage.Body = $messages $objSmtpClient.Send($objMessage) $EmailSent = $? if ($EmailSent) { $EmailSuccessfullySent = $True write-verbose "Email sent successfully to ($EmailTo)" write-Output "Email sent successfully to ($EmailTo)" } else { write-verbose "Error while sending email" $EmailSuccessfullySent = $False $CurrentEmailAttemptCount++ [String] $SmtpHost = "mail.actewagl.net.au" } } } |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| Incoming email times are all set to midnight. | Vista mail | |||
| Can't attach mp3 on Yahoo email, long wait times for sending | Vista mail | |||
| Email Sends Multiple Times | Live Mail | |||
| Email times out | Browsers & Mail | |||
| Outlook times out when sending attachments | Vista General | |||