Powershell 发送电子邮件的脚本不';我无法使用Gmail帐户

Powershell 发送电子邮件的脚本不';我无法使用Gmail帐户,powershell,smtp,gmail,smtp-auth,Powershell,Smtp,Gmail,Smtp Auth,这里有一个发送电子邮件的简单脚本 如果我使用Gmail设置(即端口465或587上的smtp.Gmail.com)运行它,脚本将无法返回错误 服务器错误响应:需要5.7.0身份验证 在cmdlet Send-MailMessage中,我未找到任何用于强制身份验证的参数。如何有效地发送电子邮件 SMTP服务器需要安全连接,或者客户端未通过身份验证。服务器响应为:5.5.1需要身份验证: 解决方案的顺序可能有助于 检查用户是否启用了2fa,如果启用,则需要 检查你的 调查 SMTP服务器需要安全连接

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

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

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

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

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

解决方案的顺序可能有助于

  • 检查用户是否启用了2fa,如果启用,则需要
  • 检查你的
  • 调查
  • SMTP服务器需要安全连接,或者客户端未通过身份验证。服务器响应为:5.5.1需要身份验证:

    解决方案的顺序可能有助于

  • 检查用户是否启用了2fa,如果启用,则需要
  • 检查你的
  • 调查

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

    1-强制脚本使用TLS 1.2

    2-关闭双因素身份验证并允许访问不安全的应用程序


    谢谢你的启发

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

    1-强制脚本使用TLS 1.2

    2-关闭双因素身份验证并允许访问不安全的应用程序


    感谢您的启发

    如果用户名/密码组合不正确(更有可能),或者帐户未被授权通过密码身份验证连接到SMTP(在具有严格安全策略的GSuite orgs中可能是这种情况),则也会发生此错误;-)请不要在问题中编辑解决方案公告。接受(即单击旁边的“勾选”)现有答案之一(如果有)。如果您的解决方案尚未包含在现有答案中,您还可以创建自己的答案,甚至可以接受它。我明白了,你就是这么做的。因此,请不要违背这个社区的习惯和既定机制。对不起,我认为在标题中添加“已解决”标志是一个好习惯,因为大多数社区都采用这种方法。好的@Yunnosch,我明白了。当然,在一个新社区中找到自己的路需要一些学习和时间。我理解。然而,你可以通过采取强烈推荐的方法来证明你是认真的。你已经走上了正轨,做出自己的答案并接受它。细节需要“打磨”。玩得开心。如果用户名/密码组合不正确(更有可能),或者帐户未被授权通过密码身份验证连接到SMTP(在具有严格安全策略的GSuite组织中可能是这种情况),也会发生此错误;-)请不要在问题中编辑解决方案公告。接受(即单击旁边的“勾选”)现有答案之一(如果有)。如果您的解决方案尚未包含在现有答案中,您还可以创建自己的答案,甚至可以接受它。我明白了,你就是这么做的。因此,请不要违背这个社区的习惯和既定机制。对不起,我认为在标题中添加“已解决”标志是一个好习惯,因为大多数社区都采用这种方法。好的@Yunnosch,我明白了。当然,在一个新社区中找到自己的路需要一些学习和时间。我理解。然而,你可以通过采取强烈推荐的方法来证明你是认真的。你已经走上了正轨,做出自己的答案并接受它。细节需要“打磨”。玩得开心。听起来很有趣,但是。。。(1) 不可行,因为脚本将从未定义数量的设备运行(2)不起作用(3)如何将Google API嵌入到PowerShell脚本中?我将尝试(1)看看它是否解决了您的问题。(3) 我甚至不确定我是否想尝试。听起来很有趣,但是。。。(1) 不可行,因为脚本将从未定义数量的设备运行(2)不起作用(3)如何将Google API嵌入到PowerShell脚本中?我将尝试(1)看看它是否解决了您的问题。(3) 我甚至不确定我是否想试试。
    # 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')
        }   
    }