Click here to Skip to main content
15,886,422 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
Hi Everyone,

I've created a small C# application which extracts attachments from outlook emails and saves them to a network location. I can open all of the saved files except for compressed files with "zip" extension.

Files with the "zip" extension can only be opened/extracted via Windows Explorer. If I try to use WinRAR, I get the following error: "The archive is either in unknown format or damaged". WinZip does not work either. If however, I open the email within outlook manually and copy the zip file to my PC, I am able to open the zipped file using WinRAR, WinZip as well as windows explorer.

Part of the code used to save the attachment is shown below.

        Microsoft.Office.Interop.Outlook.MailItem _mailItem;<br />
        ...<br />
        ...       <br />
        _AttachFileName = _mailItem.Attachments[j].FileName;<br />
        _TempAttachmentFile = Path.Combine(_tempFolder.Path, _AttachFileName);<br />
<br />
        _mailItem.Attachments[j].SaveAsFile(_TempAttachmentFile);<br />
        ...<br />
        ...


Has anyone faced a similar issue with compressed zip files? If so were you able to resolve the issue?

Any ideas or help on this will be appreciated.

Thanks
Navnit
Posted
Updated 11-May-11 16:09pm
v2
Comments
CS2011 11-May-11 22:48pm    
Can you post what is the error message you are getting ?
CoolFijiKid 11-May-11 23:51pm    
WinRAR error:
"The archive is either in unknown format or damaged"

This is the peace of code i tried which works fine.

Can you please test it.

Application app = new Application();
            NameSpace nameSpace = app.GetNamespace("mapi");
            MAPIFolder defaultFolder = nameSpace.GetDefaultFolder(OlDefaultFolders.olFolderSentMail);
            foreach (MailItem item in defaultFolder.Items)
            {
                foreach (Attachment att in item.Attachments)
                {
                    if (att.FileName.EndsWith("zip", StringComparison.CurrentCultureIgnoreCase))
                    {
                        att.SaveAsFile(Path.Combine("c:\\delete me", att.FileName));
                        break;
                    }
                }
            }
            app.Quit();
 
Share this answer
 
How do you know it is a valid e-mail?
What happens if you save whole e-mail in a file, name is as *.eml and open in Outlook Express (bundled with all Windows versions)? Can you save and open binary files from e-mail sections.

Open the file with text editor. Even if the e-mail contains binary files, it's always a plain text file. You should see it content type "Content-Type: multipart/alternative", in case of zip your binary section should be a part with

Content-Type: application/octet-stream; name="yourfilename.zip"
Content-Disposition: attachment; filename="yourfilename.zip"
Content-Transfer-Encoding: base64


These line must be followed by an empty line followed by base64-encoded string. You should be able extract it manually and decode using System.Convert.FromBase64String.

Do you see something like that?

Look how processing of attachment is implemented here:
A POP3 Client in C# .NET[^] (it's about POP3, but you're interested only in the the structure of e-mail).

—SA
 
Share this answer
 
Hi Everyone,

Thanks for providing suggestions for my issue. I managed to get to the bottom of the issue. The problem I was having wasn't caused by the code I posted earlier.

After posting my issue on the forum, I had a closer look at what I was doing within my code and found that at a later stage of my application code, I was converting the attachment into base64 string for some internal processing. This was eventually getting saved back as a file to the final destination folder. It was during this final saving process I accidently reduced the data by a byte.

Code causing the issue:

<br />
FileStream outFile; <br />
..<br />
outFile.Write(binaryData, 0, binaryData.Length-1);<br />
..<br />


Fixed code:
<br />
FileStream outFile; <br />
..<br />
outFile.Write(binaryData, 0, binaryData.Length);<br />
..<br />


Apologies for the confusion.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900