如何从 Google Cloud 通过 G-suite 发送电子邮件
How to send emails via G-suite from Google Cloud
如何从 Google Cloud owner G-suite 电子邮件帐户用 Go 发送电子邮件?
是否可以使用现有的Google Cloud projectID授权,而无需在Go源文件中指定Google账号密码?
我找到了解决办法!
而且非常简单:无需指定帐户密码,您可以将连接限制为您的服务器 IP 地址。
1) 使用 G-suite 管理员帐户
登录您的 Google 管理控制台 (https://admin.google.com)
2) 点击 Apps -> G Suite -> Gmail -> 高级设置
3) 在页面底部,将鼠标悬停在 SMTP 中继服务 上,然后点击“添加另一个”
4) 作为允许的发件人 select “仅限我域中的地址”
5) 选中仅接受来自指定 IP 地址的邮件 并输入您的服务器 IP 地址
6) 点击“ADD SETTING”确认,然后点击“SAVE”
这是发送电子邮件所需的 Go 代码:
from := "myuser@mydomain.com"
to := "mail@recipient.com"
msg := "From: " + from + "\n" +
"To: " + to + "\n" +
"Subject: Hello there\n\n" +
"SOME TEXT"
err := smtp.SendMail("smtp-relay.gmail.com:587", nil,
from, []string{to}, []byte(msg))
if err != nil {
log.Printf("smtp error: %s", err)
}
更好的方法是直接在 Google 云中创建 API/OAuth2 凭据。
通过这种方式,您甚至不必指定服务器 IP 地址作为安全措施:
如何从 Google Cloud owner G-suite 电子邮件帐户用 Go 发送电子邮件?
是否可以使用现有的Google Cloud projectID授权,而无需在Go源文件中指定Google账号密码?
我找到了解决办法!
而且非常简单:无需指定帐户密码,您可以将连接限制为您的服务器 IP 地址。
1) 使用 G-suite 管理员帐户
登录您的 Google 管理控制台 (https://admin.google.com)2) 点击 Apps -> G Suite -> Gmail -> 高级设置
3) 在页面底部,将鼠标悬停在 SMTP 中继服务 上,然后点击“添加另一个”
4) 作为允许的发件人 select “仅限我域中的地址”
5) 选中仅接受来自指定 IP 地址的邮件 并输入您的服务器 IP 地址
6) 点击“ADD SETTING”确认,然后点击“SAVE”
这是发送电子邮件所需的 Go 代码:
from := "myuser@mydomain.com"
to := "mail@recipient.com"
msg := "From: " + from + "\n" +
"To: " + to + "\n" +
"Subject: Hello there\n\n" +
"SOME TEXT"
err := smtp.SendMail("smtp-relay.gmail.com:587", nil,
from, []string{to}, []byte(msg))
if err != nil {
log.Printf("smtp error: %s", err)
}
更好的方法是直接在 Google 云中创建 API/OAuth2 凭据。 通过这种方式,您甚至不必指定服务器 IP 地址作为安全措施: