Vladislav Kostenko
Vladislav Kostenko

Reputation: 1205

What is the size limit of email body in messages.send method of Gmail API?

I am using official .net api client to send emails with attachments by messages.send method. When I attach a file of size more than approximately 5mb, I've come to

[JsonReaderException: Unexpected character encountered while parsing value: <. Path '', line 0, position 0.]
   Newtonsoft.Json.JsonTextReader.ParseValue() +1187
   Newtonsoft.Json.JsonTextReader.ReadInternal() +65
   Newtonsoft.Json.JsonTextReader.Read() +28
   Newtonsoft.Json.Serialization.JsonSerializerInternalReader.ReadForType(JsonReader reader, JsonContract contract, Boolean hasConverter) +237
   Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent) +783
   Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType) +293
   Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings) +274
   Newtonsoft.Json.JsonConvert.DeserializeObject(String value, JsonSerializerSettings settings) +57
   Google.Apis.Json.NewtonsoftJsonSerializer.Deserialize(String input) in c:\code\google.com\google-api-dotnet-client\default\Tools\Google.Apis.Release\bin\Debug\test\default\Src\GoogleApis.Core\Apis\Json\NewtonsoftJsonSerializer.cs:120
   Google.Apis.Services.<DeserializeError>d__8.MoveNext() in c:\code\google.com\google-api-dotnet-client\default\Tools\Google.Apis.Release\bin\Debug\test\default\Src\GoogleApis\Apis\Services\BaseClientService.cs:286

[GoogleApiException: An Error occurred, but the error response could not be deserialized]
   Google.Apis.Requests.ClientServiceRequest`1.Execute() in c:\code\google.com\google-api-dotnet-client\default\Tools\Google.Apis.Release\bin\Debug\test\default\Src\GoogleApis\Apis\Requests\ClientServiceRequest.cs:102

I think client use Metadata URI, for metadata-only requests. A am going to try another option: Upload URI, for media upload requests. It looks like there is a limit on email size that leads to exception of parsing error response in the client library.

So, the first question: is there a size limit? Second, there is no info about how to use different upload methods via client, do you know any client library documentation?

Update: I hacked that request produced by

var request = gmailService.Users.Messages.Send(message, AccountUserId);

is going to https://www.googleapis.com/gmail/v1/users/me/messages/send. As I supposed it didn't use media upload request.

Upvotes: 2

Views: 1947

Answers (4)

jm.
jm.

Reputation: 23714

In response to the second part of your question...

Second, there is no info about how to use different upload methods via client, do you know any client library documentation?

I did a little poking around in the API, and I see that there is also a method that takes a stream as the 3rd parameter.

services.Users.Messages.Send( body, userId, stream, contentType)

Digging into the source code of that, I see that it seems to use a URL that looks like:

upload/....

I haven't tried it yet, and I don't know (yet) what it wants for a "stream", but this looks like a good possibility for getting a resumable upload with a bigger limit.

Upvotes: 0

Vladislav Kostenko
Vladislav Kostenko

Reputation: 1205

I ended up with limit on attachments total size. Here is code snippet.

Class level:

public const int MaxAttachmentsSize = 5 * 1024 * 1024;

Method level:

var attachmentsSize = 0;
foreach (var attachment in attachments)
{
    attachmentsSize += attachment.Item1;
    if (attachmentsSize > MaxAttachmentsSize) break;
    mailMessage.Attachments.Add(attachment.Item2);
}

Upvotes: 1

Eric D
Eric D

Reputation: 7159

For anything (uploading) over a few MB you should definitely use the /upload version of the method, otherwise yes you may run into those size limitations.

Upvotes: 0

Paweł Smołka
Paweł Smołka

Reputation: 638

There is limit in MB of whole message. Google API allows you for quite big email but it may get timeout when sending if your service because of connection speed will be doing it too long.

According to this google docs it is 35MB:

google api send docs

Upvotes: 0

Related Questions