0
votes

I am trying to upload an email to gmail account using gmail REST API for import message https://developers.google.com/gmail/api/v1/reference/users/messages/import#http-request using python requests module. email is in RAW format (i.e. data of .eml file encoded with base64.b64encode()) with body

POST https://www.googleapis.com/gmail/v1/users/me/messages/import
{
 "raw": ".........."
}

This is working for RAW data size up to few MBs (say 5MB), but I am getting 'Connection Timeout' error if i try to upload bigger data (~10 MB or more).

Can anybody tell me the way to upload bigger email data using this API.

I am using OAuth access_token to call these APIs

1

1 Answers

0
votes

Try setting the uploadType to resumable

Resumable upload

To upload data files more reliably, you can use the resumable upload protocol. This protocol allows you to resume an upload operation after a communication failure has interrupted the flow of data. It is especially useful if you are transferring large files and the likelihood of a network interruption or some other transmission failure is high, for example, when uploading from a mobile client app. It can also reduce your bandwidth usage in the event of network failures because you don't have to restart large file uploads from the beginning.

Example: Resumable session initiation request

POST /upload/gmail/v1/users/userId/messages/send?uploadType=resumable HTTP/1.1
Host: www.googleapis.com
Authorization: Bearer your_auth_token
Content-Length: 38
Content-Type: application/json; charset=UTF-8
X-Upload-Content-Type: message/rfc822
X-Upload-Content-Length: 2000000

{
"id": string,
"threadId": string,
"labelIds": [
string
],
"snippet": string,
"historyId": unsigned long,
"payload": {
"partId": string,
"mimeType": string,
"filename": string,
"headers": [
{
"name": string,
"value": string
}
],
"body": users.messages.attachments Resource,
"parts": [
(MessagePart)
]
},
"sizeEstimate": integer,
"raw": bytes
}