This project is read-only.

Retrieving Attachments

Sep 4, 2012 at 9:18 PM
Edited Sep 4, 2012 at 9:21 PM

Hi, I have a test Gmail account with an email that contains a simple text file attachment. I can successfully retrieve the message but the attachment is not getting parsed / set.

The MessageContent for the bodypart that represents the file attachment has a ContentStream as per below:


--047d7b33daf4d1520204c8e57fb6Content-Type: text/plain; charset="US-ASCII"; name="New Text Document.txt"Content-Disposition: attachment; filename="New Text Document.txt"Content-Transfer-Encoding: base64X-Attachment-Id: f_h6per2w80



But all other properties are null (ContentType, ContentFileName, etc).

My code is pretty straightforward for testing attachments:


Folder folder = client.GetFolders()["INBOX"];
MessageCollection nativeMessages = folder.Search("ALL", true);
foreach (Message message in nativeMessages)
    message.Process(); // Retrieve flags and message parts
    if (message.Attachments != null && message.Attachments.Count > 0)
        foreach (Attachment att in message.Attachments) 
            if (!string.IsNullOrEmpty(att.FileName))
                Console.WriteLine(); // Never reached
Sep 5, 2012 at 9:04 AM


thank you for the feedback.

This was a common issue with attachments if the messages were sent through outlook, but I fixed this problem yesterday.

Please update to the latest version of ImapX (all files in the downloads section and also the source code were updated) and let us know if the problem still persists.

Sep 5, 2012 at 3:36 PM

I am using the latest source code (just checked again, no updates in svn) and I sent the files from Gmail to Gmail via Chrome (i.e. no Outlook was used at all). The problem still persists.


Sep 6, 2012 at 10:13 AM

ok, I see.

I'll let you know when the problem is fixed, shouldn't take much time.

Sep 6, 2012 at 12:24 PM

I updated the code, now the attachments should be parsed correctly.

Please check it and let me know if you notice any issues with that.

Sep 6, 2012 at 3:25 PM

OK looks like the attachments are getting parsed correctly but the file content is missing. I am doing a simple FileStream write. The FileData is filled with 0's.


string fileAndPath = Path.Combine(dir.FullName, file.FileName);
using (FileStream fs = new FileStream(fileAndPath, FileMode.Create, FileAccess.Write))
    fs.Write(file.FileData, 0, file.FileData.Length);

Sep 6, 2012 at 3:32 PM

Also, for some reason the attachment is getting duplicated 3 times. I have a message with 1 file and when I step through your code, there are 3 bodyparts that have the same info, causing 3 Attachments to be created, with the exact same info.

Sep 11, 2012 at 3:06 PM

There was a code update for the bug you described, please check the new version

Sep 11, 2012 at 4:07 PM

My apologies for the delay, I have been away. I have just updated my code, recompiled and reran my test. The results are the same (3 attachments objects, all exactly the same, all with zeroed bytes).

Sep 11, 2012 at 4:19 PM

Some more info: I have another email with an Excel file in it. While it still indicates 3 attachments (all 3 objects identical), I can download and open it successfully. So now the question remains why a simple text file isn't getting parsed correctly. My example is a simple text file with one line of text in it.

Sep 11, 2012 at 5:12 PM

The issue is really strange, 'cause everyone who's testing the library around this bug has different results. I still don't encounter the problem with duplicate attachments, and the aaa.txt file you sent me was parsed correctly.

Can you please send me another mail with an attachment, without any additions? Same as you test your code with.

Sep 11, 2012 at 8:53 PM

OK, sent another email

Sep 12, 2012 at 12:44 PM

Just tested the latest code with the mail you sent - everything works fine. No duplicates, both attachments parsed correctly. I sent you a screenshot

Sep 12, 2012 at 5:08 PM
Edited Sep 13, 2012 at 9:23 AM

I now understand what the problem is. See your code:

MessageCollection nativeMessages = folder.Search("ALL", true);
foreach (Message message in nativeMessages)
    message.Process(); // Retrieve flags and message parts
When you call the Search method on folder, you set the makeProcess parameter to true, if you do so, the messages are processed automatically, and there's no need to call the Process method on each message. So simply remove the message.Process() call from your code.
I'm using the following code (messages are fetched correctly, the FileData in attachments isn't empty):
var client = new ImapClient("", 993, true);

if (!client.Connection())
       MessageBox.Show("no connection"); return;
if (!client.LogIn("******", "******"))
       MessageBox.Show("login failed"); return;
var msgs = client.Folders["INBOX"].Search("ALL", true);