发送电子邮件时通过Golang应用程序使用Gmail时出现SMTP错误:534

huangapple go评论92阅读模式
英文:

smtp error: 534 when sending email through gmail in golang app

问题

在我的golang应用程序中,我使用这个代码片段从我的Gmail帐户发送电子邮件:

func send(body string) {
    from := "myaccount@gmail.com"
    pass := "mysupersecretpasswd"
    to := "whoever@whatever.com"

    msg := "From: " + from + "\n" +
        "To: " + to + "\n" +
        "Subject: Hello there\n\n" +
        body

    err := smtp.SendMail("smtp.gmail.com:587",
        smtp.PlainAuth("", from, pass, "smtp.gmail.com"),
        from, []string{to}, []byte(msg))

    if err != nil {
        log.Printf("smtp error: %s", err)
        return
    }

    log.Print("sent, visit whatever")
}

但是我收到了以下错误:

smtp error: 534 5.7.14 <https://accounts.google.com/signin/continue?sarp=1&amp;scc=1&amp;plt=AKgnsbud
5.7.14 lxVDnr-tXOckmxXi0fxExY5BKDRczBpGvMCpGWGF97jAI5DlM2oeGMDcIkvBhKT9rJAVnH
5.7.14 WP7zxssynLtkzMb35et-wxJF2AfeBhMA81QqMh2F8fkQRdf9GidA3swFnjfsUl0Pw6fiMT
5.7.14 b3zvEJyD6WAKEWcuxEGJIBTaqCtfDjipQ58cFJweUiKg1_4AJp0fGpC9ufnjBGWqWVKeW9
5.7.14 QVbUstROYK0SzjWXTTvsvZhhG3RjM> Please log in via your web browser and
5.7.14 then try again.
5.7.14  Learn more at
5.7.14  https://support.google.com/mail/answer/78754 61sm6182123wre.44 - gsmtp

我在本地主机上安装了postfix。我还允许不安全的应用程序访问我的帐户。我还在我的VPS上尝试了相同的代码,但仍无法发送电子邮件。

那么问题可能出在哪里?我该如何解决?

英文:

On my golang app I use this snippet to send email from my gmail account:

func send(body string) {
	from := &quot;myaccount@gmail.com&quot;
	pass := &quot;mysupersecretpasswd&quot;
	to := &quot;whoever@whatever.com&quot;

	msg := &quot;From: &quot; + from + &quot;\n&quot; +
		&quot;To: &quot; + to + &quot;\n&quot; +
		&quot;Subject: Hello there\n\n&quot; +
		body

	err := smtp.SendMail(&quot;smtp.gmail.com:587&quot;,
		smtp.PlainAuth(&quot;&quot;, from, pass, &quot;smtp.gmail.com&quot;),
		from, []string{to}, []byte(msg))

	if err != nil {
		log.Printf(&quot;smtp error: %s&quot;, err)
		return
	}
	
	log.Print(&quot;sent, visit whatever)
}

I get this error instead:

smtp error: 534 5.7.14 &lt;https://accounts.google.com/signin/continue?sarp=1&amp;scc=1&amp;plt=AKgnsbud
5.7.14 lxVDnr-tXOckmxXi0fxExY5BKDRczBpGvMCpGWGF97jAI5DlM2oeGMDcIkvBhKT9rJAVnH
5.7.14 WP7zxssynLtkzMb35et-wxJF2AfeBhMA81QqMh2F8fkQRdf9GidA3swFnjfsUl0Pw6fiMT
5.7.14 b3zvEJyD6WAKEWcuxEGJIBTaqCtfDjipQ58cFJweUiKg1_4AJp0fGpC9ufnjBGWqWVKeW9
5.7.14 QVbUstROYK0SzjWXTTvsvZhhG3RjM&gt; Please log in via your web browser and
5.7.14 then try again.
5.7.14  Learn more at
5.7.14  https://support.google.com/mail/answer/78754 61sm6182123wre.44 - gsmtp

I have installed postfix on my localhost. I have also allowed less secure apps to access my account.
I have also tried the same code on my VPS, but it does not send emails either.

So what could be wrong? How can I fix it?

答案1

得分: 2

我已经复制了你的代码片段,并替换了"from"、"to"和"password",它完美地运行起来了。这段代码无法工作的唯一原因是你没有允许在 Gmail 中使用不安全的应用程序。

英文:

I've copied your gist, replaced from, to and password and it works flawlessly. The only reason why this code is not working is that you are NOT allowing less secure apps in gmail.

答案2

得分: 0

谷歌移除了不安全的应用程序访问选项。这意味着你不能再使用 Gmail 的用户名和密码来通过代码发送电子邮件。

相反,你需要生成一个专用于应用程序的密码,并在代码中使用该密码,而不是你的 Gmail 帐户密码,这样更安全 发送电子邮件时通过Golang应用程序使用Gmail时出现SMTP错误:534

生成应用程序密码的步骤:

  1. 启用两步验证
    https://myaccount.google.com/signinoptions/two-step-verification/enroll-welcome
  2. 创建一个应用程序专用密码
    https://myaccount.google.com/apppasswords

这里查看详细信息和使用 Golang 发送电子邮件的示例。

英文:

Google removed less secure app access option. Mean, you can not use gmail’s username and password to send emails using code.

Instead you need to generate a password dedicated to an application, and use this password instead of your gmail account password within your code, which is more secure 发送电子邮件时通过Golang应用程序使用Gmail时出现SMTP错误:534

Making App Password:

  1. Enable 2FA
    https://myaccount.google.com/signinoptions/two-step-verification/enroll-welcome
  2. Create an app-specific password
    https://myaccount.google.com/apppasswords

See details and golang examples of how to send an email in here

huangapple
  • 本文由 发表于 2017年9月18日 12:05:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/46271449.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定