2015-09-25 80 views
5

我想使用Google的Gmail API發送消息。我已成功驗證身份,並試圖使用GmailService發送消息。如何創建Gmail API消息

我想用這個:

myService.Users.Messages.Send(myMessage, "me").Execute(); 

,其中爲myService是Google.Apis.Gmail.v1.GmailService和myMessage是Google.Apis.Gmail.v1.Data.Message

myService很好,我做了OAuth舞蹈。我可以從我的收件箱中收到消息。但我不知道如何構建myMessage。我有一個標準的.NET MailMessage,人類可讀的主題,正文,收件人,發件人等

但谷歌Message類需要的字段PayloadRaw。將完整的MailMessage轉換爲可以設置爲PayloadRaw屬性的字符串的最簡單方法是什麼?或者這不是我應該做的?

The documentation for the Message class

回答

7

我找到了解決方案。奇怪的是,.NET似乎不支持本地/輕鬆。雖然有一個很好的nuget包,名爲AE.Net.Mail,它可以爲流創建一個易於創建的消息對象。

Here's the sample code that pointed me in that direction

複製和粘貼代碼的網站似乎是下降,而谷歌的緩存可能不會永遠持續下去:

using System.IO; 
using System.Net.Mail; 
using Google.Apis.Gmail.v1; 
using Google.Apis.Gmail.v1.Data; 

public class TestEmail { 

    public void SendIt() { 
    var msg = new AE.Net.Mail.MailMessage { 
     Subject = "Your Subject", 
     Body = "Hello, World, from Gmail API!", 
     From = new MailAddress("[you]@gmail.com") 
    }; 
    msg.To.Add(new MailAddress("[email protected]")); 
    msg.ReplyTo.Add(msg.From); // Bounces without this!! 
    var msgStr = new StringWriter(); 
    msg.Save(msgStr); 

    var gmail = new GmailService(Context.GoogleOAuthInitializer); 
    var result = gmail.Users.Messages.Send(new Message { 
     Raw = Base64UrlEncode(msgStr.ToString()) 
    }, "me").Execute(); 
    Console.WriteLine("Message ID {0} sent.", result.Id); 
    } 

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

對於任何人在將來看這個,請確保您使用'AE.Net.Mail' Nuget包(https://www.nuget.org/packages/AE.Net.Mail/)而不是'System.Net.Mail'(https://msdn.microsoft.com/en-us/library/system.net.mail(v=vs.110).aspx)。它們非常相似,但工作方式稍有不同(例如'System.Net.Mail.MailMessage'沒有'Save'方法) – derekantrican

0

下面是使用使用MimeKit的替代版本。

public void SendEmail(MyInternalSystemEmailMessage email) 
{ 
    var mailMessage = new System.Net.Mail.MailMessage(); 
    mailMessage.From = new System.Net.Mail.MailAddress(email.FromAddress); 
    mailMessage.To.Add(email.ToRecipients); 
    mailMessage.ReplyToList.Add(email.FromAddress); 
    mailMessage.Subject = email.Subject; 
    mailMessage.Body = email.Body; 
    mailMessage.IsBodyHtml = email.IsHtml; 

    foreach (System.Net.Mail.Attachment attachment in email.Attachments) 
    { 
     mailMessage.Attachments.Add(attachment); 
    } 

    var mimeMessage = MimeKit.MimeMessage.CreateFromMailMessage(mailMessage); 

    var gmailMessage = new Google.Apis.Gmail.v1.Data.Message { 
     Raw = Encode(mimeMessage.ToString()) 
    }; 

    Google.Apis.Gmail.v1.UsersResource.MessagesResource.SendRequest request = service.Users.Messages.Send(gmailMessage, ServiceEmail); 

    request.Execute(); 
} 

public static string Encode(string text) 
{ 
    byte[] bytes = System.Text.Encoding.UTF8.GetBytes(text); 

    return System.Convert.ToBase64String(bytes) 
     .Replace('+', '-') 
     .Replace('/', '_') 
     .Replace("=", ""); 
} 

注:如果您收到一封電子郵件,反彈的問題,這可能是由於沒有設置ReplyToList領域。請參閱:GMail API Emails Bouncing