go - 在 golang 中使用发送网格?

标签 go sendgrid

我使用 sendgrid 在 golang 中发送邮件。虽然状态代码返回 202,但电子邮件没有发送。有人可以帮助我吗?

package main

import (
    "fmt"
    "log"

    sendgrid "github.com/sendgrid/sendgrid-go"
    "github.com/sendgrid/sendgrid-go/helpers/mail"
)

func main() {
    from := mail.NewEmail("Example User", "test@example.com")
    subject := "Sending with SendGrid is Fun"
    to := mail.NewEmail("Example User", "trungduc08trungduc08@gmail.com")
    plainTextContent := "and easy to do anywhere, even with Go"
    htmlContent := "<strong>and easy to do anywhere, even with Go</strong>"
    message := mail.NewSingleEmail(from, subject, to, plainTextContent, htmlContent)
    client := sendgrid.NewSendClient("my api-key")
    response, err := client.Send(message)
    if err != nil {
        log.Println("err:", err)
    } else {
        fmt.Println("statusCode: ", response.StatusCode)
        fmt.Println("body: ", response.Body)
        fmt.Println("headers: ", response.Headers)
    }
}

最佳答案

我假设您已经设置了一个 SendGrid 帐户,如果没有,我会从那里开始,因为它有关于如何生成 API key 并测试您的电子邮件是否已由 SendGrid 发送的分步说明。

你的问题在这一行(上面的代码看起来是直接从 SendGrid 的例子中提取的,没有修改)

from := mail.NewEmail("Example User", "test@example.com")

电子邮件地址必须是您控制的有效电子邮件地址。

参见 https://app.sendgrid.com/guide/integrate/langs/go有关如何开始的更多详细信息。

如果上述方法不起作用,则可能还需要通过 SendGrid 进行验证/验证(当您在“营销”>“营销事件”>“发件人”>“创建新发件人”下指定用于发送电子邮件的电子邮件地址时,您会收到一封验证电子邮件您的 SendGrid 帐户),然后才能使用该电子邮件地址发送电子邮件。

关于go - 在 golang 中使用发送网格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54432728/

相关文章:

node.js - 使用 sendgrid 模块部署到 heroku 时出现 npm 错误

php - 尝试使用 SwiftMailer 和 Sendgrid 发送电子邮件时出现 fatal error

docker - Hyperledger Fabric 使用 go sdk 在 CouchDB 中存储交易

c# - 在门户编辑器中更新 csproj 文件 Azure Functions

Sendgrid BCC 无法正常工作

java - 在 Spring Boot 中发送 sendgrid 电子邮件的最简单方法

Golang 将多个文件添加到 http 多部分请求

go - 关闭 Go http.Client 的连接池

regex - Golang 正则表达式匹配 +-/*

go - 安装 Golang 时遇到的问题