Send-MailMessage :SMTP 服务器需要安全连接,或者客户端未经过身份验证。这

Mig*_*pup 5 email powershell office365

第一次海报 - 我整个星期都在谷歌上搜索这个!我对 Powershell 相当陌生,我正在尝试使用 Send-MailMessage。目标是在计划任务上设置 Powershell 脚本以发送自动电子邮件。我知道将 Powershell 脚本设置为计划任务有其自己的细微差别,我已经研究过,并且我想我知道下一步该做什么,但在我达到这一点之前,我一直遇到以下错误:我称该脚本为:

\n\n
\n

Send-MailMessage :SMTP 服务器需要安全连接,或者客户端未经过身份验证。服务器响应为:5.7.57 SMTP;客户端在 MAIL FROM 期间未经过身份验证以发送匿名邮件

\n
\n\n
$secpasswd = ConvertTo-SecureString \xe2\x80\x9cPASSWORD\xe2\x80\x9d -AsPlainText -Force\n\n$mycreds = New-Object System.Management.Automation.PSCredential "user@domain.com", $secpasswd\n\nSend-MailMessage -To "user@domain.com" -Subject "Subject" -SmtpServer "smtp.office365.com" -Credential $mycreds -UseSsl -Port "587" -From "user@domain.com"\n
Run Code Online (Sandbox Code Playgroud)\n\n

我正在尝试使用 Office 365 发送邮件(正如您从 SMTP 服务器中看到的那样)。当我将其直接复制并粘贴到控制台中时,效果很好,但是当我尝试使用以下命令调用脚本时,它会显示上述错误。

\n\n
Powershell.exe -File C:\\my_path\\Script.ps1\n
Run Code Online (Sandbox Code Playgroud)\n\n

我缺少什么吗?可能是调用验证脚本的更好方法?

\n\n

任何帮助将不胜感激,我已经盯着各种论坛帖子好几天了!:)

\n

Ste*_*NLD 1

请查看我的 Github Gist 点击这里或查看下面的示例。

我遇到了同样的问题,似乎这是一条通用错误消息,大多数时候我在输入错误密码时收到此消息,但我相信没有 SendAs 权限会产生相同的错误, Port 、 SMTPServer 和 UseSSL 参数示例中的配置是针对 Office365 的,我刚刚测试过,它可以工作。

另请注意,我提供了安全字符串的示例,如果您想将密码安全地存储在脚本文件中,则应该使用此示例。

### Script Global Settings
#Declare SMTP Connection Settings
$SMTPConnection = @{
    #Use Office365, Gmail, Other or OnPremise SMTP Relay FQDN
    SmtpServer = 'outlook.office365.com'

    #OnPrem SMTP Relay usually uses port 25 without SSL
    #Other Public SMTP Relays usually use SSL with a specific port such as 587 or 443
    Port = 587 
    UseSsl = $true    

    #Option A: Query for Credential at run time.
    Credential = Get-Credential -Message 'Enter SMTP Login' -UserName "emailaddress@domain.tld"

    <#
    #Option B: Hardcoded Credential based on a SecureString
    Credential = New-Object -TypeName "System.Management.Automation.PSCredential" -ArgumentList @( 

        #The SMTP User Emailaddress
        "emailaddress@domain.tld"

        #The Password as SecureString encoded by the user that wil run this script!
        #To create a SecureString Use the folowing Command: Read-Host "Enter Password" -AsSecureString | ConvertFrom-SecureString
        "Enter the SecureString here as a single line" | ConvertTo-SecureString
    ) 
    #> 
}

### Script Variables
#Declare Mailmessages.
$MailMessageA = @{
    From = "emailaddress@domain.tld"
    To = @(
        "emailaddress@domain.tld"
    )
    #Cc = @(
    #    "emailaddress@domain.tld"
    #)
    #Bcc = @(
    #    "emailaddress@domain.tld"
    #)

    Subject = 'Mailmessage from script'
    #Priority = 'Normal' #Normal by default, options: High, Low, Normal
    #Attachments = @(
        #'FilePath'    
    #)
    #InlineAttachments = @{
        #'CIDA'='FilePath'
    #} #For more information about inline attachments in mailmessages see: https://gallery.technet.microsoft.com/scriptcenter/Send-MailMessage-3a920a6d

    BodyAsHtml = $true    
    Body = "Something Unexpected Occured as no Content has been Provided for this Mail Message!" #Default Message
}

### Script Start

#Retrieve Powershell Version Information and store it as HTML with Special CSS Class
$PSVersionTable_HTLM = ($PSVersionTable.Values | ConvertTo-Html -Fragment) -replace '<table>', '<table class="table">'

#Retrieve CSS Stylesheet
$CSS = Invoke-WebRequest "https://raw.githubusercontent.com/advancedrei/BootstrapForEmail/master/Stylesheet/bootstrap-email.min.css" | Select-Object -ExpandProperty Content

#Build HTML Mail Message and Apply it to the MailMessage HashTable
$MailMessageA.Body = ConvertTo-Html -Title $MailMessageA.Subject -Head "<style>$($CSS)</style>" -Body "
    <p>
        Hello World,    
    </p>

    <p>
        If your recieved this message then this script works.</br>
        </br>
        <div class='alert alert-info' role='alert'>
            Powershell version
        </div>
        $($PSVersionTable_HTLM)
    </P>
" | Out-String


#Send MailMessage
#This example uses the HashTable's with a technique called Splatting to match/bind the Key's in the HashTable with the Parameters of the command.
#Use the @ Symbol instead of $ to invoke Splatting, Splatting improves readability and allows for better management and reuse of variables
Send-MailMessage @SMTPConnection @MailMessageA
Run Code Online (Sandbox Code Playgroud)