email – Send-MailMessage:SMTP服务器需要安全连接或客户端未经过身份验证.该

第一次海报 – 我整个星期都在谷歌搜索!我是Powershell的新手,我正在尝试使用Send-MailMessage.目标是在计划任务上设置Powershell脚本以发送自动电子邮件.我知道设置一个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

$secpasswd = ConvertTo-SecureString “PASSWORD” -AsPlainText -Force

$mycreds = New-Object System.Management.Automation.PSCredential "user@domain.com", $secpasswd

Send-MailMessage -To "user@domain.com" -Subject "Subject" -SmtpServer "smtp.office365.com" -Credential $mycreds -UseSsl -Port "587" -From "user@domain.com"

我正在尝试使用Office 365发送邮件(您可以从SMTP服务器中看到).当我将其直接复制并粘贴到控制台时,这可以正常工作,但是当我尝试使用以下命令调用脚本时,它会显示上述错误.

Powershell.exe -File C:\my_path\Script.ps1

有什么东西我不见了吗?可能是一种更好的方法来调用验证它的脚本?

任何帮助将不胜感激,我几天来一直盯着各种论坛帖子! 🙂

最佳答案
请查看我的Github Gist获取此Click Here或参见下面的示例.

我有同样的问题,似乎这是一个通用的错误消息,大多数时候,当我输入错误的密码时,我收到此消息,但我相信没有SendAs权限给出相同的错误,端口,SMTPServer和UseSSL参数在示例中配置为Office365我刚刚测试,它的工作原理.

另请注意,我提供了Secure Strings的示例,如果要将密码安全地存储在脚本文件中,则应使用此示例.

### 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

转载注明原文:email – Send-MailMessage:SMTP服务器需要安全连接或客户端未经过身份验证.该 - 代码日志