Archived Forum Post

Index of archived forum posts

Question:

How do I delete an imap email visual basic

Jan 16 '13 at 09:42

Hi,

I can't seem to delete an email from imap server, either get an error or no error but nothing deleted. Also, I have given up on as not so important, but worth a mention, Download only UnRead emails, tried for hours again and no errors but always shown as read, I know the second its read it will flag but using exact copy's of examples no luck, wondered if something fundamental about my deleteing example below is reason this won't work either.

Tried a so many combinations of setflag & setmailflag my head is spinning.

I have tried the bundle method below & Dim Email As ChilkatEmail2 For i = 1 To N ' Download the email by sequence number. Set Email = imap.FetchSingle(i, bUid)

Emails download OK, I want to read, transfer to database & immediately delete from server.

' Select an IMAP mailbox success = imap.SelectMailbox("Inbox") If (success <> 1) Then MsgBox imap.LastErrorText Exit Sub End If

' We can choose to fetch UIDs or sequence numbers. fetchUids = 1

' Get the message IDs of all the emails in the mailbox Set messageSet = imap.Search("ALL", fetchUids) If (messageSet Is Nothing) Then MsgBox imap.LastErrorText Exit Sub End If

' Fetch the emails into a bundle object:

Set bundle = imap.FetchBundle(messageSet) If (bundle Is Nothing) Then

MsgBox imap.LastErrorText
Exit Sub

End If

' Loop over the bundle and display the FROM and SUBJECT of each.

For i = 0 To bundle.MessageCount - 1

Set Email = bundle.GetEmail(i)

' MsgBox imap.GetMailFlag(Email, "Seen")

Me.Subject = Email.Subject
Me.From = Email.From
Me.Contents = Email.Body
Me.Received = Email.EmailDate

' Me.To = Email.GetToAddr(i) Me.HeaderComplete = Email.Header ' MsgBox Email.NumHeaderFields ' & " " & Email.Uidl

   If InStr(1, Email.Body, "**Spam**") > 0 Then
    MsgBox "In here for delete"
    imap.SetMailFlag Email, "Deleted", 1

' success = imap.SetFlag(Email, "Deleted", 1) ' If (success <> 1) Then ' MsgBox imap.LastErrorText ' Exit Sub

    End If

' End If

DoCmd.GoToRecord , , acNewRec

Next

imap.Expunge

' Disconnect from the IMAP server. imap.Disconnect

' outFile.CloseExit_Command0_Click: Exit Sub

End Sub


Answer

  1. Make sure you test the return value of every Chilkat method call for success/failure. Did you test the return value of the call to SetMailFlag?

  2. Did you test the return value of the Expunge method call?

  3. Examine the imap.SessionLog. Set the imap.KeepSessionLog property equal to True (in VB.NET) or 1 (in VB6) and then examine the contents of the imap.SessionLog property after the Expunge. Does everything check out? Did the correct email's flag get set to Deleted?

  4. How are you testing whether the email was deleted? Are you using GMail? GMail can cause great confusion.


Answer

Hi, Thanks for your reply, I am not using GMAIL, it is via a UK server accessed through cPanel so think pretty standard. iMAP. I know it is not deleted because it is still on the server.

Narrowed it down a bit, below is an EXACT copy of the Chilkat visual Basic excamples.(I am using access 2007)

The first method works, the second method (I have rem'd out with a ') does NOT and causes an error including the line "No header ckx-imap-uid" I have found quite a few with similar issues & getting this "ckx-imap-uid" no header.

I also tried the examples to check for UN Read email with the setflag & setmailflag functions but never had any luck & gave up. Using exact copies of online examples in each case.

Whilst it is good I have a system that works I would like to know why & have the other method that seems to suit me better. Also I think relates to many of the other functions I am trying to use, checking flags etc.

Dim messageSet As messageSet

' We can choose to fetch UIDs or sequence numbers.

Dim fetchUids As Long fetchUids = 1 ' Get the message IDs of all the emails in the mailbox

Set messageSet = imap.Search("ALL", fetchUids)

If (messageSet Is Nothing) Then MsgBox imap.LastErrorText Exit Sub End If

' Fetch the emails into a bundle object:

Dim bundle As ChilkatEmailBundle2

Set bundle = imap.FetchBundle(messageSet)

If (bundle Is Nothing) Then

MsgBox imap.LastErrorText
Exit Sub

End If

'  To mark a complete set of emails for deletion, call SetFlags:
success = imap.SetFlags(messageSet, "Deleted", 1)
If (success <> 1) Then
    MsgBox imap.LastErrorText
    Exit Sub
End If

' Messages can also be marked for deletion individually: ' Loop over the bundle and mark each message for deletion.

'Dim i As Long

'For i = 0 To bundle.MessageCount - 1

' Dim Email As ChilkatEmail2

' Set Email = bundle.GetEmail(i)

'  To delete this email, set the "Deleted" flag to 1.
'  The email is not actually deleted until Expunge or
'  ExpungeAndClose is called.

' success = imap.SetMailFlag(Email, "Deleted", 1)

' If (success <> 1) Then

' MsgBox imap.LastErrorText

' Exit Sub ' End If

'Next

success = imap.ExpungeAndClose()

If (success <> 1) Then

MsgBox imap.LastErrorText

Exit Sub

End If

' Disconnect from the IMAP server. imap.Disconnect


Answer

There was a recent problem having to do with the "ckx-imap-*" headers not being set when FetchSingle was called. The imap.SetMailFlag method should have returned a failed status (0) if the ckx-imap-uid header could not be found.

The best way to quickly understand the source of a problem is to always check the return value of a Chilkat method (assuming it's not a "void" function), especially any method that might communicate with a server, or interact with the filesystem. When a method returns a failed status (null, 0, false, -1, etc.) then examine the contents of the LastErrorText.

Now that you've had a look at the LastErrorText and I know that the "ckx-imap-uid" header was missing, I immediately realize that it had to do with a recent problem that's already been fixed. Here's a new build for you to try:

http://www.chilkatsoft.com/preRelease/ChilkatImap.zip