I’m only writing this note because I’ve literally been working on this for 4 hours and there’s zero documentation.
If you want to send an email with attachments that’s more than ~5mb using the .Net Gmail API, you’re out of luck…. but not really. It’s actually EASIER. I don’t know why in the heck they decided to do this, but here’s the code:
MimeKit.MimeMessage mimeMessage = MimeMessage.CreateFromMailMessage(msg); MemoryStream stream = new MemoryStream(); mimeMessage.WriteTo(stream); stream.Position = 0; var message1 = new Google.Apis.Gmail.v1.Data.Message(); try { var email2 = await service.Users.Messages.Send(message1, msg.From.Address, stream, "message/rfc822").UploadAsync(); return email2.Status.ToString(); } catch (Exception e) { return e.Message; }
Instead of sending it raw and doing all that idiotic conversion, you just make your mime message and push it as a stream using “Upload” instead of “Execute”. Seriously. WTF? There are about 1000000 examples of the Execute() version, but I couldn’t find a single one of the Upload().
Hours and Hours of wasted time.
I hope this saves someone a few minutes.