0

我正在尝试使用 S22.Imap 库(https://github.com/smiley22/S22.Imap)来收听新消息。我已按照提供的示例进行操作,但需要帮助...根据我所阅读的内容,ImapClient 对象应位于后台并侦听新消息(使用 IDLE 功能)并在收到新消息时引发事件收到。它似乎工作......一段时间!我在 Windows 7 Ultimate 64 位上使用 Visual Studio 2010。我有一个简单的 VB.NET Windows 窗体项目 - 这是代码:

Imports S22.Imap

Public Class Form1

    Private Shared Sub UnhExceptionHandler(sender As Object, e As System.UnhandledExceptionEventArgs)
        Console.WriteLine("Unhandled UI Exception: {0}", DirectCast(e.ExceptionObject, Exception).Message)
    End Sub

    Private Shared Sub ThrExceptionHandler(sender As Object, e As System.Threading.ThreadExceptionEventArgs)
        Console.WriteLine("Unhandled Thread Exception: {0}", e.Exception.Message)
    End Sub

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        AddHandler AppDomain.CurrentDomain.UnhandledException, AddressOf UnhExceptionHandler
        AddHandler System.Windows.Forms.Application.ThreadException, AddressOf ThrExceptionHandler
         Try
            Dim c As ImapClient = New ImapClient("imap.gmail.com", 993, "john@john-online.co.uk", "XXXXXXXXXXXXXXXX", AuthMethod.Login, True)
            ' Should ensure IDLE is actually supported by the server
            If c.Supports("IDLE") = False Then
                Console.WriteLine("Server does not support IMAP IDLE")
            Else
                ' We want to be informed when new messages arrive
                AddHandler c.NewMessage, New EventHandler(Of IdleMessageEventArgs)(AddressOf OnNewMessage)

            End If

        Catch ex As Exception
            MsgBox(ex.Message)

        End Try
    End Sub

    Private Shared Sub OnNewMessage(sender As Object, e As IdleMessageEventArgs)
        Console.WriteLine("A new message arrived. Message has UID: " & Convert.ToString(e.MessageUID))
    End Sub

End Class

当我运行它时,一段时间一切顺利 - 我收到新消息通知正常,但过了一段时间,如果我断开并重新连接我的网络连接,我会在“UnhExceptionHandler()”中收到以下消息:

Unable to read data from the transport connection: An established connection was aborted by the software in your host machine.

在此代码示例中,我可以简单地捕获错误并重新启动 ImapCLient 连接。但这只是为了测试 S22.Imap 库 - 我想在同时检查多个帐户的应用程序中使用它,所以我需要能够捕获错误并确定哪个 ImapClient 对象引发了它?

任何人都可以提出任何可能的解决方案吗?

4

0 回答 0