Saving Email Attachment Format

Topics: Issues
Nov 6, 2013 at 5:05 PM
I am having issue with data format of the attachment being saved. I am saving csv file from email to a specific path. File is being saved but when I open the file, i see data in a single row instead of a table (rows and columns) format.

Below is the code I am using.
ImapClient imap = new ImapClient();
                imap.Port = serverPort;
                imap.Host = serverName;

                if (imap.Connect())
                {
                    if (imap.Login(serverUserName, serverPassword))
                    {
                        ImapX.Folder myFolders = imap.Folders.Inbox;
                        if (myFolders != null)
                        {
                            String dts = DateTime.Now.Subtract(new TimeSpan(6, 0, 0)).ToString("d-MMM-yyyy", CultureInfo.InvariantCulture);
                            String searchQuery = String.Format("SINCE {0}", dts);

                            foreach (var mail in imap.Folders["INBOX"].Search(searchQuery, ImapX.Enums.MessageFetchMode.Full, 1))
                            {
                                if (mail.From.Address.ToString() == fromAddress && mail.Subject.Contains("Report"))
                                {
                                    if (mail.Attachments.Count() > 0)
                                    {
                                        for (int j = 0; j < mail.Attachments.Count(); j++)
                                        {
                                            string FullFileName = String.Empty;
                                            FullFileName = mail.Attachments[j].FileName;

                                            mail.Attachments[j].Save(filePath, FullFileName);
                                        }
                                    }
                                    return;
                                }
                                else
                                {
                                    return;
                                }
                            }

                        }
                    }
                }
Thanks!
Coordinator
Nov 7, 2013 at 9:56 AM
Hi prasanna_ms,

thank you for the report! I will check this issue later this weekend and provide an update.

Best regards,

Pavel
Nov 8, 2013 at 7:35 PM
Edited Nov 8, 2013 at 7:35 PM
I'm having a similar issue. Every time I use the save() I get the correct file downloaded to the folder, but the size is 0k and there is nothing in the file. I'm downloading PDFs that are attached to a Gmail account
  private static void GrabPDFAttachmentsFromIncommingFaxes(ImapX.Message[] msgs)
    {
        if (!System.IO.Directory.Exists(filePathForScanning + "\\pdfs\\"))
        {
            System.IO.Directory.CreateDirectory(filePathForScanning + "\\pdfs\\");
        }      
                for (int i = 0; i <= msgs.Count() - 1; i++)
                {
                    if (msgs[i].Attachments != null && msgs[i].Attachments.Count() > 0)
                    {
                        string attachmentName = msgs[i].Attachments[0].ContentType.Name.ToString();
                        msgs[i].Attachments[0].Save(filePathForScanning + "\\pdfs\\", attachmentName);                           
                        Console.WriteLine("Attachment: " + attachmentName);
                    }
                }
    }
Nov 8, 2013 at 8:41 PM
I found my error.

You have to add the Download() before the Save().
         If (msgs[i].Attachments != null && msgs[i].Attachments.Count() > 0)
                    {
                        string attachmentName = msgs[i].Attachments[0].ContentType.Name.ToString();
                        msgs[i].Attachments[0].Download();
                        msgs[i].Attachments[0].Save(filePathForScanning + "\\pdfs\\", attachmentName);                           
                        Console.WriteLine("Attachment: " + attachmentName);
                    }
Nov 13, 2013 at 8:04 PM
maylortaylor wrote:
I found my error.

You have to add the Download() before the Save().
         If (msgs[i].Attachments != null && msgs[i].Attachments.Count() > 0)
                    {
                        string attachmentName = msgs[i].Attachments[0].ContentType.Name.ToString();
                        msgs[i].Attachments[0].Download();
                        msgs[i].Attachments[0].Save(filePathForScanning + "\\pdfs\\", attachmentName);                           
                        Console.WriteLine("Attachment: " + attachmentName);
                    }
I'm still having the same problem even set like that. Text files come through as 0bytes and the byte array is 0bytes even after a download. PDF's are just corrupt. This is my source:
            if(Directory.Exists(savePath))
            {
                attachment.Download();
                attachment.Save(savePath);
            }
Coordinator
Nov 13, 2013 at 9:11 PM
Hi Carl64,

maylortaylor is right, however, there are minor issues with the attachment parsing which are in process to be fixed now. Please be patient, I will provide an update soon.

Best regards,

Pavel
Coordinator
Nov 27, 2013 at 9:47 PM
Hi prasanna_ms,

please check latest code in the repository. The issue you mentioned (https://imapx.codeplex.com/workitem/1830) is now fixed.

Kind regards,

Pavel
Jan 21, 2014 at 10:35 PM
I am downloading .csv files and there is no newline character between lines. In the previous versions this worked (but it is losing the initial characters), now it is returning one long string. Can this be fixed?
Coordinator
Jan 23, 2014 at 9:09 AM
Hi GeorgePlummer,

any way to send me these files or test credentials to p13a92@gmail.com ? The CSV files do not lose the newline characters when I test it, so seems it's a more individual case.

Greets,

Pavel