Retrieving Attachments in POP3

May 30, 2014 at 7:35 AM
Hi, I need help to retrieve attachments from a mail message from a POP3 session.

Initially I am unable to get any attachments, except for inline images, I fiddled around and set MimeParser.Default.Filter.LoadMessageRawData = true, and managed to get an attached Excel file, however, I am unable to retrieve a ZIP file (which is password encrypted by the way) which was attached in the same email.

Please help! Thank you!
Coordinator
May 30, 2014 at 7:58 AM
I'm creating document how to use my library.
See it here.
http://library.higlabo.com/Document/Mail/Pop3.html

I think that you can get all files from AllFiles property of MailMessage object.
But it seems that you have some problem.
Can you elaborate mode detail of your problem?
May 30, 2014 at 8:14 AM

Thanks for the prompt reply.

I simply did the following:

using (HigLabo.Net.Pop3.Pop3Client cl = new HigLabo.Net.Pop3.Pop3Client(EMailServer, 110, EmailAddress, EmailPassword))

{

cl.ReceiveTimeout = 10000;

cl.Ssl = false;

if (!cl.Authenticate()) return;

try

{

Int64 cnt = cl.GetTotalMessageCount();

if (cnt <= 0) return;

for (Int64 x = 1; x <= cnt; x++)

{

HigLabo.Mime.MailMessage msg = cl.GetMessage(x);

String res = FindAttachments(msg.AttachmentFiles);

if (res == "") continue;

}

}

}

finally

{

cl.Close();

}

}

The FindAttachments routine:

private string FindAttachments(IEnumerable<HigLabo.Mime.MimeContent> ct)

{

String result = "";

foreach (HigLabo.Mime.MimeContent sct in ct)

{

if (sct.Contents.Count > 0)

result = AltFindAttachments(sct.Contents);

if (!sct.ContentDisposition.IsAttachment) continue;

FileInfo fi = new FileInfo(sct.FileName);

if (!fi.Extension.ToLower().Equals(".xls") && !fi.Extension.ToLower().Equals(".zip"))

{

result = result + String.Format("\r\n\r\nFile {0}:\r\nInvalid/Error", sct.FileName);

continue;

}

using (MemoryStream ms = new MemoryStream())

{

Byte[] data = sct.GetDecodedData();

ms.Write(data, 0, data.Length);

ms.Seek(0, 0);

if (fi.Extension.ToLower().Equals(".xls"))

result = result + String.Format("\r\n\r\nFile {0}:\r\n{1}", sct.FileName, ProcessExcelStream(ms));

else

{

ZipForge zip = new ZipForge();

zip.OpenArchive(ms, false);

if (ArchivePassword != "")

zip.Password = ArchivePassword;

ArchiveItem ai = new ArchiveItem();

Boolean proc = zip.FindFirst("*.xls", ref ai);

while (proc)

{

using (MemoryStream uz = new MemoryStream())

{

zip.ExtractToStream(ai.FileName, uz);

uz.Seek(0, 0);

result = result +

String.Format("\r\n\r\nFile {0}:\r\n{1}", ai.FileName, ProcessExcelStream(uz));

}

proc = zip.FindNext(ref ai);

}

zip.CloseArchive();

}

}

}

return result.Trim();

}

May 30, 2014 at 8:24 AM
I sent myself an email containing a excel file and an encrypted zip file containing the same excel file. Only the excel file can be retrieved. The zip file is not found as one of the attachments.
Coordinator
May 30, 2014 at 8:25 AM
Change here
String res = FindAttachments(msg.AttachmentFiles);
to
String res = FindAttachments(msg.AllFiles);
It may solve your problem.
May 30, 2014 at 8:26 AM
I tried both methods, same thing, only the excel file was retrieved. The zip file was not.
Coordinator
May 30, 2014 at 8:33 AM
Hi lilo73.

Could you send me the message source that cause this problem?
higuchihiguchihiguchi@hotmail.com
Coordinator
Jun 4, 2014 at 12:18 AM
Hi, everybody.
This issue was solved with lilo73 cooperation.

I uploaded new version that works fine without this problem.
You can get it from here
https://higlabo.codeplex.com/releases/view/122995
or get from Nuget ver1.0.0.3.
Jul 10, 2014 at 6:24 AM

Hi, I have encountered another problem. It has a FormatException. Exception message: One of the identified items was in an invalid format.

Coordinator
Jul 24, 2014 at 9:08 AM
Hi lilo73.

Please send me a mail that cause above problem.
I'll investigate it and want solve problem for you.

regards.
Jul 25, 2014 at 3:12 AM

Hi Higty, I couldn’t reproduce it. It was a one-off. Will send you a raw copy if it happens again. Thanks!

Coordinator
Jul 25, 2014 at 5:36 AM
Hi lilo73.

Good news.
Maybe some release solve this issue.

regards.