Google SMTP 服务器出现故障

Google SMTP server fails

我在尝试使用 Google 的 SMTP 服务器时遇到以下错误。

535 5.7.8 Username and Password not accepted. Learn more at 5.7.8 https://support.google.com/mail/?p=BadCredentials fa15sm2375541pjb.40 - gsmtp

这是我的代码:

// Sender data.
    from := req.FormValue("email")
    //password := "xxxx" //<- log in password fails
    password := "xxxx" // <- app password fails

    // Receiver email address.
    to := []string{
        "myemail@gmail.com",
    }

    // smtp server configuration.
    smtpHost := "smtp.gmail.com"
    smtpPort := "587"

    msg := req.FormValue("name") + "\n" + req.FormValue("message")

    message := []byte(msg)

    auth := smtp.PlainAuth("", from, password, smtpHost)

    err := smtp.SendMail(smtpHost+":"+smtpPort, auth, from, to, message)
    if err != nil {
        tmp.Message = "Message not sent: " + err.Error()
        htmlTags["contact"] = tmp
        err = tmpl.ExecuteTemplate(w, "send_failure", htmlTags["contact"])
    } else {
        tmp.Message = "Message sent"
        htmlTags["contact"] = tmp
        err = tmpl.ExecuteTemplate(w, "send_success", htmlTags["contact"])
    }
} else {
    tmp.Message = "You message has not been sent. Cookies are required to send messages."
    htmlTags["contact"] = tmp
    err = tmpl.ExecuteTemplate(w, "send_failure", htmlTags["contact"])
}

该帐户启用了 2FA 并使用了应用程序密码。

允许安全性较低的应用程序:开启

发送代码也存在于具有自签名证书的服务器上,出现以下错误:

Certificate - missing
This site is missing a valid, trusted certificate (net::ERR_CERT_AUTHORITY_INVALID).

如果您的用户名和密码正确,这些是让 Gmail 从任何客户端运行的步骤。

# Visit https://accounts.google.com
#
# 1. Click on [Security] menu
# 2. Scroll to section [Less secure app access] and set it to ON
# 3. Scroll to section [Signing in to Google] and set [Use your phone to sign in] to OFF 
# 4. Scroll to section [Signing in to Google] and set [2-step Verification] to OFF

现在发送一个简单的 text/plain 电子邮件示例:

To: "Jane Doe" <jane@gmail.com>
From: "John Doe" <john@gmail.com>
Subject: A text/plain email
MIME-Version: 1.0 (Created with SublimeText 3)
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: 7bit

Good morning.

This is a message in text/plain format.
It contains text/plain.
It contains no base64 inline images or attachments.
Each line is no more than 76 characters in length.

Please reach out if you have any questions.


Thank you,

John Doe
SENIOR DEVELOPER
XYZ Solutions Inc.
P | 999.555.1234

然后,您可以使用 CURL 发送它:

# GMAIL TEST - text-plain.eml
curl --verbose -ssl smtps://smtp.gmail.com:465 --login-options AUTH=PLAIN --user john@gmail.com:passwword123 --mail-from john@gmail.com --mail-rcpt jane@gmail.com --mail-rcpt-allowfails --upload-file text-plain.eml