如何创建 Gmail API 消息

Posted

技术标签:

【中文标题】如何创建 Gmail API 消息【英文标题】:How to create a Gmail API Message 【发布时间】:2015-12-23 14:01:21 【问题描述】:

我想使用 Google 的 Gmail API 发送消息。我已成功验证,正在尝试使用 GmailService 发送消息。

我想用这个:

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,带有人类可读的主题、正文、收件人、发件人等。

但是 Google Message 类需要字段 PayloadRaw。将完整的MailMessage 转换为可以设置为PayloadRaw 属性的字符串的最简单方法是什么?或者这根本不是我应该做的?

The documentation for the Message class.

【问题讨论】:

【参考方案1】:

我找到了解决方案。奇怪的是,.NET 似乎并不本机/轻松地支持这一点。不过有一个不错的 nuget 包,称为 AE.Net.Mail,它可以将易于创建的消息对象写入流。

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

在站点似乎已关闭时复制和粘贴代码,并且 Google 的缓存可能不会永远存在:

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("yourbuddy@gmail.com"));
    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("=", "");
  

【讨论】:

对于将来查看此内容的任何人,请确保您使用的是 AE.Net.Mail Nuget 包 (nuget.org/packages/AE.Net.Mail) 而不是 System.Net.Mail (msdn.microsoft.com/en-us/library/system.net.mail(v=vs.110).aspx)。它们非常相似,但工作方式略有不同(例如System.Net.Mail.MailMessage 没有Save 方法)【参考方案2】:

这是使用 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

【讨论】:

【参考方案3】:

Gmail API 消息的 C# 代码(发送电子邮件)

   namespace GmailAPIApp
   
        class SendMail
        
              static string[] Scopes =  GmailService.Scope.GmailSend ;
              static string ApplicationName = "Gmail API .NET Quickstart";

             static void Main(string[] args)
             
                UserCredential credential;

                using (var stream =
                new FileStream("credentials_dev.json", FileMode.Open, 
                  FileAccess.Read))
                
                   string credPath = "token_Send.json";
                   credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                                GoogleClientSecrets.Load(stream).Secrets,
                                 Scopes,
                                 "user",
                                 CancellationToken.None,
                                 new FileDataStore(credPath, true)).Result;
                 Console.WriteLine("Credential file saved to: " + credPath);
               

        // Create Gmail API service.
        var service = new GmailService(new BaseClientService.Initializer()
        
            HttpClientInitializer = credential,
            ApplicationName = ApplicationName,
        );

        // Define parameters of request.           
        string plainText = "To:xxxx@gmail.com\r\n" +
                         "Subject: Gmail Send API Test\r\n" +
                         "Content-Type: text/html; charset=us-ascii\r\n\r\n" +
                         "<h1>TestGmail API Testing for sending <h1>";                          

        var newMsg = new Google.Apis.Gmail.v1.Data.Message();
        newMsg.Raw = SendMail.Base64UrlEncode(plainText.ToString());
        service.Users.Messages.Send(newMsg, "me").Execute();
        Console.Read();
    
    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("=", "");
        
       
 

【讨论】:

【参考方案4】:

这是对我有用的代码:

Private Async Sub BtnSendGmail_Click(sender As Object, e As EventArgs) Handles BtnSendGmail.Click

    Try
        Dim credential As UserCredential = Await GoogleWebAuthorizationBroker.AuthorizeAsync(New ClientSecrets With 
            .ClientId = "---------Your ClientId------------",
            .ClientSecret = "----------Your ClientSecret-----------"
        , GmailService.Scope.GmailSend, "user", CancellationToken.None, New FileDataStore(Me.GetType().ToString()))

        Dim service = New GmailService(New BaseClientService.Initializer() With 
            .HttpClientInitializer = credential,
            .ApplicationName = Me.GetType().ToString()
        )

        Dim plainText As String = "From: sender@gmail.com" & vbCrLf &
                                  "To: dest1@gmail.com," & "dest2@gmail.com" & vbCrLf &
                                  "Subject: This is the Subject" & vbCrLf &
                                  "Content-Type: text/html; charset=us-ascii" & vbCrLf & vbCrLf &
                                  "This is the message text."

        Dim newMsg = New Google.Apis.Gmail.v1.Data.Message With 
            .Raw = EncodeBase64(plainText.ToString())
        
        service.Users.Messages.Send(newMsg, "me").Execute()

        MessageBox.Show("Message Sent OK")
    Catch ex As Exception
        MessageBox.Show("Message failed :" & vbCrLf & "Source: " & ex.Source & vbCrLf & "HResult: " & ex.HResult & vbCrLf & "Message: " &
                        ex.Message & vbCrLf & "StackTrace: " & ex.StackTrace)
    End Try
End Sub


Public Shared Function EncodeBase64(ByVal text As String) As String
    ' Encodes a text-string for sending as an email message
    Dim bytes As Byte() = System.Text.Encoding.UTF8.GetBytes(text)
    Return System.Convert.ToBase64String(bytes).Replace("+"c, "-"c).Replace("/"c, "_"c).Replace("=", "")
End Function

【讨论】:

以上是关于如何创建 Gmail API 消息的主要内容,如果未能解决你的问题,请参考以下文章

使用 Gmail API 创建的草稿邮件中未呈现图像

如何使用 Gmail API 发送回复

如何仅为社交电子邮件创建 gmail-api 访问范围

带有 .Net 客户端库的 Gmail api:缺少草稿消息 [400]

如何在浏览器中打开使用 Gmail API 创建的草稿?

如何在没有用户交互的情况下使用 GMail API 创建草稿