views:

1615

answers:

4

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

It was OK when downloading mails, but for attachments it is only listing the file name of the attachment. Is there anyone who has tried this?

If not is there any other better free library which can do the same (I need a free/open source solution for this, because I am doing this for my campus project)

ImapConnect connection = new ImapConnect("imap.gmail.com", 993, true);
ImapCommand command = new ImapCommand(connection);
ImapAuthenticate auth = new ImapAuthenticate(connection, "[email protected]", "bigdick123");
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();
A: 

My choice is an interimap project on codeplex. It perfectly deals with attachments.

Nisus
A: 

If you want to use it for a short period please use the chilkat IMAP API. you can save the entire email as a eml file and therez enough sample to get anyone running. It it fully functional for a month free after which its paid

Simultaneously you want to seperately download attachments with coolwired use the 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();
 }
}                                  
Vicky Biswas
+1  A: 

I used this to read attachment from eml files. http://www.codeproject.com/KB/cs/mime_project.aspx?msg=3455831#xx3455831xx

Rajdip
A: 

ImapX is the best library. Works perfectly with GMail. Dead simple to use.

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

MartinHN