猿问

googleapi:错误 400:需要收件人地址,invalidArgument

我正在运行以下脚本来发送电子邮件。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


感谢帮助!!!


繁花不似锦
浏览 193回答 1
1回答

阿波罗的战车

我相信你的目标如下。您想使用 googleapis for golang 发送电子邮件。您已经能够使用 Gmail API 获取和发送电子邮件。修改点:在您的脚本中,我认为mailtext需要对其进行修改。在这种情况下,换行符和空格很重要。虽然raw已创建,但并未放入请求正文。当以上几点反映到您的脚本时,它变成如下。修改后的脚本:从:&nbsp; &nbsp; mailtext := `&nbsp; &nbsp; From: sender@gmail.com&nbsp; &nbsp; To: receiver@gmail.com&nbsp; &nbsp; Subject: Saying Hello&nbsp; &nbsp; Date: Thu, 8 Oct 2020 09:55:06 -0600&nbsp; &nbsp; Message-ID: <1234@local.machine.example>&nbsp; &nbsp; This is a message just to say hello.&nbsp; &nbsp; So, "Hello".&nbsp; &nbsp; `&nbsp; &nbsp; raw :=&nbsp; base64.URLEncoding.EncodeToString([]byte(mailtext))&nbsp; &nbsp; &nbsp;// create gmail.Message&nbsp; &nbsp; &nbsp;var message gmail.Message&nbsp; &nbsp; &nbsp;message.Id = "Msg 1"&nbsp; &nbsp; &nbsp;message.LabelIds = []string{"SENT"}至:&nbsp; &nbsp; user := "me"&nbsp; &nbsp; // Modified&nbsp; &nbsp; mailtext := `From: sender@gmail.comTo: receiver@gmail.comSubject: Saying HelloDate: Thu, 8 Oct 2020 09:55:06 -0600Message-ID: <1234@local.machine.example>This is a message just to say hello.So, "Hello".`&nbsp; &nbsp; raw := base64.URLEncoding.EncodeToString([]byte(mailtext))&nbsp; &nbsp; // create gmail.Message&nbsp; &nbsp; var message gmail.Message&nbsp; &nbsp; message.Id = "Msg 1"&nbsp; &nbsp; message.LabelIds = []string{"SENT"}&nbsp; &nbsp; message.Raw = raw&nbsp; // Added我认为在这种情况下,Date:可能Message-ID:会被新的价值观所取代。或者,请mailtext进行如下修改。&nbsp; mailtext := "From: sender@gmail.com\nTo: receiver@gmail.com\nSubject: Saying Hello\nDate: Thu, 8 Oct 2020 09:55:06 -0600\nMessage-ID: <1234@local.machine.example>\n\nThis is a message just to say hello.\nSo, \"Hello\"."
随时随地看视频慕课网APP

相关分类

Go
我要回答