MailSystem.Net: Fetching email that caused newmessagerecieved event to fire
I'm new using this component so bear with me. In short, I am trying to
capture the email that caused the newmessagerecieved event to fire. Here
is my code, is there anything obviously wrong that would cause this to
fail? It seems to me, through debugging, that it is failing when it sets
tempinbox to INBOX. Why is this?
Imports System.ComponentModel
Imports ActiveUp.Net.Mail
Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles
MyBase.Load
        Application_Start()
    End Sub
    Private Sub Application_Start()
        Dim worker = New BackgroundWorker()
        AddHandler worker.DoWork, AddressOf StartIdleProcess
        If worker.IsBusy Then
            worker.CancelAsync()
        End If
        worker.RunWorkerAsync()
    End Sub
    Dim _imap As Imap4Client
    Private Sub StartIdleProcess(sender As Object, e As DoWorkEventArgs)
        If _imap IsNot Nothing AndAlso _imap.IsConnected Then
            _imap.StopIdle()
            _imap.Disconnect()
        End If
        _imap = New Imap4Client()
        _imap.ConnectSsl("imap.gmail.com", 993)
        _imap.Login("user@gmail.com", "pass")
        Dim inbox = _imap.SelectMailbox("INBOX")
        AddHandler _imap.NewMessageReceived, AddressOf NewMessageReceived
        inbox.Subscribe()
        _imap.StartIdle()
    End Sub
Private Sub NewMessageReceived(source As Object, e As
NewMessageReceivedEventArgs)
        Dim _tempimap As Imap4Client = source
        Dim tempinbox = _tempimap.SelectMailbox("INBOX")
        Dim first As Integer = tempinbox.FirstUnseen
        Dim email As Message = tempinbox.Fetch.MessageObject(first + 1)
        MsgBox(email.BodyHtml.ToString)
    End Sub
End Class
 
No comments:
Post a Comment