簡體   English   中英

用於發送 email 的腳本不適用於 Gmail 帳戶

[英]Script for sending email doesn't work with Gmail account

這里有一個發送 email 的簡單腳本。

If I run it using Gmail settings (ie smtp.gmail.com on port 465 or 587) the script doesn't work returning the error

服務器錯誤響應:5.7.0 Authentication Required

# 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中,我沒有找到任何強制身份驗證的參數。 如何有效發送 email?

SMTP 服務器需要安全連接或客戶端未通過身份驗證。 服務器響應是:5.5.1 需要身份驗證:

解決方案按可能提供幫助的順序排列。

  1. 檢查用戶是否啟用了 2fa,如果是,您將需要應用程序密碼
  2. 檢查您的驗證碼位置
  3. 查看Xoauth2

我解決了這個問題。 如此所述,我必須:

1 - 強制腳本使用 TLS 1.2

2 - 關閉雙重身份驗證並允許訪問不安全的應用程序

感謝DaImTo的啟發

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM