This project is read-only.

Attachement not retrieved ?

Oct 8, 2009 at 5:10 PM
Edited Oct 8, 2009 at 5:18 PM

Hello,

 

I tried InterIMAP.Synchronous and it seems attachements are not retrieved. Is that correct ?

 

My code:

 

client = new IMAPClient(config, null, 1);
                List<IMAPFolder> folders = client.GetFoldersByName("INBOX", true);
                IMAPFolder inbox = folders[0];
                Console.Clear();
                Console.WriteLine("INBOX: " + inbox.Messages.Count + " messages");
                foreach (IMAPMessage m in inbox.Messages.Where(t => ((!t.Flags.Answered)
                    && (!t.Flags.Deleted)
                    && (!t.Flags.Draft)
                    )
                    ))
                {
                    m.RefreshData(true, true);
                    Console.Clear();
                    Console.WriteLine("From: " + m.From.First().DisplayName);
                    Console.WriteLine("Subject: " + m.Subject);
                    Console.WriteLine(m.Embedded.Count() + " inline files");
                    Console.WriteLine(m.Attachments.Count() + " attached files");
                    
                    if (m.Attachments.Count == 1)
                    {
                        IMAPFileAttachment a = m.Attachments.First();
                        string data = new ASCIIEncoding().GetString(a.FileData);
                        Console.WriteLine("DATA: " + data);
                    }
                    Pause();
                }

// Displays always 0 inline files and 0 attached files.

 

Thanks  a lot for your work.

Nov 13, 2009 at 1:22 PM

I may have experienced a similar issue but I am not sure if it was identical.

You did not specify how you instantiated the "config".

I ended up doing it like this:

 

config = new IMAPConfig(mailServer, string.Format("{0}/{1}/{2}", domain, userName, mailboxAlias), password, false, true, "");
config.AutoGetMsgID = false;
config.CacheFile = "";

 

After that, I do a search for unread messages (had to modify the lib for this) and do a refresh on each message and it consistently finds all attachments this way.

maybe it helps...

Nov 21, 2009 at 6:29 PM

this is not worked for me. in Asynchronous mode it will retrieve attachments. but in synchronous mode it wont work please help meeee...

 

Dec 25, 2009 at 8:26 PM

Yeah, I saw the same issues you are talking about.

Below is the change to the source code that I made let the "ProcessBodyParts" method property process the attachments.

I'm not sure how good of a fix this is as I have only tested it with my Google Apps Email Account.

In the InterIMAP.Synchronous.IMAP class, with the ProcessBodyParts(IMAPMessage msg) method.  Line 1484.

Originally it looks like this:

"else if (content.ContentId != null || content.ContentDescription != null)"

I changed it to:

"else if (content.ContentId != null || content.ContentDescription != null || disp.ToLower().Contains("attachment"))"

May 5, 2010 at 10:24 PM

I had a similar problem, attachments not downloading in google, fixed it using the following code.

I added the section that checks for "disp" containing "attachment;", also set disp to ToLower() when assigning.  The code is not that robust but it solved the problem I was having.

        /// <summary>
        /// Takes the IMAPMessageContent objects created in ProcessBodyStructure and downloads the content
        /// </summary>
        /// <param name="msg"></param>
        public void ProcessBodyParts(IMAPMessage msg)
        {
            List<IMAPMessageContent> processedContent = new List<IMAPMessageContent>();
            
            foreach (IMAPMessageContent content in msg._bodyParts)
            {
                if (content.ContentDisposition != null || content.ContentDescription != null)
                {
                    string disp = content.ContentDisposition == null ? "" : content.ContentDisposition.ToLower();

                    if (disp.Contains("inline") && (content.ContentId != null || content.ContentDescription !=null))
                    {
                        // this is an embedded image. 
                        IMAPFileAttachment image = new IMAPFileAttachment();
                        image.FileData = content.BinaryData;
                        image.FileEncoding = content.ContentTransferEncoding;
                        image.FileName = content.ContentId == null ? "" : content.ContentId.Replace("<", "").Replace(">", "");
                        if (content.ContentId == null && content.ContentDescription != null)
                            image.FileName = content.ContentDescription;
                        image.FileSize = content.BinaryData.Length;
                        image.FileType = content.ContentType.Substring(0, content.ContentType.IndexOf(";") > -1 ? content.ContentType.IndexOf(";") : content.ContentType.Length);
                        msg._embedded.Add(image);
                        processedContent.Add(content);
                    }
                    else if (content.ContentId != null || content.ContentDescription != null)
                    {
                        // this is an attached file
                        IMAPFileAttachment file = new IMAPFileAttachment();
                        file.FileData = content.BinaryData;
                        file.FileEncoding = content.ContentTransferEncoding;
                        file.FileName = content.ContentId== null ? "" : content.ContentId.Replace("<", "").Replace(">", "");
                        if (content.ContentId == null && content.ContentDescription != null)
                            file.FileName = content.ContentDescription;
                        file.FileSize = content.BinaryData.Length;
                        file.FileType = content.ContentType.Substring(0, content.ContentType.IndexOf(";") > -1 ? content.ContentType.IndexOf(";") : content.ContentType.Length);
                        msg._attachments.Add(file);
                        processedContent.Add(content);
                    }
						  /* PDC 2010-05-05 Added support for ContentDisposition containing "attachement; filename="{filename}""
							*/
						  else if (disp.Contains("attachment;"))
						  {
							  const string fileNameParam = "filename=";
							  int i = disp.IndexOf(fileNameParam);
							  if (i != -1)
							  {
								  i += fileNameParam.Length;
								  // this is an attached file
								  IMAPFileAttachment file = new IMAPFileAttachment();
								  file.FileData = content.BinaryData;
								  file.FileEncoding = content.ContentTransferEncoding;
								  file.FileName = content.ContentDisposition.Substring(i, content.ContentDisposition.Length - i);
								  file.FileName = file.FileName.Replace("\"", "");
								  if (content.ContentId == null && content.ContentDescription != null)
									  file.FileName = content.ContentDescription;
								  file.FileSize = content.BinaryData.Length;
								  file.FileType = content.ContentType.Substring(0, content.ContentType.IndexOf(";") > -1 ? content.ContentType.IndexOf(";") : content.ContentType.Length);
								  msg._attachments.Add(file);
								  processedContent.Add(content);
							  }
						  }
					 }
            }

            foreach (IMAPMessageContent content in processedContent)
                msg._bodyParts.Remove(content);
            
            msg.ContentLoaded = true;
        }        
May 11, 2010 at 2:34 PM

Hi,

i'm using this library and it works fine, but with attachments i also have a small problem. My attachments are not detected. I have used the new code, but my content.ContentDisposition and my content.ContentDescription is null, so he isn't using the new code that checks for attachments. Any ideas what that could be??

 

Nico