IMAP in C # - Download letters and attachments

I tried this in C # using an open source project called "Koolwired.Imap" on sourceforge.

This was normal when downloading emails, but for attachments this is just a list of the attachment file name. Has anyone tried this?

If there is no other best free library that can do the same (I need a free / open source solution for this because I do this for my project on campus)

ImapConnect connection = new ImapConnect("imap.gmail.com", 993, true); ImapCommand command = new ImapCommand(connection); ImapAuthenticate auth = new ImapAuthenticate(connection, "<username>@gmail.com", "<password>"); connection.Open(); auth.Login(); string htmlbody = ""; ImapMailbox mailbox = command.Select("INBOX"); mailbox = command.Fetch(mailbox); int mailCount = mailbox.Messages.Count; for (int i = 0; i < mailCount ; i++) { ImapMailboxMessage msg = mailbox.Messages[mailCount - 1]; msg = command.FetchBodyStructure(msg); msg.BodyParts[0].ContentEncoding = BodyPartEncoding.NONE; msg = command.FetchBodyPart(msg, msg.HTML); foreach (ImapMessageBodyPart a in msg.BodyParts) { if (a.Data != null) { string fileName = ""; if (a.Attachment) fileName = ParseFileName(a.Disposition); string mimeType = a.ContentType.MediaType.ToLower(); a.ContentEncoding = BodyPartEncoding.UTF7; htmlbody = a.Data; } } } auth.Logout(); connection.Close(); 
+4
source share
5 answers

My choice is the interimap project on codeplex. It does a great job with applications.

+1
source
+1
source

ImapX is the best library. Works great with GMail. Dead is easy to use.

http://hellowebapps.com/products/imapx/

0
source

Where do you write

 ImapMailboxMessage msg = mailbox.Messages[mailCount - 1]; 

You can use ImapMailboxMessage msg = mailbox.Messages[i];

to improve performance if there are several addresses in the selected folder.

[mailCount -1] never reads the last message.

0
source

If you want to use it for a short period of time, use the chilkat IMAP API. you can save all email as an eml file and get enough samples to run any user. That he is fully operational for a month for free, after which his paid

At the same time, you want to separately download attachments using coolwired following

 ImapMailboxMessage mbStructure = new ImapMailboxMessage(); mbStructure = command.FetchBodyStructure(a); for (int j = 0; j < a.BodyParts.Count; j++) { //create dir if doesnot exist if (!Directory.Exists(path)) { DirectoryInfo di = Directory.CreateDirectory(path); } if (mbStructure.BodyParts[j].Attachment) { //Attachment command.FetchBodyPart(mbStructure, mbStructure.BodyParts.IndexOf(mbStructure.BodyParts[j])); //Write Binary File FileStream fs = new FileStream(path + mbStructure.BodyParts[j].FileName, FileMode.Create); fs.Write(mbStructure.BodyParts[j].DataBinary, 0, (int)mbStructure.BodyParts[j].DataBinary.Length); fs.Flush(); fs.Close(); } } 
-one
source

All Articles