本文介绍了googleapi:错误400:必需的收件人地址,invalidArgument的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行以下脚本来发送电子邮件.发件人和收件人替换了发件人和收件人地址.

I am running following script to send email message. The From and To addresses are replaced by sender and receiver.

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: [email protected]
To: [email protected]
Subject: Saying Hello
Date: Thu, 8 Oct 2020 09:55:06 -0600
Message-ID: <[email protected]>

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

Environment: macOS Catalina 10.15.7 go1.15.2 darwin/amd64

感谢帮助!

推荐答案

我相信您的目标如下.

  • 您想使用googleapis发送电子邮件给golang.
  • 您已经能够使用Gmail API收发电子邮件.
  • 在您的脚本中,我认为需要修改mailtext.在这种情况下,换行和空格很重要.
  • 尽管创建了raw,但并未将其放入请求正文中.
  • In your script, I think that mailtext is required to be modified. In this case, the line break and spaces are important.
  • Although raw is created, that is not put to the request body.

当以上几点反映到您的脚本中时,它如下所示.

When above points are reflected to your script, it becomes as follows.

    mailtext := `
    From: [email protected]
    To: [email protected]
    Subject: Saying Hello
    Date: Thu, 8 Oct 2020 09:55:06 -0600
    Message-ID: <[email protected]>

    This is a message just to say hello.
    So, "Hello".
    `
    raw :=  base64.URLEncoding.EncodeToString([]byte(mailtext))

     // create gmail.Message

     var message gmail.Message
     message.Id = "Msg 1"
     message.LabelIds = []string{"SENT"}

到:

    user := "me"
    // Modified
    mailtext := `From: [email protected]
To: [email protected]
Subject: Saying Hello
Date: Thu, 8 Oct 2020 09:55:06 -0600
Message-ID: <[email protected]>

This is a message just to say hello.
So, "Hello".`
    raw := base64.URLEncoding.EncodeToString([]byte(mailtext))

    // create gmail.Message

    var message gmail.Message
    message.Id = "Msg 1"
    message.LabelIds = []string{"SENT"}
    message.Raw = raw  // Added

  • 我认为在这种情况下,Date:Message-ID:可能会替换为新值.

    • I think that in this case, Date: and Message-ID: might be replaced with new values.

      或者,请按如下所示修改mailtext.

      Or, please modify mailtext as follows.

        mailtext := "From: [email protected]\nTo: [email protected]\nSubject: Saying Hello\nDate: Thu, 8 Oct 2020 09:55:06 -0600\nMessage-ID: <[email protected]>\n\nThis is a message just to say hello.\nSo, \"Hello\"."
      

    • 这篇关于googleapi:错误400:必需的收件人地址,invalidArgument的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 14:33