【问题标题】:Smtp server errors while sending email in PowerShell在 PowerShell 中发送电子邮件时出现 SMTP 服务器错误
【发布时间】:2020-05-11 12:16:39
【问题描述】:

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 gs4u@yahoo.com -to gs4u@outlook.com -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 gowthamsaarthy4u@yahoo.com -to gowthamsaar ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.Mail.SmtpClient:SmtpClient) [Send-MailMessage], SmtpException
    + FullyQualifiedErrorId : SmtpException,Microsoft.PowerShell.Commands.SendMailMessage

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

【问题讨论】:

  • 在您的示例中,您似乎缺少 Get-Credential 周围的右括号
  • @derekbaker783 你好。实际上它确实有一个右括号。我在编写查询时错过了。谢谢告知。现在用括号更新了问题。请帮帮我。
  • 不带附件试试看是否可行
  • $SMTPPort 应该是 int 类型。删除那里的引号。

标签: powershell powershell-cmdlet


【解决方案1】:

(截至 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 ('gohy4u@gmail.com', $password)
    Send-MailMessage -From gohy4u@gmail.com -to g4u@gmail.com -Subject 'Subject line here' -Body 'Body line goes here' -SmtpServer 'smtp.gmail.com' -port 587 -UseSsl -Credential $credential -Attachments $Attachment

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-17
    • 2014-02-21
    • 2019-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多