发送电子邮件的脚本不适用于 Gmail 帐户

问题描述

这里有一个简单的脚本来发送电子邮件

如果我使用 Gmail 设置(即端口 465 或 587 上的 smtp.gmail.com)运行它,脚本将不起作用,返回错误

服务器错误响应:需要 5.7.0 身份验证

# graphical stuff
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
Add-Type -AssemblyName PresentationFramework

# import modules
$workdir = Get-Location
Import-Module -Name "$workdir\Modules\Forms.psm1" # module for windows forms

$answ = [System.Windows.MessageBox]::Show("Configure for sending mail alerts?",'ALERTS','YesNo','Info')
if ($answ -eq "Yes") {    
    # dialog Box
    $formail = New-Object System.Windows.Forms.Form
    $formail.Text = "CONfig"
    $formail.Size = "500,300"
    $formail.StartPosition = 'CenterScreen'
    $formail.Topmost = $true
    $address = New-Object System.Windows.Forms.Label
    $address.Location = New-Object System.Drawing.Size(10,20) 
    $address.Size = New-Object System.Drawing.Size(120,20) 
    $address.Text = "Mail address:"
    $formail.Controls.Add($address)
    $addressBox = New-Object System.Windows.Forms.TextBox
    $addressBox.Location = New-Object System.Drawing.Point(130,20)
    $addressBox.Size = New-Object System.Drawing.Size(300,20)
    $formail.Add_Shown({$addressBox.Select()})
    $formail.Controls.Add($addressBox)
    $passwd = New-Object System.Windows.Forms.Label
    $passwd.Location = New-Object System.Drawing.Size(10,50) 
    $passwd.Size = New-Object System.Drawing.Size(120,20) 
    $passwd.Text = "Password:"
    $formail.Controls.Add($passwd)
    $passwdBox = New-Object System.Windows.Forms.MaskedTextBox
    $passwdBox.PasswordChar = '*'
    $passwdBox.Location = New-Object System.Drawing.Point(130,50)
    $passwdBox.Size = New-Object System.Drawing.Size(300,20)
    $formail.Add_Shown({$passwdBox.Select()})
    $formail.Controls.Add($passwdBox)
    $smtp = New-Object System.Windows.Forms.Label
    $smtp.Location = New-Object System.Drawing.Size(10,80) 
    $smtp.Size = New-Object System.Drawing.Size(120,20) 
    $smtp.Text = "SMTP server:"
    $formail.Controls.Add($smtp)
    $smtpBox = New-Object System.Windows.Forms.TextBox
    $smtpBox.Location = New-Object System.Drawing.Point(130,80)
    $smtpBox.Size = New-Object System.Drawing.Size(300,20)
    $formail.Add_Shown({$smtpBox.Select()})
    $formail.Controls.Add($smtpBox)
    $port = New-Object System.Windows.Forms.Label
    $port.Location = New-Object System.Drawing.Size(10,110) 
    $port.Size = New-Object System.Drawing.Size(120,20) 
    $port.Text = "Port:"
    $formail.Controls.Add($port)
    $portBox = New-Object System.Windows.Forms.TextBox
    $portBox.Location = New-Object System.Drawing.Point(130,110)
    $portBox.Size = New-Object System.Drawing.Size(300,20)
    $portBox.Text = '587'
    $formail.Add_Shown({$portBox.Select()})
    $formail.Controls.Add($portBox)
    $OKButton = New-Object System.Windows.Forms.Button
    $OKButton.Location = "150,160"
    $OKButton.Size = '100,30'
    $OKButton.Text = "Ok"
    $OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
    $formail.AcceptButton = $OKButton
    $formail.Controls.Add($OKButton)
    $result = $formail.ShowDialog()

    # setting credentials
    $usr = $addressBox.Text
    $pwd = ConvertTo-securestring $passwdBox.Text -AsPlainText -Force
    $credential = New-Object System.Management.Automation.PSCredential($usr,$pwd)

    # define email content
    $subject = 'TestMail.ps1'
    $body = "Questa mail è stata mandata da uno script PowerShell"

    # sending email
    $ErrorActionPreference= 'Stop'
    Try {
        Send-MailMessage    -From $addressBox.Text `
                            -To $addressBox.Text `
                            -Subject $subject `
                            -Body $body `
                            -SmtpServer $smtpBox.Text `
                            -UseSsl `
                            -Port $portBox.Text `
                            -Credential $credential
        $ErrorActionPreference= 'Inquire'
    }
    Catch {
        Write-Output "`nError: $($error[0].ToString())"
        $answ = [System.Windows.MessageBox]::Show("Sending alert email Failed",'WARNING','Ok','Warning')
    }   
}

在 cmdlet Send-MailMessage 中,我没有找到任何强制身份验证的参数。如何有效地发送电子邮件

解决方法

SMTP 服务器需要安全连接或客户端未通过身份验证。服务器响应为:5.5.1 需要身份验证:

按可能提供帮助的解决方案排序。

  1. 检查用户是否启用了 2fa,如果是,您将需要一个 apps password
  2. 检查您的Captcha loc
  3. 查看Xoauth2
,

我解决了这个问题。如here所述,我必须:

1 - 强制脚本使用 TLS 1.2

2 - 关闭两步验证并允许访问不安全的应用

感谢DaImTo的启发