System.FormatException解码Gmail邮件的Raw字段

时间:2014-08-01 10:45:35

标签: c# .net base64 gmail-api

我正在尝试解码message.Raw字段,如下所示:

byte[] mailContent = Convert.FromBase64String(message.Raw);

但我得到System.FormatException

我在这里缺少什么?有必要做一些有意义的步骤吗?感谢。

**修改:**消息.Raw内容太大,无法在此处发布,因此我上传了example

2 个答案:

答案 0 :(得分:3)

message.Raw字段不仅Base64已编码,而且URL-safe已编码。

看看这个问题: Code for decoding/encoding a modified base64 URL

原始代码取自上面的链接:

///<summary>
/// Base 64 Encoding with URL and Filename Safe Alphabet using UTF-8 character set.
///</summary>
///<param name="str">The origianl string</param>
///<returns>The Base64 encoded string</returns>
public static string Base64ForUrlEncode(string str)
{
    byte[] encbuff = Encoding.UTF8.GetBytes(str);
    return HttpServerUtility.UrlTokenEncode(encbuff);
}
///<summary>
/// Decode Base64 encoded string with URL and Filename Safe Alphabet using UTF-8.
///</summary>
///<param name="str">Base64 code</param>
///<returns>The decoded string.</returns>
public static string Base64ForUrlDecode(string str)
{
    byte[] decbuff = HttpServerUtility.UrlTokenDecode(str);
    return Encoding.UTF8.GetString(decbuff);
}

答案 1 :(得分:1)

这是因为message.Raw的格式不正确。我在post找到了解决方案。