从Windows Server 2003 Powershell 2.0发送邮件通知到Office 365

我的脚本是为了吸收不正确完成的计划任务中的错误,并通过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 = "[email protected]" #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 "[email protected]" -Body $reportHTML -Subject 'Job(s) Failed' -UseSsl -SmtpServer 'smtp.office365.com' -From '[email protected]' -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 "[email protected]" -Body $reportHTML -Subject 'Job(s) Failed' -UseSsl -SmtpServ er 'smtp.office365.com' -From '[email protected]' -Credential $emailCred + CategoryInfo : InvalidOperation: (System.Net.Mail.SmtpClient:SmtpClient) [Send-MailMessage], SmtpExcept ion + FullyQualifiedErrorId : SmtpException,Microsoft.PowerShell.Commands.SendMailMessage 

我也无法指定一个端口,但在我看来,这可能是另一个问题,因为我从服务器回来,是否有一些原因,我不能发送这个电子邮件出来? 我发现了一些关于设置SMTP中继的事情,但我不pipe理。

如果我使用我们用于邮件的域( somedomain.com ),我会得到一个关于authentication的不同的错误消息:

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

查看您收到的原始错误消息:

SMTP服务器需要安全的连接或者客户端没有被authentication。 服务器响应是:5.7.57 SMTP; 在MAIL FROM期间,客户端未经过身份validation发送匿名邮件

第一个build议(安全连接的要求)可能被忽略,因为你已经通过使用-UseSSL开关参数指定了符合这样的要求

我们留下的是,您提供的证书没有成功validation您的印象。

如果是这种情况,SMTP服务器会将您的身份视为“匿名”,一个身份很less被允许提交电子邮件,除非发送主机明确列入白名单。 有鉴于此,SMTP响应消息:

5.7.57 SMTP; 在MAIL FROM期间,客户端未经过身份validation发送匿名邮件

突然变得更有意义。

我的猜测是您的SMTP地址( [email protected] )与您的用户主体名称(可能是[email protected]或类似名称)不同,这会导致身份validation失败