在PowerShell中发送电子邮件时,Smtp服务器错误

问题描述 投票:0回答:1

PowerShell电子邮件功能如下:

    $Attachment = "C:\Users\HP\Desktop\Tools\Email_on_failure\errorlist.txt"
    $Subject = "Errors!"
    $Body = "See attachment"
    $SMTPServer = "smtp.mail.yahoo.com"
    $SMTPPort = "465"
    Send-MailMessage -From [email protected] -to [email protected] -Subject $Subject -Body $Body -SmtpServer $SMTPServer -port $SMTPPort -UseSsl -Credential (Get-Credential) -Attachments $Attachment

但是我在下面收到此错误:

Send-MailMessage : Smtp server returned an invalid response.
At C:\Users\HP\Desktop\Tools\Email_on_failure\EOF.ps1:28 char:2
+     Send-MailMessage -From [email protected] -to gowthamsaar ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.Mail.SmtpClient:SmtpClient) [Send-MailMessage], SmtpException
    + FullyQualifiedErrorId : SmtpException,Microsoft.PowerShell.Commands.SendMailMessage

也是!处理时间约为20秒。请帮助!

powershell cmdlets cmdlet
1个回答
0
投票

(截至2020年有效的解决方案)

[如果您希望使用著名提供商(例如gmail,yahoo和Outlook)的SMTP设置从PowerShell发送电子邮件,则需要先执行以下操作(我现在使用过gmail。您可以针对yahoo或展望):

  1. 您需要为您的Gmail帐户关闭2step身份验证。
  2. 然后您需要打开LessSecureApp访问。

现在使用下面的代码从PowerShell发送电子邮件(所使用的PS版本为V5)来触发电子邮件:


    $Attachment = "C:\abc\def\errorlist.txt"
    $password = ConvertTo-SecureString 'd*@*@*@*@*@*@*0' -AsPlainText -Force
    $credential = New-Object System.Management.Automation.PSCredential ('[email protected]', $password)
    Send-MailMessage -From [email protected] -to [email protected] -Subject 'Subject line here' -Body 'Body line goes here' -SmtpServer 'smtp.gmail.com' -port 587 -UseSsl -Credential $credential -Attachments $Attachment

我使用$ credentials标记提供了凭据,因为我在一些不涉及中断的自动化中使用了此代码。可以在-Credential处使用(Get-Credentials)作为参数,以更安全的方式来提供凭据,但是在脚本执行期间,它会通过显示Feed凭据弹出窗口而中断。

© www.soinside.com 2019 - 2024. All rights reserved.