问题描述
当我使用Gmail API Node.js客户端发送附件大于5 MB的电子邮件时,出现错误"413请求实体太大".
When I use the Gmail API Node.js client to send an email with an attachment larger than 5 MB I get an error "413 Request Entity Too Large".
我首先创建一个字符串mimeMessage,其中包含类型为multipart/mixed的MIME消息.该消息的一部分是大小大于5 MB的base64编码附件.然后,我尝试发送它:
I first create a string mimeMessage which contains a MIME message of type multipart/mixed. One part of this message is a base64 encoded attachment with a size > 5 MB. Then I try to send it:
gmail = google.gmail({ version: 'v1', auth: authentication });
encodedMimeMessage = Buffer.from(mimeMessage)
.toString('base64')
.replace(/\+/g, '-')
.replace(/\//g, '_')
.replace(/=+$/, '');
gmail.users.messages.send({
userId: 'me',
resource: { raw: encodedMimeMessage }
}, (err, res) => {
...
});
这将导致错误响应"413请求实体太大".
This results in an error response "413 Request Entity Too Large".
根据api文档,应使用可恢复的上传( https://developers.google.com/gmail/api/guides/uploads#resumable ).但是文档仅提供HTTP请求的示例,而没有描述如何使用Node.js客户端完成请求.我想避免将对google-api-nodejs-client的调用与HTTP请求混在一起.如果无法避免这一点,我将非常感谢一个很好的示例,说明如何在Node.js中执行此操作.
According to the api documentation a resumable upload should be used (https://developers.google.com/gmail/api/guides/uploads#resumable). But the documentation only gives examples for HTTP requests and does not describe how it can be done using the Node.js client. I would like to avoid mixing calls to the google-api-nodejs-client with HTTP requests. If this cannot be avoided I would highly appreciate a good example how to do this in Node.js.
我尝试将uploadType设置为可恢复:
I tried to set uploadType to resumable:
gmailApi.users.messages.send({
userId: 'me',
uploadType: 'resumable',
resource: { raw: encodedMimeMessage }
}, (err, res) => {
...
});
我从服务器响应中看到,它最终出现在查询字符串中,但是并不能解决问题.
I see from the server response that it ended up in the query string, but it did not solve the problem.
我在PHP中找到了示例(使用Gmail API发送大型附件, 如何使用gmail API发送大附件),Java( https://developers.google. com/api-client-library/java/google-api-java-client/media-upload )和Python().但是他们分别使用了"Google_Http_MediaFileUpload","MediaHttpUploader"和"MediaIoBaseUpload",我不知道如何将其应用于nodejs-client.
I found examples in PHP (Send large attachment with Gmail API, How to send big attachments with gmail API), Java (https://developers.google.com/api-client-library/java/google-api-java-client/media-upload) and Python (Error 10053 When Sending Large Attachments using Gmail API). But they are using a 'Google_Http_MediaFileUpload', 'MediaHttpUploader' and 'MediaIoBaseUpload' respectively and I do not know how to apply this to the nodejs-client.
我在Python中找到了一个示例(使用Gmail API来发送大于10mb的附件),该附件使用uploadType ='multipart'和未经base64编码的邮件.但是当我不对消息进行base64编码时,总是会收到错误响应.
I found an example in Python (Using Gmail API to Send Attachments Larger than 10mb) which uses uploadType = 'multipart' and a message that is not base64 encoded. But I always get an error response when I do not base64 encode the message.
推荐答案
如果其他人遇到此问题:发送电子邮件时,请不要使用第一个方法参数的resource.raw
属性.而是使用media
属性:
In case someone else comes across this problem: Do not use the resource.raw
property of the first method argument when sending an email. Instead use the media
property:
const request = {
userId: 'me',
resource: {},
media: {mimeType: 'message/rfc822', body: mimeMessage}
};
gmailApi.users.messages.send(request, (err, res) => {
...
});
在这种情况下,mimeMessage
不得使用base64进行编码.在resource
中,您可以选择指定属性threadId
.
The mimeMessage
must not be encoded with base64 in this case. In the resource
you can optionally specify the property threadId
.
const request = {
userId: 'me',
resource: {threadId: '...'},
media: {mimeType: 'message/rfc822', body: mimeMessage}
};
gmailApi.users.messages.send(request, (err, res) => {
...
});
这篇关于使用Gmail API Node.js客户端发送大型附件(大于5 MB)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!