Java 在gmail api中向邮件添加附件的格式
我正在尝试使用gmail api()创建草稿 我发送请求机构如下:Java 在gmail api中向邮件添加附件的格式,java,gmail-api,Java,Gmail Api,我正在尝试使用gmail api()创建草稿 我发送请求机构如下: { "id": "001", "message": { "raw": "VG86IGZvb0BleGFtcGxlLmNvbQ0KU3ViamVjdDpJZ25vcmUNCg0KdGVzdCBtYWlsIGJvZHkNCg==", "payload": { "headers": [ ], "mimeType": "message/rfc822" }
{
"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
--##########
谢谢。保存草稿和附件的正确格式如下:
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中将其作为原始参数传递。已经有了答案,但仍然有一个问题:
[B@66d3c617
是一个byte[].toString()
。由于它是Base64,您可以执行新字符串(bytes,StandardCharsets.US\u ASCII)
。谢谢,是的,我在编码附加文件时遇到问题,这意味着我们应该如何编码?假设它是一个图像,首先将其转换为字节数组,然后将字节数组转换为blob,然后再转换为base 64。或者将文件转换为字节数组,并在附件字段中使用。上面是“内容传输编码:base64”因此,我只是假设字节数组是Base64 ASCII文本。我没有复杂的想法。我正在获取附加的图像,但它没有被打开,因为它说它在windows中已损坏。我尝试了以下方法,byte[]encoded=Base64.getEncoder().encode(fileContent);//文件内容是字节数组字符串pngData=new String(已编码,StandardCharsets.US_ASCII”);pngData=pngData.replace(“/”,“”);pngData=pngData.replace(“+”,“-”);pngData=pngData.replace(“=”,“*”);有3种不同类型,结尾有填充(=
)也可配置。Base64.getUrlEncoder()
使用-
i.o./+
。并且getMimeEncoder
可以将文本换行。字节数据是有效的png吗?