Retrieving Attachments

Sep 4, 2012 at 8:18 PM
Edited Sep 4, 2012 at 8: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

VGhpcyBpcyBhIHRlc3QhISE=

 

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
            }
        }
    }
}
Coordinator
Sep 5, 2012 at 8:04 AM

Hi,

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

Thanks

Coordinator
Sep 6, 2012 at 9:13 AM

ok, I see.

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

Coordinator
Sep 6, 2012 at 11:24 AM

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 2: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);
    fs.Close();
}

Sep 6, 2012 at 2: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.

Coordinator
Sep 11, 2012 at 2:06 PM

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

Sep 11, 2012 at 3: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 3: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.

Coordinator
Sep 11, 2012 at 4: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 7:53 PM

OK, sent another email

Coordinator
Sep 12, 2012 at 11:44 AM

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

Coordinator
Sep 12, 2012 at 4:08 PM
Edited Sep 13, 2012 at 8: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("imap.gmail.com", 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);