Having trouble reading the text/html message part

微笑、不失礼 提交于 2019-11-30 12:08:42

The body data appears to be base64url-encoded, not base64-encoded. The difference is the use of - and _, instead of + and /, in the encoding’s alphabet of 64 characters. One solution is to replace all - and _ characters with + and / respectively, before calling FromBase64String.

See http://tools.ietf.org/html/rfc4648#section-5

Here is the code I ended up using:

                foreach (MessagePart p in m.Payload.Parts)
                {
                    if (p.MimeType == "text/html")
                    {
                         byte[] data = FromBase64ForUrlString(p.Body.Data);
                         string decodedString = Encoding.UTF8.GetString(data);
                         Response.Write(decodedString);                            
                    }
                }

....

    public static byte[] FromBase64ForUrlString(string base64ForUrlInput)
    {
        int padChars = (base64ForUrlInput.Length % 4) == 0 ? 0 : (4 - (base64ForUrlInput.Length % 4));
        StringBuilder result = new StringBuilder(base64ForUrlInput, base64ForUrlInput.Length + padChars);
        result.Append(String.Empty.PadRight(padChars, '='));
        result.Replace('-', '+');
        result.Replace('_', '/');
        return Convert.FromBase64String(result.ToString());
    }

Good article http://www.codeproject.com/Tips/76650/Base-base-url-base-url-and-z-base-encoding

On this page https://developers.google.com/gmail/api/v1/reference/users/messages/attachments/get

you can find that there is a .NET example with this code for decoding:

// Converting from RFC 4648 base64-encoding
// see http://en.wikipedia.org/wiki/Base64#Implementations_and_history
String attachData = attachPart.Data.Replace('-', '+');
attachData = attachData.Replace('_', '/');
byte[] data = Convert.FromBase64String(attachData);
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!