【发布时间】:2016-12-08 22:36:24
【问题描述】:
由于某种原因,我不知道如何使用 gmail 帐户、Appengine 和 Golang 发送电子邮件。
这是我所做的:
我去了 Google Cloud Platform > Appengine > 设置 > 选择项目,我在电子邮件 API 授权发件人上添加了 gmail 帐户。
我尝试使用来自 (https://golang.org/pkg/net/smtp/#pkg-examples) (func SendMail) 的代码来完成这项工作
package main
import (
"log"
"net/smtp"
)
func main() {
// Set up authentication information.
auth := smtp.PlainAuth("", "user@gmail.com", "password", "smtp.gmail.com")
// Connect to the server, authenticate, set the sender and recipient,
// and send the email all in one step.
to := []string{"recipient@example.net"}
msg := []byte("To: recipient@example.net\r\n" +
"Subject: discount Gophers!\r\n" +
"\r\n" +
"This is the email body.\r\n")
err := smtp.SendMail(smtp.gmail.com:587", auth, "sender@example.org", to, msg)
if err != nil {
log.Fatal(err)
}
}
在前端 (JavaScript) 上,我尝试运行此代码后收到不成功的响应。
我一直在 appengine 登台服务器上运行它
我尝试了不同的 smtp 服务器、端口、用户,但仍然无法正常工作 (support.google.com/a/answer/176600?hl=en)
我在 github 和其他一些博客上找到了一些示例,我尝试了它们,但并没有什么不同。 github.com/golang/go/wiki/SendingMail
nathanleclaire.com/blog/2013/12/17/sending-email-from-gmail-using-golang/
在所有示例中,一切看起来都很简单,但我肯定遗漏或误解了一些东西。
【问题讨论】:
-
仅供参考:我刚刚使用我的 gmail 帐户成功发送了一封包含您的代码的电子邮件。
-
您是在本地使用代码还是在 Google Appengine 上使用代码。应用引擎上的工作方式略有不同。
-
本地。嗯...那么我唯一的猜测是端口。 telnet smtp.gmail.com 587 可以在您的实例上工作吗?
-
我试过了,但它不会从 appengine 容器远程登录 smtp.gmail.com 587。我知道 Google Cloud Platform 上有一个 gmail api,但我希望在尝试之前使用 smtp 以简单的方式发送电子邮件。
-
是的,我就是这么想的。此外,我看到应用引擎不允许标准端口 (cloud.google.com/compute/docs/tutorials/sending-mail )。
标签: google-app-engine go smtp