JohnyMotorhead
JohnyMotorhead

Reputation: 821

Gmail attachment size limitation

We're implementing emails sending with gmail api C# .NET Using the Google .net client library. We send emails containing large attachments with size more than 7 MegaBytes, and everything works good

var mailMessage = new System.Net.Mail.MailMessage
                                  {
                                      Body = message.Body,
                                      Subject = message.Subject
                                  };
foreach (var to in message.To) mailMessage.To.Add(to.Address);
foreach (var a in message.Attachments) mailMessage.Attachments.Add(a);

var mimeMessage = MimeMessage.CreateFromMailMessage(mailMessage);

var gmailMessage = new Google.Apis.Gmail.v1.Data.Message { Raw = EncodeToBase64Url(mimeMessage.ToString()) };
await gmailService.Users.Messages.Send(gmailMessage, "me").ExecuteAsync().ConfigureAwait(false);

What confuses us is attachment size as we can't find information about it's limitations. Also due to Gmail documentation it has method "Upload" to add attachments and we try to understand when we must use it instead of simple send like we do (please see the code above).

Upvotes: 0

Views: 783

Answers (1)

Rafa Guillermo
Rafa Guillermo

Reputation: 15377

Answer:

Total attachment size per Gmail message must be lower than 25MB.

More Information:

From the documentation on sending attachments with Gmail:

You can send up to 25 MB in attachments. If you have more than one attachment, they can't add up to more than 25 MB.

As for upload methods:

  • Simple upload is designed for uploading small files, less than 5MB, when you can do the whole upload in one request.
POST /upload/gmail/v1/users/userId/messages/send?uploadType=media HTTP/1.1
Host: www.googleapis.com
Content-Type: message/rfc822
Content-Length: number_of_bytes_in_file
Authorization: Bearer your_auth_token

Email Message data
  • Multpart upload is for when you want to send metadata along with the upload data, such as what thread the message belongs to or what labels the message should have.
POST /upload/gmail/v1/users/userId/messages/send?uploadType=multipart HTTP/1.1
Host: www.googleapis.com
Authorization: Bearer your_auth_token
Content-Type: multipart/related; boundary=foo_bar_baz
Content-Length: number_of_bytes_in_entire_request_body

--foo_bar_baz
Content-Type: application/json; charset=UTF-8

{
  "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
}

--foo_bar_baz
Content-Type: message/rfc822

Email Message data
--foo_bar_baz--

The body of a multipart message should consist of a users.messages.attachments resource

References:

Upvotes: 1

Related Questions