Frage

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.

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;

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.

War es hilfreich?

Lösung

Quote from the MSDN:

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

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.

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

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top