This question already has answers here:
Having trouble reading the text/html message part
                                
                                    (3个答案)
                                
                        
                                5年前关闭。
            
                    
我正在尝试解码message.Raw字段是这样的:

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

但我得到一个System.FormatException

我在这里想念什么?有必要做一些其他步骤吗?谢谢。

**编辑:**消息。原始内容太大,无法在此处发布,因此我上载了example

最佳答案

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);
}

关于c# - System.FormatException解码Gmail邮件的Raw字段,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25078518/

10-15 03:23