克服gmails附件阻塞

时间:2014-12-21 20:32:54

标签: c# .net

我已经通过gmail向某人发送了一些rar-red可执行文件,我甚至无法下载自己的附件。我得到了:

Anti-virus warning - 1 attachment contains a virus or blocked file. Downloading this attachment is disabled.

我找到this并尝试在C#中实现相同的内容:

private static void Main(string[] args)
{
    var encodedString = System.IO.File.ReadAllText(@"D:\original.txt");
    var decodedString = Base64Decode(encodedString);
    var file = new System.IO.StreamWriter("d:\\result.rar");
    file.WriteLine(decodedString);

    file.Close();
}

public static string Base64Decode(string base64EncodedData)
{
    var base64EncodedBytes = System.Convert.FromBase64String(base64EncodedData);
    return System.Text.Encoding.UTF8.GetString(base64EncodedBytes);
}
不幸的是,由此产生的rar文件不能是红外的(D:\ result.rar:意外的归档结束)。我解码数据的方式有问题吗?我也不是百分之百确定将哪些数据放入original.txt。

我原来发送的电子邮件看起来像这样(其中......表示未显示数据以保持此帖子简短):

...
--047d7b2e141ddd6f7e04ff5a46b7--
--047d7b2e141ddd6fd404ff5a46b9
Content-Type: application/rar; name="Bla.rar"
Content-Disposition: attachment; filename="Bla.rar"
Content-Transfer-Encoding: base64
X-Attachment-Id: f_hy7mvnnm0

UmFyIRoHAM+QcwAADQAAAAAAAADyjXSAgCsAPD8BAO9bCQACZPAbLMVLsj4dMwsAIAAAAHhtbDJi
aWIuZXhlGAHRGQyJJhwSInMkmEkCQzAZBw4pgMAgIjAeMOKyLMXBZNYoQDUBBQRHiIjmHB05MkUx
...
cjR8qjjxbcI0s1b0SW86worLTu1860ysaZspuo8IlJZtnBtamJHLdGdp9YTcfkhsrnCba4l8o3oN
RiJTNE614Lb7GdfvySstNFD1eyVx1gexl8lax6hwn/8gxD17AEAHAA==
--047d7b2e141ddd6fd404ff5a46b9--

我用这个作为我的数据:

UmFyIRoHAM+QcwAADQAAAAAAAADyjXSAgCsAPD8BAO9bCQACZPAbLMVLsj4dMwsAIAAAAHhtbDJi
aWIuZXhlGAHRGQyJJhwSInMkmEkCQzAZBw4pgMAgIjAeMOKyLMXBZNYoQDUBBQRHiIjmHB05MkUx
...
cjR8qjjxbcI0s1b0SW86worLTu1860ysaZspuo8IlJZtnBtamJHLdGdp9YTcfkhsrnCba4l8o3oN
RiJTNE614Lb7GdfvySstNFD1eyVx1gexl8lax6hwn/8gxD17AEAHAA==

这是对的吗?

PS:

我选择的数据是正确的,我已经回答了我的问题。希望这将有助于未来的人。

3 个答案:

答案 0 :(得分:1)

请改为尝试:

    private static void Main(string[] args)
    {
        var encodedString = System.IO.File.ReadAllText(@"D:\original.txt");
        var bytes = System.Convert.FromBase64String(encodedString);
        System.IO.File.WriteAllBytes("d:\\result.rar", bytes);
    }

答案 1 :(得分:1)

使用Android设备上的Gmail应用程序下载文档。然后将设备连接到笔记本电脑,将文件从Android设备传输到笔记本电脑。

答案 2 :(得分:0)

采用here形式的DecodeWithString()可以解决问题。希望这将有助于将来的某个人。

PS:

这是代码:

public void DecodeWithString() {
   System.IO.StreamReader inFile;    
   string base64String;

   try {
      char[] base64CharArray;
      inFile = new System.IO.StreamReader(@"D:\original.txt",
                              System.Text.Encoding.ASCII);
      base64CharArray = new char[inFile.BaseStream.Length];
      inFile.Read(base64CharArray, 0, (int)inFile.BaseStream.Length);
      base64String = new string(base64CharArray);
   }
   catch (System.Exception exp) {
      // Error creating stream or reading from it.
      System.Console.WriteLine("{0}", exp.Message);
      return;
   }

   // Convert the Base64 UUEncoded input into binary output. 
   byte[] binaryData;
   try {
      binaryData = 
         System.Convert.FromBase64String(base64String);
   }
   catch (System.ArgumentNullException) {
      System.Console.WriteLine("Base 64 string is null.");
      return;
   }
   catch (System.FormatException) {
      System.Console.WriteLine("Base 64 string length is not " +
         "4 or is not an even multiple of 4." );
      return;
   }

   // Write out the decoded data.
   System.IO.FileStream outFile;
   try {
      outFile = new System.IO.FileStream("d:\\result.rar",
                                 System.IO.FileMode.Create,
                                 System.IO.FileAccess.Write);
      outFile.Write(binaryData, 0, binaryData.Length);
      outFile.Close();
   }
   catch (System.Exception exp) {
      // Error creating stream or writing to it.
      System.Console.WriteLine("{0}", exp.Message);
   }
}