Attachment issue

Topics: Issues
Aug 7, 2014 at 5:14 AM
I'm seeing an odd behavior with your package. Although it appears to see the attachments just fine when I download the message, it actually inserts the message body into the file when downloaded.

I've tried a number of approaches, including using anywhere from the 2.0 to 4.0 framework without success. Ideally, I'll target 2.0 as the app will run on an old server. Maybe its because the mailbox is on rackspace? Maybe its something that I'm doing?

Here's what I've tried...
client.Folders.Inbox.Messages.Download();

foreach (Message msg in client.Folders.Inbox.Messages)
{
// standard example
    foreach (var file in msg.Attachments)
    {
        if (!file.Downloaded)
        {
            file.Download();
        }
        file.Save(fileDownloadPath);
    }
    
// basic
    msg.Attachments[0].Download();
    string fName = msg.Attachments[0].FileName;
    byte[] fileData = msg.Attachments[0].FileData;
    File.WriteAllBytes(fileDownloadPath + "\\" + fName, fileData);

// use body parts
    foreach (var b in msg.BodyParts)
    {
        if (b.Size > 10)
        {
            b.Download();
            File.WriteAllText(fileDownloadPath + "\\" + fName, b.ContentStream);
        }
    }
}
Maybe I'm trying to "download" the message parts too many times, but I've tried about every combination that I can think of. I'd like to use this package due to its ease of use, but will probably have to use the limilabs.com assembly as its working well.
Coordinator
Aug 7, 2014 at 8:32 PM
Edited Aug 7, 2014 at 8:32 PM
Hi noffer,

by default, the client will fetch the messages using MessageFetchMode.Basic, which means, the it will download the body parts, but won't download the attachments. Based on this, you can change your code as follows:
client.Folders.Inbox.Messages.Download();

foreach (Message msg in client.Folders.Inbox.Messages)
{

      // Use msg.Body to access the message body (html / plain text)

      // Download all attachments and save to disk
      foreach (var file in msg.Attachments)
      {
            if (!file.Downloaded)
                  file.Download();
            file.Save(fileDownloadPath);
      }

      // Download all inline attachments/images and save to disk
      foreach (var file in msg.EmbeddedResources)
      {
            if (!file.Downloaded)
                  file.Download();
            file.Save(fileDownloadPath);
      }

}
What you can also do, is trying to connect to your server using the sample application (you can find it in the downloads section or by downloading the latest code from the repository).

If you still experience this issue, I'd ask you to make a log file as described in the documentation and send it to info@imapx.org .


Best regards,

Pavel
Aug 8, 2014 at 4:46 AM
Thanks for the reply,

I'm not sure what exactly changed, but using the msg.Attachments approach works this evening.

Here are the 3 items that were changed...
1) We dropped the body content alltogether in the emails, so they just have a single attachment
2) I think client.Behavior.MessageFetchMode = ImapX.Enums.MessageFetchMode.Full; was set
3) I cleaned up the code a bit so that the entire client object is wrapped in a using statement, which calls a Connect, Login, then process the messages in a single method in the class containing the email processing login.

Perhaps one or all 3 changes did the trick as I'm able to process all messages.

Thanks again for your continued support of this package!