trying to send email with gmail api receive 'raw' RFC822 payload message string

前端 未结 2 1537
灰色年华
灰色年华 2021-01-17 04:26

I\'m trying to perform a simple Reply to email with Gmail API.

I\'m receiving the following error:

Google.GoogleApiException: \'Google.Apis.Re

相关标签:
2条回答
  • 2021-01-17 05:05

    Note that Users.messages: send method supports an /upload URI and accepts uploaded media with the following characteristics:

    • Maximum file size: 35MB
    • Accepted Media MIME types: message/rfc822

    Also, you may want to check details given in Sending Email,

    Emails are sent as base64url encoded strings within the raw property of a message resource. The high-level workflow to send an email is to:

    1. Create the email content in some convenient way and encode it as a base64url string.
    2. Create a new message resource and set its raw property to the base64url string you just created.
    3. Call messages.send, or, if sending a draft, drafts.send to send the message.

    With these, try adding the raw parameter in your request body with value set to a base64 encoded email.

    Just for additional insights (as these are not C# related), see these related SO posts

    • Failed sending mail through google api with javascript
    • Sending gmail attachment using api failed
    0 讨论(0)
  • 2021-01-17 05:23

    You could resort to using third party MIME library with below steps:

    1. Download the gmail message in raw format
    2. Parse the raw message using library
    3. Modify the message
    4. Output it back to raw format using library
    5. Insert back into the gmail message and then send.

    Have a look at this solution on SO https://stackoverflow.com/a/26599752/2637802

    Using the MimeKit library heres sample code:

    var msgRequest = service.Users.Messages.Get("me", msg.Id);
    msgRequest.Format = UsersResource.MessagesResource.GetRequest.FormatEnum.Raw;
    var msgDetails = msgRequest.Execute();
    
    using (MemoryStream rawInStream = new MemoryStream(Base64FUrlDecode(msgDetails.Raw)))
    using (MemoryStream rawOutStream = new MemoryStream())
    {
        var message = MimeKit.MimeMessage.Load(rawInStream);
    
        message.To.Clear();
        message.To.Add(new MimeKit.MailboxAddress("", "<email address>"));
        message.Subject = "Edited Subject";
    
        message.WriteTo(rawOutStream);
        msgDetails.Raw = Base64UrlEncode(rawOutStream.ToArray());
    }
    
    service.Users.Messages.Send(msgDetails, "me").Execute();
    
    private static byte[] Base64FUrlDecode(string input)
    {
        int padChars = (input.Length % 4) == 0 ? 0 : (4 - (input.Length % 4));
        StringBuilder result = new StringBuilder(input, input.Length + padChars);
        result.Append(String.Empty.PadRight(padChars, '='));
        result.Replace('-', '+');
        result.Replace('_', '/');
        return Convert.FromBase64String(result.ToString());
    }
    
    private static string Base64UrlEncode(byte[] input)
    {
        // Special "url-safe" base64 encode.
        return Convert.ToBase64String(input)
          .Replace('+', '-')
          .Replace('/', '_')
          .Replace("=", "");
    }
    
    0 讨论(0)
提交回复
热议问题