Archived Forum Post

Index of archived forum posts

Question:

imap.CheckForNewEmail VB^ code example

Jul 18 '13 at 08:53

Can't get this to work in VB6 and can't find working VB6 code example.

Want to loop check gmail to notify me if any new emails arrive.

Thanks


Accepted Answer

I just tried the following code and it works for me with 9.4.0 (haven't tried 9.4.1 yet):

Option Explicit

Private Sub Command1_Click()
    Dim lo_Imap As CHILKATIMAPLib.ChilkatImap
    Dim lo_MsgSet As CHILKATIMAPLib.messageSet

    Set lo_Imap = New CHILKATIMAPLib.ChilkatImap

    ' Use your unlock code to unlock the component
    If lo_Imap.UnlockComponent("YOUR UNLOCK CODE") <> 1 Then
        Err.Raise vbObjectError, , "Could not unlock IMAP component." & vbNewLine & lo_Imap.LastErrorText
    End If

    With lo_Imap
        ' Setup connection settings (Example uses Gmail IMAP connection settings)
        .Port = 993
        .Ssl = 1

        ' Connect to the server (example uses GMail IMAP server)
        If .Connect("imap.gmail.com") <> 1 Then
           Err.Raise vbObjectError, , "Could not connect to IMAP server." & vbNewLine & .LastErrorText
        End If

        ' Use your e-mail username and password to login
        If .Login("myemail@gmail.com", "mypassword") <> 1 Then
            Err.Raise vbObjectError, , "Could not login to IMAP server." & vbNewLine & .LastErrorText
        End If

        ' Select the Inbox
        If .SelectMailbox("inbox") <> 1 Then
            Err.Raise vbObjectError, , "Could not select Inbox." & vbNewLine & .LastErrorText
        End If

        ' Get unread messages
        Set lo_MsgSet = .Search("UNSEEN", 1)
        If lo_MsgSet Is Nothing Then
            Err.Raise vbObjectError, , "Could not retrieve new messages." & vbNewLine & .LastErrorText
        End If

        ' Display the retrieved message count
        MsgBox "Found new message count: " & lo_MsgSet.Count
    End With
End Sub

Answer

Hmm, that worked with DLL but didn't work as ocx. Odd so switched coding to use dll rather than ocx

Thanks