英文:
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&scc=1&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 := "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)
}
I get this error instead:
smtp error: 534 5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&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
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 帐户密码,这样更安全
生成应用程序密码的步骤:
- 启用两步验证
https://myaccount.google.com/signinoptions/two-step-verification/enroll-welcome - 创建一个应用程序专用密码
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
Making App Password:
- Enable 2FA
https://myaccount.google.com/signinoptions/two-step-verification/enroll-welcome - Create an app-specific password
https://myaccount.google.com/apppasswords
See details and golang examples of how to send an email in here
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论