login about faq

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

asked Apr 13 at 08:59

varcarme's gravatar image

varcarme
1111


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?

link

answered Apr 13 at 11:51

jpbro's gravatar image

jpbro ♦
1.1k2618

edited Apr 14 at 14:03

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or __italic__
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×191
×13
×12

Asked: Apr 13 at 08:59

Seen: 271 times

Last updated: Apr 14 at 14:03

powered by OSQA