1

Closed

Invalid Attachment Name Handling

description

For whatever reason, I seem to get messages with a colon (@":") in the attachment file name. This caused an exception on writing the file to disk with a filestream. Clearly, this was a filename issue, as my operating system, Windows, does not accept file names with colons (@":") in the file name. I was using the async IMAPMailboxManger to save attachments. There are a ton of ways to handle this exception. The best would probably be to handle this at the point that the data goes into the program -- when the attachment file name is parsed, but I chose to change the code in the IMAPMailboxManager save method. One fix for this is to change the code in the InterIMAP.Asynchronous.Client.IMAPMailboxManager.SaveAttachment()
 
Here is my updated code for the method that fixes file name issue when saving the file:
 
    public void SaveAttachment(IMessageContent content, string path)
    {
        string fn = content.ContentFilename;
        //MUST ERROR-PROOF INCOMING PARAMETER "content.ContentFilename" STRING PROPERTY VALUE:
        fn = fn.Replace(@"\", @"_");
        fn = fn.Replace(@"/", @"_");
        fn = fn.Replace(@":", @"_");
        fn = fn.Replace(@"*", @"_");
        fn = fn.Replace(@"?", @"_");
        fn = fn.Replace("\"", @"_");
        fn = fn.Replace(@"<", @"_");
        fn = fn.Replace(@">", @"_");
        fn = fn.Replace(@"|", @"_");
 
        //CONSTRUCT FILE PATH STRING, BUT ONLY AFTER ERROR CHECKING INCOMING PARAMETERS
        string p = String.Format("{0}\\{1}", path.TrimEnd('\\'), fn);

        //CREATE AND WRITE THE FileStream
        FileStream fstream = new FileStream(p, FileMode.Create);
        fstream.Write(content.BinaryData, 0, content.BinaryData.Length);
        fstream.Close();
    }
Closed Apr 5, 2010 at 12:14 PM by JasonMiesionczek
i included your fix into the library, but i wrote the replace logic as this:string fn = content.ContentFilename;fn = Regex.Replace(fn, "[\\/:\*?\"<>|]+", "_");

comments