Example of creating a draft with an attachment in C#?

烈酒焚心 提交于 2019-12-13 22:17:52

问题


I've written a test application in C# that creates a draft message using the new Gmail API. It works fine when the message has no attachment.

I'm moving from the IMAP API and have used the MailBee.NET components with that API. The MailBee.NET components includes a class that produces an RFC 2822 message, so I've re-used this and have Base64-encoded the message and have assigned to the "Raw" property as described here:

https://developers.google.com/gmail/api/guides/drafts

        MailMessage msg = new MailMessage();
        msg.Subject = "test!";
        msg.BodyPlainText = "Test content";
        msg.Attachments.Add(@"D:\Trace.log", "Trace.log", Guid.NewGuid().ToString(), null, null, NewAttachmentOptions.Inline, MailTransferEncoding.Base64);

        Message m = new Message();
        m.Raw = Convert.ToBase64String(msg.GetMessageRawData());

        Draft d = new Draft();
        d.Message = m;
        d = gs.Users.Drafts.Create(d, "me").Execute();

It works fine when no attachment is added, but fails with a 500 response when one is added:

{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "backendError",
    "message": "Backend Error"
   }
  ],
  "code": 500,
  "message": "Backend Error"
 }
}

Could somebody please provide an example of how to do this using the .NET API? The example on the API page is very barebones and doesn't really give much in the way of useful information and the documentation isn't great. It would probably be best to use the Message / MessagePart / MessagePartBody classes included with the .NET Client, however I can't find any clear guidance or examples on their use so don't know where to begin.

Questions:

1) Can anybody provide some example code of creating a draft message with an attachment using the classes within the .NET Client?

2) Is it possible to attach more than one file? The documentation refers to a single file throughout and the Multipart guidance refers to exactly two parts: metadata and attachment.


回答1:


Providing a sample "raw" field that you're uploading would definitely be helpful to debug (either base64 encoded or just directly).

However this sounds related to: GMail API : unable to add an attachment in a draft




回答2:


also about this: m.Raw = Convert.ToBase64String(msg.GetMessageRawData());

you want to make sure you're using "web safe" (aka "url safe") base64 encoding alphabet from http://tools.ietf.org/html/rfc4648#section-5

as it says in the docs at the URL you mentioned: """ Your application can create drafts using the drafts.create method. The general process is to:

Create a MIME message that complies with RFC 2822. Convert the message to a URL-safe base64-encoded string. Create a draft, setting the value of the drafts.message.raw field to the encoded string. """

Google APIs use the




回答3:


Much like for the poster of the other question GmailGuy referred to, this has magically started working overnight. So it must've been a Gmail-side problem after all.

Regarding:

m.Raw = Convert.ToBase64String(msg.GetMessageRawData());

Thanks for the heads-up on this; I had actually encoded it previously but while trying 20 different things to get things working I removed it and forgot to add it back in!

Also, to confirm: yes, you're able to add more than one attachment when you use the raw message approach.



来源:https://stackoverflow.com/questions/24440521/example-of-creating-a-draft-with-an-attachment-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!