Archived Forum Post

Index of archived forum posts

Question:

IMAP move / read header / delete

Apr 14 '17 at 14:03

I am trying to write code to:

  1. look at each message in an inbox

  2. copy the message from the inbox into a subfolder based on the domain of the recipient (emails for numerous domains go into the inbox)

  3. Once copied, the message in the inbox should be deleted.

  4. This will eventually run on a timer, so the inbox is kept relatively clear

The problem I am having is that very occasionally messages seem to be copied OK, and as of yet I have not manages to delete the message from the inbox.

Here is my code. There could be syntax errors as I have stripped out quite a few lines...

    Dim imap As New Chilkat.Imap
    Dim success As Boolean

    success = imap.UnlockComponent("Anything for 30-day trial")
    If (success <> True) Then
        Console.WriteLine(imap.LastErrorText)
        Exit Sub
    End If

    imap.Ssl = False
    imap.Port = 143

    success = imap.Connect("myserver.com")
    If (success <> True) Then
        Console.WriteLine(imap.LastErrorText)
        Exit Sub
    End If

    success = imap.Login("myuser", "mypass")
    If (success <> True) Then
        Console.WriteLine(imap.LastErrorText)
        Exit Sub
    End If

    success = imap.SelectMailbox("Inbox")
    If (success <> True) Then
        Console.WriteLine(imap.LastErrorText)
        Exit Sub
    End If

    Dim messageSet As Chilkat.MessageSet
    Dim fetchUids As Boolean = True

    messageSet = imap.Search("ALL", fetchUids)
    If (messageSet Is Nothing) Then
        Console.WriteLine(imap.LastErrorText)
        Exit Sub
    End If

    Dim bundle As Chilkat.EmailBundle
    bundle = imap.FetchHeaders(messageSet)
    If (bundle Is Nothing) Then
        Console.WriteLine(imap.LastErrorText)
        Exit Sub
    End If

    Dim i As Integer
    For i = 0 To bundle.MessageCount - 1
        Dim email As Chilkat.Email
        email = bundle.GetEmail(i)

        Dim myData As Hashtable = New Hashtable

        myData("For") = email.GetHeaderField("X-Assp-Intended-For")
        myData("UID") = email.GetHeaderField("ckx-imap-Uid")

        If email.GetHeaderField("ckx-imap-isUid") = "YES" Then 
            myData("isUID") = True
        Else
            myData("isUID") = False
        End If

        'for simplicity, in this example, there is 1 recipient

        myData(1) = "recipient1@domain.com"
        myData("domain") = "domain.com"

        success = imap.SelectMailbox(myData("domain"))
        If (success <> True) Then
          Exit Sub
        End If

        success = imap.Copy(myData("UID"), myData("isUID"), myData("domain"))
        If (success <> True) Then
          Console.WriteLine(imap.LastErrorText)
          Exit Sub
        End If

        success = imap.SetMailFlag(email, "Deleted", 1)
        If (success <> True) Then
          Console.WriteLine(imap.LastErrorText)
          Exit Sub
        End If
    Next

   '  Expunge and close the mailbox.
    success = imap.Expunge
    If (success <> True) Then
      MsgBox(imap.LastErrorText)
      Exit Sub
    End If

    success = imap.Disconnect()

Answer

According to the Chilkat documentation for the SetMailFlag method, standard flags start with a backslash:

Both standard system flags as well as custom flags may be set. Standard system flags typically begin with a backslash character, such as "Deleted", "Seen", "Answered", "Flagged", "Draft", and "Answered". Custom flags can be anything, such as "NonJunk", "$label1", "$MailFlagBit1", etc..

So maybe using "\Deleted" in your SetMailFlag call will work?