我正在运行以下脚本来发送电子邮件。From 和 To 地址被发送者和接收者替换。
func SendMail() {
b, err := ioutil.ReadFile("credentials.json")
if err != nil {
log.Fatalf("Unable to read client secret file: %v", err)
}
config, err := google.ConfigFromJSON(b, gmail.MailGoogleComScope)
if err != nil {
log.Fatalf("Unable to parse client secret file to config: %v",
err)
}
client := getClient(config)
srv, err := gmail.New(client)
if err != nil {
log.Fatalf("Unable to retrieve Gmail client: %v", err)
}
user := "me"
mailtext := `From: sender@gmail.com
To: receiver@gmail.com
Subject: Saying Hello
Date: Thu, 8 Oct 2020 09:55:06 -0600
Message-ID: <1234@local.machine.example>
This is a message just to say hello.
So, "Hello".
`
raw := base64.URLEncoding.EncodeToString([]byte(mailtext))
var message gmail.Message
message.Id = "Msg 2"
message.LabelIds = []string{"SENT"}
message.Raw = raw
doFunc := srv.Users.Messages.Send(user, &message)
m, err := doFunc.Do()
if err != nil {
println(err.Error())
println("Message not sent")
} else {
fmt.Printf("%+v", m)
}
该脚本报告以下错误:
googleapi: Error 400: Recipient address required, invalidArgument
环境:macOS Catalina 10.15.7 go1.15.2 darwin/amd64
感谢帮助!!!
阿波罗的战车
相关分类