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 "", $secpasswd

Send-MailMessage -To "" -Subject "Subject" -SmtpServer "" -Credential $mycreds -UseSsl -Port "587" -From ""

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

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


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

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


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

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

    #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

        #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 = @(
    #Cc = @(
    #    "emailaddress@domain.tld"
    #Bcc = @(
    #    "emailaddress@domain.tld"

    Subject = 'Mailmessage from script'
    #Priority = 'Normal' #Normal by default, options: High, Low, Normal
    #Attachments = @(
    #InlineAttachments = @{
    #} #For more information about inline attachments in mailmessages see:

    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 "" | 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 "
        Hello World,    

        If your recieved this message then this script works.</br>
        <div class='alert alert-info' role='alert'>
            Powershell version
" | 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服务器需要安全连接或客户端未经过身份验证.该 - 代码日志