如何在Javascript中使用GMAIL API发送带有附件PDF文件(大于10MB)的电子邮件。我尝试了一些代码的PDF文件附件和发送电子邮件如下所示。
const base64Data = 'Base64 PDF File string here (10 MB)'
const mixedB = 'mixedB';
const relatedB = 'relatedB';
const alternativeB = 'alternativeB';
const To = 'test@gmail.com'
const message = '<p>This is TEST EMAIL with PDF Attachment</p>'
const messageParts = [
'From: ' + 'vijay@test.com',
'To: ' + To,
`Subject: This is Test Email`,
'MIME-Version: 1.0',
'Content-Type: multipart/mixed; boundary="' + mixedB + '"',
'',
'--' + mixedB,
'Content-Type: multipart/related; boundary="' + relatedB + '"',
'',
'--' + relatedB,
'Content-Type: multipart/alternative; boundary="' + alternativeB + '"',
'',
'--' + alternativeB,
'Content-Type: text/html; charset=utf-8',
'',
message, // html content.
'',
'--' + alternativeB + '--',
'',
'--' + relatedB + '--',
'',
'--' + mixedB,
'Content-Type: application/pdf;name="attachedFile.pdf"',
'Content-Transfer-Encoding: base64',
'Content-Disposition: attachment;filename="attachedFile.pdf"',
'',
base64Data, // base64 data of the file.
'',
'--' + mixedB + '--'
]
let aaa = messageParts.join('\r\n')
var sendRequest = gapi.client.gmail.users.messages.send({
'userId': 'me',
'uploadType': 'multipart',
'resource': {
'raw': window.btoa(aaa).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '')
}
});
return sendRequest.execute();
我收到一封电子邮件,但附件中的PDF文件在下载后无法打开。
代码中有没有什么愚蠢的错误?请指点一下。
发布于 2021-03-17 17:59:32
你应该做一个Resumable Upload,而不是多部分的。
正如Upload options上提到的,可恢复上传更适合较大的文件:
可恢复上传: uploadType=resumable。对于可靠的传输,对于较大的文件尤其重要。
https://stackoverflow.com/questions/66653064
复制相似问题