【问题标题】:Powershell Send-MailMessage fails authentication on Azure PipelinePowershell Send-MailMessage 在 Azure Pipeline 上的身份验证失败
【发布时间】:2020-12-21 13:43:14
【问题描述】:

我正在尝试使用以下Powershell 任务在Azure Devops 的管道上发送一些电子邮件:

- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: |    
      $username = "$(emailUsername)"
      $password = ConvertTo-SecureString -String "$(emailPassword)" -AsPlainText -Force

      $credentials = [PSCredential]::New($username, $password)

      $sendMailParams = @{
          SMTPServer  = 'smtp.office365.com'
          Port        = 587
          UseSsl      = $false
          Credential  = $credentials
          From        = $username
          To          = '${{ parameters.recipientsAddresses }}'
          Subject     = '${{ parameters.mailSubject }}'
          Body        = '${{ parameters.mailBody }}'
      }

      $encoding = [System.Text.Encoding]::UTF8

      Send-MailMessage @sendMailParams -Encoding $encoding

PowerShell 脚本可以在我的机器上本地运行,但是当我尝试在我的管道上运行它时它会失败并显示以下消息:

Send-MailMessage : The SMTP server requires a secure connection or the client was not authenticated. The server 
response was: 5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM [<MAIL_SERVER>.outlook.com]

我在这里错过了什么?

【问题讨论】:

  • 你有MS代理还是自托管代理?
  • MS代理,目前在ubuntu-latest上运行
  • 所以我猜你需要打开端口或在你的 SMTP 中访问代理 ip

标签: azure powershell azure-devops


【解决方案1】:

这可能有不同的原因:

  1. From 地址为emailAddress,这不是正确的电子邮件地址。对于Office 365,发件人需要是Office 365 系统上的真实地址。如果您将电子邮件地址硬编码为 From 和您的 Office 365 密码,您可以验证这一点。
  2. 确保您设置的$credentials 正确。

【讨论】:

  • 我真的怀疑这些原因,因为这个脚本在本地运行时工作得很好。
  • 你能尝试一次使用不同的值吗?
  • 是的,我在本地和管道上都试过了,得到了相同的结果。
猜你喜欢
  • 2021-06-04
  • 1970-01-01
  • 2022-11-04
  • 1970-01-01
  • 2021-10-16
  • 1970-01-01
  • 1970-01-01
  • 2017-05-13
  • 2018-02-14
相关资源
最近更新 更多