我的剧本旨在吸收计划任务中的错误未正确完成并通过 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 中继,但我做不到。
如果我使用我们用于邮件的域名(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 "[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
使用 Verbose 我得到了这个:
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
答案1
查看您收到的原始错误消息:
SMTP 服务器需要安全连接,否则客户端未通过身份验证。服务器响应为:5.7.57 SMTP;客户端未通过身份验证,无法在 MAIL FROM 期间发送匿名邮件
第一个建议(安全连接要求)可能被忽略,因为你做过-UseSSL
已经使用switch 参数指定符合此类要求
我们留下的印象是,您提供的凭证未能成功验证您的身份。
如果是这种情况,SMTP 服务器会将您的身份视为“匿名”,除非发送主机明确列入白名单,否则这种身份很少被允许提交电子邮件。因此,SMTP 响应消息如下:
5.7.57 SMTP;客户端未通过身份验证,无法在 MAIL FROM 期间发送匿名邮件
突然变得更有意义了。
我猜你的 SMTP 地址 ( [email protected]
) 是不是与您的用户主体名称相同([email protected]
或类似名称),这会导致身份验证失败