Mailkit - 使用 SendAsync 未获得预期的异常 (vb.net)

Mailkit - not getting expected exceptions using SendAsync (vb.net)

如果我使用 Send 方法,我可以按预期处理任何异常。

Try
    'Synchronous method
    OutputStatus("Sending message, please wait")
    smtp.Send(mail)

Catch smtpEx As SmtpCommandException
    If smtpEx.ErrorCode = SmtpErrorCode.RecipientNotAccepted Then
        OutputStatus("Unable to send message to: " & smtpEx.Mailbox.Address)
    ElseIf smtpEx.ErrorCode = SmtpErrorCode.UnexpectedStatusCode Then
        OutputStatus("Unable to send message: " & smtpEx.Message)
    ....
    End If

但是,如果我使用 SendAsync 方法,我不会收到任何异常,并且不会触发 MessageSent 事件处理程序(因此我陷入了 Do While 循环)。 如果消息正常,则事件处理程序正常工作。

AddHandler smtp.MessageSent, AddressOf SMTPMessageSent
mbSendingMessage = True
Try
    smtp.SendAsync(mail)

    Do While mbSendingMessage
        Application.DoEvents()
    Loop

Catch ex As Exception
    OutputStatus("Error sending, see GWMailer.err")
End Try

 ....

Private Sub SMTPMessageSent(sender As Object, e As MailKit.MessageSentEventArgs)
    mbSendingMessage = False
End Sub

非常感谢任何帮助。 谢谢

你为什么不这样做呢?

Await smtp.SendAsync(mail)

有关如何处理 VB.NET 中的异步 API 的更多信息,请查看此文档:https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/operators/await-operator