C# Creating and Sending Zip files with gmail - c#

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.

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.

Related

How to get MIME types of files within zip file without extracting and saving them on server?

In an ASP.NET Core 2.2 app I have the ability to upload a ZIP file. The contents of the ZIP file are extracted and saved in a directory. However, before saving the files on the server, I want to check their MIME types (a.k.a. content-types) to ensure that none of them are potentially dangerous files to store, such as EXE. If the ZIP file contains an unwanted file, I'd like to just show a model error on the page.
I tried to loop through the files within the zip to check their MIME types after storing the ZIP file in the directory. With this method, while I can see the file name with extension, I can't see the MIME type. Going by the extension alone isn't a good idea because it can be spoofed.
Directory.GetFiles(directory, "*.zip", SearchOption.TopDirectoryOnly).ToList()
.ForEach(zipFilePath =>
{
using (FileStream zipToOpen = new FileStream(zipFilePath, FileMode.Open))
{
using (ZipArchive archive = new ZipArchive(zipToOpen, ZipArchiveMode.Read))
{
foreach (ZipArchiveEntry entry in archive.Entries)
{
//entry does not contain MIME type, only filename with extension
}
}
}
});
Another solution would be to set the folder's permissions to deny execution, but I don't want to do that because it's something easy to forget.
Lastly, there is some way of storing files in an App_Data folder which isn't publicly available and so files in it can't be directly executed. The issue with that is I just can't find such a folder. It doesn't seem to be created automatically with my app. I'm thinking this must be a difference between ASP.NET and ASP.NET Core.

Can I ReadLock a file while I create/modify it using System.IO.File.WriteAllText() method?

I need to create a process that creates/modifies some text files in a folder. I am using below code to do that:
file = new System.IO.FileInfo(filePath);
file.Directory.Create();
System.IO.File.WriteAllText(file.FullName, "Some text...");
I have a Biztalk queue that looks into the text files in the folder every 2 minutes and picks up the files to process them. I want to lock the files when I am creating/modifying so that Biztalk wont try to process those files. How can I achieve this?
I read about Transactional NTFS in windows which will let me create Transaction context but windows documentation says this feature will deprecated and recommends not to use it.
If the file is on a local NTFS volume of CIFS share, the File Adapter will not attempt to read an open file. However,
A better pattern would be to do your file work in a temporary folder, then copy the completed files to the BizTalk folder only when they are done. That way, you don't have to worry about locking at all.
To acquire an exclusive lock you can use the file stream to do so
using (FileStream fs = new FileStream("Test.txt", FileMode.Append, FileAccess.Write, FileShare.None))
{
using (StreamWriter sw = new StreamWriter(fs))
{
sw.WriteLine("test");
}
}
This way you are locking the file exclusively for the current file stream. Any other application or even a new instance of file stream from another thread within the same application attempts to read or write to the file will be denied by the operating system.
In most cases, write file with different extension then rename the file works fine.

Access Denied When Creating a new File Using Filestream - Xamarin IOS

I am trying to create an empty PDF file in Xamarin.IOS using Filestream. When I try to create the Filestream using the following code:
FileStream fs = new FileStream("InvestmentAgreemen.pdf", FileMode.Create, FileAccess.ReadWrite);
I get the following error: "Access to the path "/private/var/containers/Bundle/Application/05AA1616-B66B-483D-8BA1-80A2B1AEC973/NewEPA.app/InvestmentAgreemen.pdf" is denied."
It worked fine when running on the iPad simulator, but as soon as I moved it to a real device I got this permissions error. Additionally, I have to use a filestream to create the empty PDF because I am also using Syncfusion's PDF tools for xamarin.IOS which require the use of Filestream for saving.
I also tried creating a FileIOPermission object granting AllAccess to AllFiles but I got a system not allowed error.
How can I resolve this permissions error?
You are trying to create a file in the root of your application's bundle, which is read-only. I'm surprised that this didn't fail on the simulator also.
To create a file at runtime, you need to specify a user writable path
var documents =
Environment.GetFolderPath (Environment.SpecialFolder.MyDocuments);
var filename = Path.Combine (documents, "InvestmentAgreemen.pdf");
Create your pdf within a writable directory of your application instead of the root directory of the bundle:
using (var docPath = NSFileManager.DefaultManager.GetUrl(NSSearchPathDirectory.CachesDirectory, NSSearchPathDomain.All, null, true, out var nsError))
using (var fileStream = new FileStream(Path.Combine(docPath.Path, "InvestmentAgreemen.pdf"), FileMode.Create, FileAccess.ReadWrite))
{
// do something with the file stream
}
I have created a sample which creates and saves the PDF document in Xamarin.IOS platform. Which can be downloaded from the below location:
http://www.syncfusion.com/downloads/support/directtrac/184238/ze/PDFSave1731207096
Please try the sample and let me know whether the issue is fixed.
Regards,
Surya Kumar

Base64encoded GZipStream is not matching windows [duplicate]

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.
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.

EWS save/export EmailMessage in other format

I'm working with the EWS Managed API 2.0.
At this moment I can save EmailMessages to my harddrive as *.eml files.
However I can't open them correctly to show the content.
How can an EmailMessage (.eml) be saved as an .html, .doc or .txt file directly?
Use following code if you want to save it as .eml
message.Load(new PropertySet(ItemSchema.MimeContent));
MimeContent mimcon = message.MimeContent;
FileStream fStream = new FileStream("c:\test.eml", FileMode.Create);
fStream.Write(mimcon.Content, 0, mimcon.Content.Length);
fStream.Close();
For msg file look at following link:
http://msdn.microsoft.com/en-us/library/cc463912%28EXCHG.80%29.aspx
After saving it as .eml file you can look at following post for parsing it:
Recommendations on parsing .eml files in C#
Hope its helpful.

Categories

Resources