How can I download message from mailbox

May 11, 2009 at 11:26 PM

Hello,

I cannot figure out what should I do to download message from mailbox.

It is no problem for me to download subject, size, date etc.

I was trying to download BodyPart.Data, but it is null. I can't find anything hwat help to do it.

Here is source code:


 ImapConnect connection = new ImapConnect("localhost");
ImapCommand command = new ImapCommand(connection);
ImapAuthenticate auth = new ImapAuthenticate(connection, user, password);
connection.Open();
  auth.Login();
  ImapMailbox mailbox = command.Select("Inbox");
  mailbox = command.Fetch(mailbox);
  string htmlBody = String.Empty;
  ImapMailboxMessage msg = mailbox.Messages[0];
  msg = command.FetchBodyStructure(msg);
  ImapMessageBodyPartList bodypart = msg.BodyParts;
  for(int i=0; i<bodypart.Count; i++)
  htmlBody = HttpUtility.HtmlEncode(bodypart[i].BodyPart) + " " + bodypart[i].Encoding;

 

When I'm checking mailbox from mailclient(like The Bat) everything is working.

I really need help. I have to do it in two days.

Thanks a lot!

Coordinator
May 26, 2009 at 5:24 PM

I'm guessing you missed the deadline but if your still intrested i would like to know what mail server your using. It could be that the client doesn't recogonize its reponses. When I started this project I had no idea that different IMAP servers were going to have such varring responses. If its not somthing different comming from the server you might want to throw a debug flag and walk through and make sure something not hitting a error which manafests it self as a null property.

May 26, 2009 at 5:56 PM

I'm using Dovecot as Imap server on Linux.

 I've figured it out how to download data.

This is my C# code:

ImapConnect connection = new ImapConnect(ipaddress);
  ImapCommand command = new ImapCommand(connection);
  ImapAuthenticate auth = new ImapAuthenticate(connection, user, password);
  connection.Open();
  auth.Login();
  string htmlbody = "";
  ImapMailbox mailbox = command.Select(skrzynka);
  mailbox = command.Fetch(mailbox);
  int i = mailbox.Messages.Count;
  ImapMailboxMessage msg = mailbox.Messages[i-1];  
  command.FetchBodyStructure(msg);
   
  if (msg.HasHTML)
  {
  msg.BodyParts[0].ContentEncoding = BodyPartEncoding.NONE;
  command.FetchBodyPart(msg, msg.HTML);
   
  foreach (ImapMessageBodyPart a in msg.BodyParts)
  {
  if ((a.Data != null) && a.ContentType.MediaType.Equals("text/html"))
  {
   
  a.ContentEncoding = BodyPartEncoding.UTF7;
  htmlbody = a.Data;
  }
  }
  }
  else
  if (msg.HasText)
  {
  command.FetchBodyPart(msg, msg.Text);
  foreach (ImapMessageBodyPart a in msg.BodyParts)
  {
  if ((a.Data != null) && a.ContentType.MediaType.Equals("text/html"))
  {
  htmlbody = a.Data;
  }
  }
  }
  auth.Logout();
  connection.Close();

I have another problem, which I can't solve. When I'm downloading data  Koolwired library remove special chars from message for example:

Original message: Some message=1

Returned data: Some message

When I'm sniffing connection by wireshark I see this chars.

In my opinion Koolwired Imap gets Encoding from Imap and  getting it wrong(always UTF-7).

As you see I've been trying to set Encoding manually, but it doesn't seem to help - a.ContentEncoding = BodyPartEncoding.UTF7;


Can You help me in that?

Coordinator
May 26, 2009 at 6:08 PM

The encoding is more a read property not really a set. Might need to step through that code and see whats happening.

May 26, 2009 at 6:27 PM

is it possible to get message without integrated encoding?

I can get message and encode it myself.

In applications like The Bat, Thunderbird user can manually choose encoding.

I'll be grateful if you check this code :)