如果使用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
任何帮助,我们将不胜感激。 谢谢
答案 0 :(得分:2)
你为什么不这样做呢?
Await smtp.SendAsync(mail)
有关如何在VB.NET中处理异步API的更多信息,请查看以下文档:https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/operators/await-operator