Windows-Server-2003

將郵件通知從 Windows Server 2003 Powershell 2.0 發送到 Office 365

  • November 11, 2016

我的腳本旨在吸收未正確完成的計劃任務中的錯誤,並通過 Office365 將它們發送到我的電子郵件地址:

$reportHTML = Get-Content C:\windows\tasks\SchedLgU.Txt | Select-String -Pattern "an exit code of \(1\)" -context 2
$reportHTML = Out-String -InputObject $reportHTML

#TODO Add an if statement

$emailUsername = "myemail@somedomain.com"

#TODO: Change this to a file....
#$emailPassword = cat "C:\ps\emailCred.txt" | convertto-securestring
$emailPassword = ConvertTo-SecureString "somepassword" -AsPlainText -Force


$emailCred = new-object -typename System.Management.Automation.PSCredential($emailUsername, $emailPassword)



Send-MailMessage -To "myemail@somedomain.com"  -Body $reportHTML -Subject 'Job(s) Failed' -UseSsl -SmtpServer 'smtp.office365.com' -From 'myemail@somedomain.com' -Credential $emailCred

但是當我執行它時,我收到以下錯誤消息:

Send-MailMessage : The SMTP server requires a secure connection or the client was not authenticated. The server respons
e was: 5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM
At C:\ps\FailedJobNotificiation.ps1:14 char:17
+ Send-MailMessage <<<<  -To "myemail@somedomain.com"  -Body $reportHTML -Subject 'Job(s) Failed' -UseSsl -SmtpServ
er 'smtp.office365.com' -From 'myemail@somedomain.com' -Credential $emailCred
   + CategoryInfo          : InvalidOperation: (System.Net.Mail.SmtpClient:SmtpClient) [Send-MailMessage], SmtpExcept
  ion
   + FullyQualifiedErrorId : SmtpException,Microsoft.PowerShell.Commands.SendMailMessage

我也無法指定埠,但在我看來這可能是另一個問題,因為我收到了伺服器的回复,是否有某些原因我無法發送這封電子郵件?我在設置 SMTP 中繼時發現了一些東西,但我無法做到。

如果我使用我們用於郵件的域 ( somedomain.com),我會收到關於身份驗證的不同錯誤消息:

Send-MailMessage : The remote certificate is invalid according to the validation procedure.
At C:\ps\FailedJobNotificiation.ps1:16 char:17
+ Send-MailMessage <<<<  -To "myemail@somedomain.com"  -Body $reportHTML -Subject 'Lois Job(s) Failed' -UseSsl -SmtpServ
er 'mail.somedomain.com' -From 'myemail@somedomain.com' -Credential $emailCred
   + CategoryInfo          : InvalidOperation: (System.Net.Mail.SmtpClient:SmtpClient) [Send-MailMessage], Authentica
  tionException
   + FullyQualifiedErrorId : AuthenticationException,Microsoft.PowerShell.Commands.SendMailMessage

使用詳細我得到這個:

Send-MailMessage : The remote certificate is invalid according to the validation procedure.
At C:\ps\FailedJobNotificiation.ps1:17 char:17
+ Send-MailMessage <<<<  -Verbose -To "myemail@somedomain.com"  -Body $reportHTML -Subject 'Job(s) Failed' -UseSsl
-SmtpServer 'mail.somedomain.com' -From 'myemail@somedomain.com' -Credential $emailCred
   + CategoryInfo          : InvalidOperation: (System.Net.Mail.SmtpClient:SmtpClient) [Send-MailMessage], Authentica
  tionException
   + FullyQualifiedErrorId : AuthenticationException,Microsoft.PowerShell.Commands.SendMailMessage

查看您收到的原始錯誤消息:

SMTP 伺服器需要安全連接或客戶端未通過身份驗證。伺服器響應為:5.7.57 SMTP;在 MAIL FROM 期間,客戶端未通過身份驗證發送匿名郵件

第一個建議(安全連接的要求)可能會被忽略,因為您已經-UseSSL使用switch 參數指定了遵守此類要求

我們留下的印像是您提供的憑據沒有成功驗證您的身份。

如果是這種情況,SMTP 伺服器會將您的身份視為“匿名”,除非發送主機被明確列入白名單,否則該身份很少允許送出電子郵件。鑑於此,SMTP 響應消息:

5.7.57 SMTP;在 MAIL FROM 期間,客戶端未通過身份驗證發送匿名郵件

突然變得更有意義了。

我的猜測是您的 SMTP 地址 ( myemail@somedomain.com)您的使用者主體名稱不同(可能是myusername@corp.somedomain.com或類似名稱),這會導致身份驗證失敗

引用自:https://serverfault.com/questions/659496