Google API 发送带附件的电子邮件 - REST API 呼叫邮递员

Google API Send Email With Attachment - REST API Call Postman

我想创建一个 email/message throw Google API。 user messages send

我设法使用此端点创建了一封简单的电子邮件(仅文本)

POST https://gmail.googleapis.com/gmail/v1/users/{userId}/messages/send

并仅在正文中插入包含此信息的 Base64 编码字符串的“原始”字段,例如:


现在我正在尝试发送带有一些附件的电子邮件,但无法正常工作。 我只找到了 Java/Javascript 库的示例,但我想让它抛出标准 Rest Api 调用(现在我正在使用 Postman 来测试这个端点)。

首先,我必须使用https://developers.google.com/gmail/api/reference/rest/v1/users.messages/send 要么 POST https://gmail.googleapis.com/upload/gmail/v1/users/{userId}/messages/send

您能否留下一个包含正文和两个附件(例如两个 pdf)的电子邮件示例? 谢谢

如果您使用的是邮递员,我建议您按照以下步骤进行操作:

1.) 要使用的端点是:https://gmail.googleapis.com/upload/gmail/v1/users/me/messages/send?uploadType=multipart

2.)请求的内容类型头应该是:Content-Type: message/rfc822

3.) 请求正文应为:raw -> text

4.)正文内容格式如下:

Content-Type: multipart/mixed; boundary=foo_bar_baz
MIME-Version: 1.0
to: recipient@email.com
from: sender@email.com
subject: POSTMAN Rest API Execution

--foo_bar_baz
Content-Type: text/html; charset="UTF-8"
MIME-Version: 1.0

<h1>What is Lorem Ipsum?</h1>
<p style="color: darkred">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum<p>

--foo_bar_baz
Content-Type: application/pdf
MIME-Version: 1.0
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="Attachment_file.pdf"

JVBERi0xLjQKJdPr6eEKMSAwIG9iago8PC9UaXRsZSAoUHJvamVjdCBwcm9wb3NhbCkKL1Byb2R1Y2VyIChTa2lhL1BERiBtMTAzIEdvb2dsZSBEb2NzIFJlbmRlcmVyKT4+CmVuZG9iagozIDAgb2JqCjw8L2NhIDEKL0JNIC9Ob3JtYWw+PgplbmRvYmoKNyAwIG9iago8PC9UeXBlIC9YT2JqZWN0Ci9TdWJ0eXBlIC9JbWFnZQovV2lkdGggMTIwMAovSGVpZ2h0IDEyCi9Db2xvclNwYWNlIC9EZXZpY2VSR0IKL0JpdHNQZXJDb21wb25lbnQ==

--foo_bar_baz--

附带说明一下,我也遵循了 REST API 文档,但我收到了各种错误消息,因为我很困惑。但是,我发现了一个老问题,它能够帮助我制定使用 api 的正确方法......你可能想检查它 --> Mail attachment wrong media type Gmail API

如果您有时间,您可能还想阅读这篇文章 --> https://www.rfc-editor.org/rfc/rfc2046。阅读它帮助我克服了我正在经历的困惑,并让我清楚了为什么上述步骤有效。