Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I wrote a PowerShell script which is supposed to send emails automatically, but sometimes the email won't send out due to a network issue.

Here is how I'm sending email:


Here are the error logs:

Exception calling "Send" with "1" argument(s): "Failure sending mail."
At line:1 char:24
+ $smtp_notification.Send <<<< ($mail_notification)
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

Is there anyway to re-run the sending line when I met this failure? Can anyone give me some suggestions please?

share|improve this question
up vote 3 down vote accepted

Put a loop with a try block in it, very simplistically:

$worked = $false
while (-not $worked) {
  try {
    #Perform command to retry, passing -ErrorAction Stop

    $worked = $true  # An exception will skip this
  } catch {
    # Should check to retry: error record is in $_
share|improve this answer
Thanks very much Richard, this is really usful! – pansal Apr 8 '12 at 2:22

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.