在gmail api中添加邮件附件的格式

时间:2017-11-09 10:50:24

标签: java gmail-api

我正在尝试使用gmail api(link to api doc

创建草稿

我发送请求正文如下,

    {
  "id": "001",
  "message": {
    "raw": "VG86IGZvb0BleGFtcGxlLmNvbQ0KU3ViamVjdDpJZ25vcmUNCg0KdGVzdCBtYWlsIGJvZHkNCg==",
    "payload": {
      "headers": [

      ],
      "mimeType": "message/rfc822"
    }
  }
}

其中raw包含以下的base64编码字符串,

 To: foo@example.com
Subject:Ignore

test mail body

这工作正常,但我正在努力将附件添加到邮件中。我试过以下,但它不会工作,

 To: foo@example.com
Subject:Ignore

test mail body
Content-Type: multipart/mixed; boundary=##########


--##########

Content-Type: text/html; charset=UTF-8

 test mail body  

 --##########

Content-Type : image/png
Content-Disposition: attachment; filename=sample.png
Content-Transfer-Encoding: base64


[B@66d3c617

 --##########

感谢。

1 个答案:

答案 0 :(得分:1)

使用附件保存草稿的正确格式如下,

Content-Type: multipart/mixed; boundary="foo_bar_baz"
MIME-Version: 1.0
to: receiver@gmail.com
from: sender@gmail.com
subject: Subject Text

--foo_bar_baz
Content-Type: text/plain; charset="UTF-8"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit

The actual message text goes here

--foo_bar_baz
Content-Type: image/jpeg
MIME-Version: 1.0
Content-Transfer-Encoding: 
Content-Disposition: attachment; filename="example.jpg"

VG86IGZvb0BleGFtcGxlLmNvbQ0KU3ViamVjdDpJZ25vcmUNCg0KdGVzdCBtYWlsIGJvZHkNCg==

--foo_bar_baz--

将上面的代码编码为base64,然后在gmail api中将其作为原始参数传递。