我正在尝试使用S22.Imap库(https://github.com/smiley22/S22.Imap)来侦听新消息。 我已按照提供的示例进行操作,但需要帮助...... 根据我读过的内容,ImapClient对象应该位于后台并侦听新消息(使用IDLE功能)并在收到新消息时引发事件。 它似乎工作......一段时间! 我在Windows 7旗舰版64位上使用Visual Studio 2010。 我有一个简单的VB.NET Windows Forms项目 - 这是代码:
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对象引发它吗?
有人可以建议任何可能的决议吗?