2014-07-09 123 views
4

我正嘗試使用.Net客戶端庫在Gmail中創建草稿。我可以成功登錄並檢索草稿列表,以便驗證和API正在工作。現在我需要創建Draft類的一個實例並將其發送到API。但是草稿信息需要看起來像什麼?無論我在https://developers.google.com/gmail/api/v1/reference/users/drafts/create上填寫API Explorer,我的草稿始終爲空。 也從我的C#代碼做當我需要設置draft.Message.Raw場到別的我得到一個錯誤:使用.Net CLient庫的Gmail api:缺少草稿消息[400]

Missing draft message [400] 
+0

在ruby中有相同的問題http://stackoverflow.com/questions/25493213/creating-draft-via-google-gmail-api?noredirect=1#comment39790928_25493213 – barnett

回答

3

使用客戶端庫,你可以編碼電子郵件設置的base64到原始屬性然後使用該消息作爲草稿的消息屬性。

更一般地: 草稿消息由一個ID和一個消息資源 https://developers.google.com/gmail/api/v1/reference/users/drafts

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

消息資源應具有其「原始」字段設置爲base64編碼RCF 2822格式的字符串。

如:

from: [email protected] 
to: [email protected] 
subject: test email 

email body 

爲Base64編碼的字符串是:

ZnJvbTogbWVAZW1haWwuY29tDQp0bzogeW91QGVtYWlsLmNvbQ0Kc3ViamVjdDogdGVzdCBlbWFpbA0KDQplbWFpbCBib2R5 

所以一個draft.create的請求體應該是這個樣子:

{ 
    "message": { 
    "raw": "ZnJvbTogbWVAZW1haWwuY29tDQp0bzogeW91QGVtYWlsLmNvbQ0Kc3ViamVjdDogdGVzdCBlbWFpbA0KDQplbWFpbCBib2R5" 
    } 
} 
0

message > raw是預計將成爲完整的SMTP消息。

{ 
    "message": { 
     "raw": "From: [email protected]\nTo:[email protected]\nSubject:Ignore\n\nTest message\n" 
} 

另外,您還可以設置在message > payload相應的字段:

{ 
    "message": { 
     "payload": { 
      "headers": { 
       {"name": "From", "value": "[email protected]}, 
       {"name": "To", "value": "[email protected]"}, 
       {"name": "Subject", "value":"Ignore"} 
      }, 
      "body": { 
       "data": "Test message" 
      } 
     } 
    } 
} 
2

我這個苦苦掙扎,直到今天。

我所做的就是對這個鏈接上的解決方案進行修改以適應草稿。 http://jason.pettys.name/2014/10/27/sending-email-with-the-gmail-api-in-net-c/

賈森使用一種叫做AE.Net.Mail的NuGet序列化的郵件對象,以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("=", ""); 
    } 

而且在我的主要方法,我設計了這樣

 CriaService("[email protected]"); 

     var msg = new AE.Net.Mail.MailMessage 
     { 
      Subject = "Your Subject", 
      Body = "Hello, World, from Gmail API!", 
      From = new MailAddress("[email protected]") 
     }; 
     msg.To.Add(new MailAddress("[email protected]")); 
     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, "[email protected]").Execute(); 
     } 
     catch (Exception ex) 
     { 
      Console.WriteLine(ex.Message); 
     }