4
votes

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, "<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
and dont share your log in details with people :) - Serkan Hekimoglu
it is a test account .. no issue .. Thank you for reminding.. - Buddhi Dananjaya
how can I run this code? Does it work for downloading mails only from a gmail account? Is it possibile to convert the downloaded files after the download? - Nicholas

4 Answers

1
votes

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

0
votes

Where you write

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

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

for better works when you have more than one email in selected folder.

The [mailCount -1] is never read the last message.

-1
votes

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();
 }
}