This project is read-only.

miss-matching between the bodyparts and the ImapMailboxMessage.from

Apr 30, 2009 at 10:30 PM
Edited May 1, 2009 at 4:25 AM

Hello there,

I am experiencing some odd behavior which I am sure is a result of something I am not doing right...

I am reading in message from an INBOX. for each message I extract the HTML bodypart as well as sender, subject and etc. then I set the Deleted flag of the message to TRUE and at the end of the loop I call the command.Expunge method.

At some point, I am not sure why I am starting to get a miss-matching between the bodyparts and the ImapMailboxMessage.from/ and etc. it seems the bodyparts belong to a diferent message than other properties of the ImapMailboxMessage.

I just downloaded version and haven't had a chance to test with it. this was tested with

any help will be appreciated :-) thank you!!

Here are the relevant parts of the code:

mboxItem.ImapMailBox = mboxItem.ImapCommand.Fetch(mboxItem.ImapMailBox)

For Each imapMsg As ImapMailboxMessage In mboxItem.ImapMailBox.Messages

Dim htmlBody As String = String.Empty
Dim bodyStructure As ImapMailboxMessage = mboxItem.ImapCommand.FetchBodyStructure(imapMsg)

For i As Integer = 0 To bodyStructure.BodyParts.Count - 1

Dim bodyPart As ImapMailboxMessage = mboxItem.ImapCommand.FetchBodyPart(imapMsg, i) 

If imapMsg.HasHTML Then

If Not bodyPart.BodyParts(bodyPart.HTML).Data = Nothing AndAlso bodyPart.BodyPart (bodyPart.HTML).ContentType.MediaType = "text/html" Then
htmlBody = bodyPart.BodyParts(bodyPart.HTML).Data
Exit For
End If

If Not bodyPart.BodyParts(bodyPart.Text).Data = Nothing AndAlso bodyPart.BodyParts(bodyPart.Text).ContentType.MediaType = "text/html" Then
htmlBody = bodyPart.BodyParts(bodyPart.Text).Data
Exit For
End If

End If


If htmlBody <> String.Empty And htmlBody.Trim <> "" Then
           'at this point the imapMsg.From.Address is not of the same message as the HTML bodypart which was retrieved above
           Dim mailMsg As New MailMessage(imapMsg.From.Address, imapMsg.[To](0).Address, imapMsg.MessageID, htmlBody)
           mailMsg.Subject = imapMsg.Subject
           PersistMessage(db, MailBox, mailMsg)
'this method records data from this email message to the database
End If

    'set deleted flag to true
    mboxItem.ImapCommand.SetDeleted(imapMsg.ID, True)


'delete messages

Apr 30, 2009 at 10:31 PM
Edited May 1, 2009 at 4:26 AM
Also, I had to add this line to the ImapCommand.Expunge method. I know nothing about IMAP but this method freazed when it was called and many messages where flagged DELETED = true. this seems to overcome that. I am not sure if this change is related to the above problem...

if (response.IndexOf("OK EXPUNGE completed") > -1) { break; }

so it now looks like this:

public void Expunge()
    if (!(Connection.ConnectionState == ConnectionState.Open))
    string response;
     response = Connection.Read();
    if (response.IndexOf("OK EXPUNGE completed") > -1) { break; }
while (!(IsResponseEnd(response) || Regex.IsMatch(response, EXPUNGE_COMPLETE)));
May 1, 2009 at 11:15 PM
could it be because new message are arrving into the INBOX while I am looping through the ImapMailBox.Messages list?
May 8, 2009 at 4:30 PM


May 26, 2009 at 6:38 PM
Edited May 26, 2009 at 6:48 PM

Yea looks like what ever IMAP server your using has slightly different responses than the ones ive test on. If you know cant you tell me what mail server your using?

Anyway Ill get that added to the regex for expunge completed.


In ImapCommand update the following lines in private variables and that should fix the expunge without having to implement that fix you did:
const string OK_COMPLETE = @"^kw\d+\WOK\W([Ff][Ee][Tt][Cc][Hh]\W|[Ll][Ii][Ss][Tt]\W|)[Cc][Oo][Mm][Pp][Ll][Ee][Tt][Ee]([Dd]|)";
const string EXPUNGE_COMPLETE = @"^kw\d+\WOK\W([Ee][Xx][Pp][Uu][Nn][Gg][Ee]\W|)[Cc][Oo][Mm][Pp][Ll][Ee][Tt][Ee]([Dd]|)";

May 26, 2009 at 11:58 PM

thank you!! it is Exchange 2007 SP1