2017-01-20 63 views
0

我有一個從一個CSV文件導入聯繫人列表產生郵件PowerShell腳本:發送-返回MAILMESSAGE 4.4.1連接超時每10分鐘

# Get Credential 
$Credential = & "C:\Powershell Scripts\Windows\Get-CredentialFromWindowsCredentialManager.ps1" ms.outlook.15:[email protected] 

# Get Contacts 
$contacts = Import-Csv -Path "C:\Powershell Scripts\Email\Contacts.csv" 

# Compose Email for each contact 
foreach($contact in $contacts) 
{ 
    Write-Output "Creating email for: $($contact.FirstName) $($contact.LastName)" 
    $To = "$($contact.FirstName) $($contact.LastName) <$($contact.Email)>" 
    $From = "My Email <[email protected]>" 
    $Subject = "$($contact.FirstName), I have a suggestion for you!" 
    $Body = "<html></html>" 
    $SMTPServer = "smtp.office365.com" 
    $Port = 587 
    Send-MailMessage -To $To -From $From -Subject $Subject -SmtpServer $SMTPServer -Credential $Credential -UseSsl -Body $Body -BodyAsHtml -Port $Port 

    # Due to the Message Send rate limit (30 per minute) I added this to slow the rate down 
    Start-Sleep -Seconds 10 
} 

每隔10分鐘,我得到以下SMTP例外:

Send-MailMessage : Service not available, closing transmission channel. The 
server response was: 4.4.1 Connection timed out. Total session duration: 
00:10:08.3716645 
At C:\Powershell Scripts\Email\SendEmail.ps1:17 char:2 
+  Send-MailMessage -To $To -From $From -Subject $Subject -SmtpServer $SMTPServer ... 
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    + CategoryInfo   : InvalidOperation: (System.Net.Mail.SmtpClient:SmtpClient) [Send-MailMessage], SmtpException 
    + FullyQualifiedErrorId : SmtpException,Microsoft.PowerShell.Commands.SendMailMessage

是否有任何設置可以修改或更改代碼,防止出現這種情況?

+0

你是怎麼【考證】(https://social.technet.microsoft.com/Forums/exchange/EN-US/46095334-d788-4b4e-b790-4336a4174c9c/PowerShell的sendmailmessage-TIME-OUT-441-錯誤?論壇= exchange2010)? –

+0

我只發送了大約100-200封電子郵件(遠低於下面文章中概述的Office 365 E1計劃的限制)。 https://technet.microsoft.com/en-us/library/exchange-online-limits.aspx – EdC

回答

1

不要親自這樣做,這只是一個普遍的咆哮,但是你已經有了一個腳本來對付超出你的控制範圍的資源。因此,您不能期望SMTP連接成功,並且想知道您應該怎麼做以防止它失敗。深呼吸。答案是考慮邊緣案例,並在您的代碼中實際照顧它們。當然,你已經睡到嘗試不違反速率限制,但這不是一個可靠的解決方案。在這種情況下,圍繞 Send-MailMessage調用的簡單異常處理程序就足夠了。你可能會包含一些重試和小的睡眠延遲。

A「的接受失敗的最大數量」門檻可以用來打出來的重試循環而導致某種向內報警等

長話短說,不只是在牆上扔意大利麪閉上你的眼睛。

< /咆哮>

一個例子,但不一定是最整潔的解決方案:

[Int32] $maxAttempts  = 5; 
[Int32] $failureDelay  = 2; 
[Int32] $interMessageDelay = 10; 
[Int32] $numAttempts  = 0; 
[Boolean] $messageSent  = $false; 

:CONTACT foreach ($contact in $contacts) { 
    $numAttempts = 1; 
    $messageSent = $false; 
    while (($numAttempts -le $maxAttempts) -and (! $messageSent)) { 
     try { 
      Write-Host -Object ('Sending message, attempt #{0} of #{1}...' -f $numAttempts, $maxAttempts); 
      Send-MailMessage <blah>; 
      Write-Host -Object "Ok.`n"; 
      $messageSent = $true; 
      } #try 
     catch [System.Exception] { 
      # ^^^^^ This exception type needs changing, but I don't know the full 
      # type of your failure. 
      Write-Host -Object 'Failed.'; 
      if ($numAttempts -ge $maxAttempts) { 
       Write-Host -Object "ERROR : Maximum attempts reached - aborting.`n"; 
       continue CONTACT; 
      } else { 
       Write-Host -Object ('Sleeping for {0} second(s)...' -f $failureDelay); 
       Start-Sleep -Seconds $failureDelay; 
       $numAttempts++; 
      } #else-if 
      } #catch 
     } #while 
    Write-Host -Object ('Sleeping for {0} second(s)...' -f $interMessageDelay); 
    Start-Sleep -Seconds $interMessageDelay; 
    } #foreach