Gmail api with .Net CLient library: Missing draft message [400]

I am trying to create drafts in Gmail using the .Net Client Library. I can successfully log in and get a list of drafts to work with authentication and api. Now I need to create an instance of the Draft class and send it to the API. But what does the draft message look like? No matter what I populate in the https://developers.google.com/gmail/api/v1/reference/users/drafts/create API , my project is always empty. Also, when I do this from my C # code, I need to set something else for the draft.Message.Raw field, I get an error message:

Missing draft message [400] 
+4
source share
3

, base64 , .

: https://developers.google.com/gmail/api/v1/reference/users/drafts

{
  "id": string,
  "message": users.messages Resource
}

"" , RCF 2822 base64.

:

from: me@email.com
to: you@email.com
subject: test email

email body

base64:

ZnJvbTogbWVAZW1haWwuY29tDQp0bzogeW91QGVtYWlsLmNvbQ0Kc3ViamVjdDogdGVzdCBlbWFpbA0KDQplbWFpbCBib2R5

, draft.create :

{
  "message": {
    "raw": "ZnJvbTogbWVAZW1haWwuY29tDQp0bzogeW91QGVtYWlsLmNvbQ0Kc3ViamVjdDogdGVzdCBlbWFpbA0KDQplbWFpbCBib2R5"
  }
}
+3

.

, , - . http://jason.pettys.name/2014/10/27/sending-email-with-the-gmail-api-in-net-c/

Jason nuget AE.Net.Mail RFC 2822.

, nugets

Install-Package Google.Apis.Gmail.v1
Install-Package AE.Net.Mail

static GmailService Service;
    public static void CriaService(string emailaConectar)
    {
        var certificate = new X509Certificate2(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, ClientCredentials.CertificatePath), ClientCredentials.ClientSecret, X509KeyStorageFlags.Exportable);

        var credential = new ServiceAccountCredential(
            new ServiceAccountCredential.Initializer(ClientCredentials.ServiceAccountEmail)
            {
                Scopes = new[] { GmailService.Scope.GmailCompose },
                User = emailaConectar
            }.FromCertificate(certificate)) { };

        Service = new GmailService(new BaseClientService.Initializer()
        {
            HttpClientInitializer = credential,
            ApplicationName = ClientCredentials.ApplicationName,
        });
    }

    private static string Base64UrlEncode(string input)
    {
        var inputBytes = System.Text.Encoding.ASCII.GetBytes(input);
        // Special "url-safe" base64 encode.
        return Convert.ToBase64String(inputBytes)
          .Replace('+', '-')
          .Replace('/', '_')
          .Replace("=", "");
    }

Main,

        CriaService("xpto@gmail.com");

        var msg = new AE.Net.Mail.MailMessage
        {
            Subject = "Your Subject",
            Body = "Hello, World, from Gmail API!",
            From = new MailAddress("xpto@gmail.com")
        };
        msg.To.Add(new MailAddress("someone@gmail.com"));
        msg.ReplyTo.Add(msg.From); 
        var msgStr = new StringWriter();
        msg.Save(msgStr);

        Message m = new Message();
        m.Raw = Base64UrlEncode(msgStr.ToString());

        var draft = new Draft();
        draft.Message = m;

        try
        {
            Service.Users.Drafts.Create(draft, "xpto@opportunity.com.br").Execute();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
+2
,

message > raw SMTP-.

{
    "message": {
      "raw": "From: me@example.com\nTo:you@example.com\nSubject:Ignore\n\nTest message\n"
}

message > payload:

{
    "message": {
      "payload": {
          "headers": {
              {"name": "From", "value": "me@example.com},
              {"name": "To", "value": "you@example.com"},
              {"name": "Subject", "value":"Ignore"}
           },
           "body": {
              "data": "Test message"
           }
       }
    }
}
0

Source: https://habr.com/ru/post/1547768/


All Articles