SmtpClient Class: send() 有效但 sendAsync() 无效

SmtpClient Class: send() works but not sendAsync()

我正在编写一个简单的 Windows 表单(Visual Studio 2010、.NET 4.0、Windows 2008 32 位)。单击按钮发送电子邮件。

函数SendMaildButton_Click()发送邮件,如果完成(aSync方法),它调用SendCompletedCallback()

Public Class MailForm
    Private sc As System.Net.Mail.SmtpClient

    Private Sub MailForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        sc = New System.Net.Mail.SmtpClient
        AddHandler sc.SendCompleted, AddressOf SendCompletedCallback
    End Sub

    Private Sub SendCompletedCallback(ByVal sender As Object, _
                                      ByVal e As AsyncCompletedEventArgs)
        ' [...]
    End Sub

    Private Sub SendMaildButton_Click(ByVal sender As System.Object, _
                                      ByVal e As System.EventArgs) Handles SendButton.Click
        'sc instance of SmtpClient already created on OnLoad form event
        ' [sc.Host, sc.Port, sc.Credentials stuff ...]

        Dim message As MailMessage
        ' [message filling]

        ' [relevant part : Send or SendAsync]

        ' freedom...
        message.Dispose()
    End Sub
End Class

如果发送代码是:

sc.Send()

事件 fakeSMTPServer(虚拟 smtp 服务器)或 Gmail 获取它。当我宁愿使用 :

Dim userState As String = "plap" 'message.GetHashCode
AddHandler sc.SendCompleted, AddressOf SendCompletedCallback
sc.SendAsync(message, userState)

它不起作用...fakeSMTPServer 通知邮件接收,但没有真正检索它。
消息错误是:"Failure sending mail"

这是 fakeSMTPServer 的堆栈跟踪:

19 janv. 2017 17:15:43 ERROR com.nilhcem.fakesmtp.server.MailSaver -
java.io.EOFException: Pre-mature end of <CRLF>.<CRLF> terminated data
        at org.subethamail.smtp.io.DotTerminatedInputStream.read(DotTerminatedInputStream.java:73) ~[fakeSMTP-2.0.jar:na]
        at org.subethamail.smtp.io.DotUnstuffingInputStream.read(DotUnstuffingInputStream.java:47) ~[fakeSMTP-2.0.jar:na]
        at org.subethamail.smtp.io.DotUnstuffingInputStream.read(DotUnstuffingInputStream.java:76) ~[fakeSMTP-2.0.jar:na]

我错过了什么?

问题出在这里:

message.Dispose()

您正在释放您的资源 发送电子邮件之前。你应该只在 SendCompletedCallback 释放你的资源,因为现在你只是说:开始发送这个……哦,不,没关系。