我正在编写一个简单的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]
我错过了什么?
答案 0 :(得分:1)
问题在于:
message.Dispose()
您可以在发送电子邮件之前释放资源。你应该只在SendCompletedCallback
中释放你的资源,因为现在你只是说:开始发送这个...哦,不,没关系。