【问题标题】:Send-MailMessage : The SMTP server requires a secure connection or the client was not authenticated. TheSend-MailMessage :SMTP 服务器需要安全连接或客户端未通过身份验证。这
【发布时间】:2017-04-10 01:12:03
【问题描述】:

第一次发布海报 - 我整个星期都在谷歌上搜索!我对 Powershell 还很陌生,我正在尝试使用 Send-MailMessage。目标是在计划任务上设置 Powershell 脚本以发送自动电子邮件。我知道将 Powershell 脚本设置为计划任务有它自己的细微差别,我已经研究过,我想我知道接下来该怎么做,但在我到达那一点之前,当我调用脚本:

Send-MailMessage : SMTP 服务器需要安全连接或客户端未通过身份验证。服务器 响应是:5.7.57 SMTP;在 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

我有什么遗漏的吗?可能是调用对其进行身份验证的脚本的更好方法?

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

【问题讨论】:

    标签: email powershell office365


    【解决方案1】:

    请查看我的 Github Gist 以获取此 Click Here 或查看下面的示例。

    我遇到了同样的问题,似乎这是一条通用错误消息,大多数时候我在输入错误密码时收到此消息,但我相信没有 SendAs 权限会给出相同的错误,端口、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
    

    【讨论】:

      猜你喜欢
      • 2023-03-21
      • 2011-05-28
      • 2017-12-02
      • 2012-03-06
      • 2015-11-26
      • 2016-09-25
      相关资源
      最近更新 更多