亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

googleapi:錯誤 400:需要收件人地址,invalidArgument

googleapi:錯誤 400:需要收件人地址,invalidArgument

Go
繁花不似錦 2022-07-11 10:24:01
我正在運行以下腳本來發送電子郵件。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: [email protected]: [email protected]: Saying HelloDate: Thu, 8 Oct 2020 09:55:06 -0600Message-ID: <[email protected]>This is a message just to say hello.So, "Hello".`raw := base64.URLEncoding.EncodeToString([]byte(mailtext))var message gmail.Messagemessage.Id = "Msg 2"message.LabelIds = []string{"SENT"}message.Raw = rawdoFunc := 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感謝幫助!??!
查看完整描述

1 回答

?
阿波羅的戰車

TA貢獻1862條經驗 獲得超6個贊

我相信你的目標如下。

  • 您想使用 googleapis for golang 發送電子郵件。

  • 您已經能夠使用 Gmail API 獲取和發送電子郵件。

修改點:

  • 在您的腳本中,我認為mailtext需要對其進行修改。在這種情況下,換行符和空格很重要。

  • 雖然raw已創建,但并未放入請求正文。

當以上幾點反映到您的腳本時,它變成如下。

修改后的腳本:

從:

    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:會被新的價值觀所取代。


或者,請mailtext進行如下修改。


  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\"."



查看完整回答
反對 回復 2022-07-11
  • 1 回答
  • 0 關注
  • 219 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號