googleapis / google-api-dotnet-client

Google APIs Client Library for .NET
https://developers.google.com/api-client-library/dotnet
Apache License 2.0
1.33k stars 523 forks source link

I am having issue with Sending attachment with size more than 5 MB #1357

Closed anomepani closed 5 years ago

anomepani commented 5 years ago

I am having an issue with Sending attachment with size more than 5 MB in Dot Net using Gmail API. I am using GSuite account and want to extend this limit, How can I achieve this. If a working demo is available that would be very helpful.

jskeet commented 5 years ago

Please could you give more information about exactly what's happening? Please include your source code and the precise error you're seeing.

LindaLawton commented 5 years ago

Which upload type are you using? uploadType=media has a limit of max 5 mb you need to use either uploadType=multipart or uploadType=resumable if you want to attach larger files.

IIR there is a hard limit of attachments on gmail is 50 mb.

anomepani commented 5 years ago

SendEmail.cs Frontend Code for Send mail.txt

I have attached source code file which I am using for uploading a file and also added code for front end in which using jquery ajax I am uploading a file using Web API. Error Logs and stacktrace.txt

jskeet commented 5 years ago

Okay, I see you're using the Users.messages:send method, which has a documented 35MB limit - so it should be okay with "just a bit more than 5MB".

I suspect Linda's right, and the problem is that you're using the method that uses the uploadType=media approach. Instead of populating a Google.Apis.Gmail.v1.Data.Message with the Raw property, create a MemoryStream with the raw content of the data (not base64-encoded or anything - just the binary data) and pass that to the other overload of the Stream. You'll then need to call the Upload method on the returned object. This will use uploadType=resumable and manage the upload for you.

For more information on this sort of upload, see https://developers.google.com/gmail/api/guides/uploads

anomepani commented 5 years ago

I have reviewed, I didn't fully understand how to use it and don't find any sample demo or way to use this overloaded method for how I pass attachment as stream and for contentType which information I have to set. It would be very helpful if the sample example for uploadType=resumableis available.

jskeet commented 5 years ago

As per the linked documentation, the mime type would be "message/rfc822".

For the stream, you're already creating a MimeMessage - so write that to a MemoryStream, then rewind it:

MemoryStream stream = new MemoryStream();
mailMessage.WriteTo(stream);
stream.Position = 0;
// Pass stream into the Send method
anomepani commented 5 years ago

Hi, we have updated our code as per your requested changed Using overload method we are not getting any error but also we are not able to receive an email as well. I am not sure what missing from my side. Please see my attached updated code. UpdatedCode_With_Stream_Send_Mail.txt

jskeet commented 5 years ago

You're not calling the Upload method - that's the media upload equivalent of Execute, effectively.

anomepani commented 5 years ago

@jskeet Thank you very much for quick response. Now I am able to send message upto 35MB. So 35MB is hard limit for sending email using Gmail API. 35 MB Email message.

jskeet commented 5 years ago

Yes, that's the limit as per the documentation. Closing this issue now.

jskeet commented 4 years ago

@shiranZe: It's not clear to me what we're meant to take from your comment. As noted in earlier comments, the 35MB restriction is as documented.

keithtmccartney commented 1 year ago

Hi, we have updated our code as per your requested changed Using overload method we are not getting any error but also we are not able to receive an email as well. I am not sure what missing from my side. Please see my attached updated code. UpdatedCode_With_Stream_Send_Mail.txt

This thread has helped me out, your sample code here has been incredibly helpful; many thanks.