简体   繁体   English

如何通过 G-suite 从 Google Cloud 发送电子邮件

[英]How to send emails via G-suite from Google Cloud

How can you send an email in Go from the Google Cloud owner G-suite email account?如何从 Google Cloud 所有者 G-suite 电子邮件帐户在 Go 中发送电子邮件?

Is it possible to use the Google Cloud projectID existing authorizations, without specifying the Google account password inside the Go source files?是否可以使用 Google Cloud projectID 现有授权,而无需在 Go 源文件中指定 Google 帐户密码?

I found the solution!我找到了解决方案!

And it's very simple: instead of specifying the account password, you can restrict the connection to your server IP address .而且非常简单:您可以限制连接到您的服务器 IP 地址,而不是指定帐户密码。

1) Sign in to your Google Admin console ( https://admin.google.com ) using the G-suite administrator account 1) 使用 G-suite 管理员帐户登录您的 Google 管理控制台 ( https://admin.google.com )

2) Click on Apps -> G Suite -> Gmail -> Advanced settings 2) 点击应用-> G Suite -> Gmail ->高级设置

3) At the bottom of the page, mouse over on SMTP Relay Service and click on " ADD ANOTHER " 3) 在页面底部,将鼠标悬停在SMTP Relay Service 上并点击“ ADD ANOTHER

4) As Allowed Sender select " Only addresses in my domain " 4) 作为允许的发件人选择“仅我域中的地址

5) Check Only accept mail from the specified IP addresses and type your server IP address 5) 勾选只接受来自指定 IP 地址的邮件并输入您的服务器 IP 地址

6) Confirm by clicking on " ADD SETTING " and then " SAVE " 6) 点击“添加设置”,然后点击“保存”确认



This is the Go code required to send an email:这是发送电子邮件所需的 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)
}

An even better way is to create API/OAuth2 credentials directly in Google Cloud.更好的方法是直接在 Google Cloud 中创建 API/OAuth2 凭据。 In this way you don't even have to specify the server IP addresses as a security measure:通过这种方式,您甚至不必指定服务器 IP 地址作为安全措施:

https://medium.com/wesionary-team/sending-emails-with-go-golang-using-smtp-gmail-and-oauth2-185ee12ab306 https://medium.com/wesionary-team/sending-emails-with-go-golang-using-smtp-gmail-and-oauth2-185ee12ab306

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM