Send-MailMessage :SMTP 服务器需要安全连接或客户端未通过身份验证服务器响应为:5.7.57 SMTP;

问题描述

我正在尝试使用 PowerShell 为自动邮件编写脚本。下面是我的脚本。我不知道为什么我会收到身份验证错误。我怀疑我可能需要添加证书。出于测试目的,我在本地机器上有自签名证书,但我不确定如何将其添加到此脚本中。如果它不是证书,请告诉我为什么会出现此错误以及解决方案是什么。

PS:- 我查看了其他 stackoverflow 答案,但没有任何帮助。

# Define clear text string for username and password
    [string]$userName = 'sample@domain.net'
    [string]$userPassword = 'xxxx@paxxxword'
    # Convert to securestring
    [securestring]$secStringPassword = ConvertTo-securestring $userPassword -AsPlainText -Force
    # Get the credential
    [pscredential]$credential = New-Object System.Management.Automation.PSCredential ($userName,$secStringPassword)
    #$credential = Get-Credential
    ## Define the Send-MailMessage parameters
    $mailParams = @{
        SmtpServer                 = 'smtp.outlook.com'
        Port                       = '587' # or '25' if not using TLS
        UseSSL                     = $true ## or not if using non-TLS
        Credential                 = $credential
        From                       = 'sample@domain.net'
        To                         = 'user@domain.com'
        Subject                    = "testing mail with powershell - $(Get-Date -Format g)"
        Body                       = 'This is a test email using SMTP Client Submission'
        DeliveryNotificationoption = 'OnFailure','OnSuccess'
    }
    ## Send the message
    Send-MailMessage @mailParams

以下是我得到的错误:-

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 
[ZR0P278Cxxxx.xxxP278.PROD.outlook.com]
At line:26 char:1
+ Send-MailMessage @mailParams
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.Mail.SmtpClient:Sm 
   tpClient) [Send-MailMessage],SmtpException
    + FullyQualifiedErrorId : SmtpException,Microsoft.PowerShell.Commands.Send 
   MailMessage

解决方法

您似乎将 From 地址作为电子邮件地址传递,这不是正确的电子邮件地址。对于 Outlook,From 必须是Outlook 系统上的真实地址。

此外,造成这种情况的原因可能有很多,我建议您也在这里查看 Lydia Zhou 提供的见解:https://social.technet.microsoft.com/Forums/en-US/22134794-12bf-4673-b67e-2f45510e34fa/530-5757-smtp-client-was-not-authenticated-to-send-anonymous-mail-during-mail-from?forum=onlineservicesexchange