C#创建和发送与Gmail Zip文件 [英] C# Creating and Sending Zip files with gmail

查看:169
本文介绍了C#创建和发送与Gmail Zip文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想ZIP和使用C#发送的.csv文件。
我创建的文件,压缩它们并使用Gmail的SMTP主机发送。

I'm trying to Zip and send .csv files using C#. I create the files, zip them and send them using an SMTP Gmail host.

有时可能需要这封电子邮件数个小时才能到达它的目的地。
为了测试我使用非常小的文件,所以大小不是问题。

It can sometimes take this email several hours to reach it's destination. For testing I am using very small files so size isn't an issue.

如果我尝试手动使用Gmail我得到发送这些拉链以下错误:
。myFile.csv.zip包含可执行文件出于安全考虑,Gmail不允许您发送这类文件

If I try to "manually" send these zips using gmail I get the following error: "myFile.csv.zip contains an executable file. For security reasons, Gmail does not allow you to send this type of file."

我觉得可能是我的压缩方法的问题,但它是非常简单的:

I think there might be a problem with my compression method but it's very straight forward:

string compressedFile = fileName + ".zip";

FileInfo fi = new FileInfo(fileName);
using (FileStream inFile = fi.OpenRead())
{
    using (FileStream outFile = File.Create(compressedFile))
    {
        using (GZipStream Compress = new GZipStream(outFile, CompressionMode.Compress))
        {
            inFile.CopyTo(Compress);
            Compress.Close();
        }
        outFile.Dispose();
    }
}
return compressedFile;



刚一说明:
。如果我采取相同的文件,手动ZIP或RAR它,我也没问题。这种情况与文本文件了。

Just a note: If I take the same file and manually Zip or rar it I have no problem. This happens with text files too.

推荐答案

报价从的 MSDN

压缩GZipStream对象写入文件以.gz的扩展,可以用许多常见的压缩工具

Compressed GZipStream objects written to a file with an extension of .gz can be decompressed using many common compression tools

的gzip文件是不是真正的ZIP文件进行解压缩。他们通常使用GZ为文件扩展名。您可以尝试,也许Gmail是非常严格的配对的扩展和压缩格式。

GZip files are not really "zip" files. They usually use "gz" as file extension. You might try that, maybe Gmail is very strict about "matching" extensions and compression formats.

如果您正在使用.NET 4.5,您可以选择使用的的 ZipArchive 类。那一个实际处理ZIP文件。

If you are using .NET 4.5 you can alternatively use the ZipArchive class. That one actually handles "zip" files.

这篇关于C#创建和发送与Gmail Zip文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆