전자 메일을 비동기 적으로 보내려고하고 있으며 전자 메일에 AlternateView가 연결되어 있지 않으면 정상적으로 작동합니다. 여기 .NET SMTP SendAsync with AlternateViews가 폐기 된 예외를 throw합니다.
Cannot access a disposed object. Object name: 'System.Net.Mail.AlternateView'
System.Net.Mail.SmtpException: Failure sending mail. ---> System.ObjectDisposedException: Cannot access a disposed object.
Object name: 'System.Net.Mail.AlternateView'.
at System.Net.Mail.AlternateView.get_LinkedResources()
at System.Net.Mail.MailMessage.SetContent()
at System.Net.Mail.MailMessage.BeginSend(BaseWriter writer, Boolean sendEnvelope, AsyncCallback callback, Object state)
at System.Net.Mail.SmtpClient.SendMailCallback(IAsyncResult result)
이 몇 가지 예제 코드입니다 :
Dim msg As New System.Net.Mail.MailMessage
msg.From = New System.Net.Mail.MailAddress("[email protected]", "My Name")
msg.Subject = "email subject goes here"
'add the message bodies to the mail message
Dim hAV As System.Net.Mail.AlternateView = System.Net.Mail.AlternateView.CreateAlternateViewFromString(textBody.ToString, Nothing, "text/plain")
hAV.TransferEncoding = Net.Mime.TransferEncoding.QuotedPrintable
msg.AlternateViews.Add(hAV)
Dim tAV As System.Net.Mail.AlternateView = System.Net.Mail.AlternateView.CreateAlternateViewFromString(htmlBody.ToString, Nothing, "text/html")
tAV.TransferEncoding = Net.Mime.TransferEncoding.QuotedPrintable
msg.AlternateViews.Add(tAV)
Dim userState As Object = msg
Dim smtp As New System.Net.Mail.SmtpClient("emailServer")
'wire up the event for when the Async send is completed
AddHandler smtp.SendCompleted, AddressOf SmtpClient_OnCompleted
Try
smtp.SendAsync(msg, userState)
Catch '.... perform exception handling, etc...
End Try
그리고 콜백 .....
Public Sub SmtpClient_OnCompleted(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs)
If e.Cancelled Then
'Log the cancelled error
End If
If Not IsNothing(e.Error) Then
'Log a real error....
' this is where the error is getting picked up
End If
'dispose the message
Dim msg As System.Net.Mail.MailMessage = DirectCast(e.UserState, System.Net.Mail.MailMessage)
msg.Dispose()
End Sub
근래의 HAV는 GC에 의해 자동으로 배치하면서 MSG 객체 요구 처리의 기준 ..
건배를 추가한다는 것이다 Microsoft에 제출하십시오. 그게 내가 어디든지 데려다 주는지 알게 될거야. – hacker
Microsoft는 오류없이 정상적으로 작동 할 수 있다고 대답하고 표시했습니다. 전자 메일을 비동기 적으로 다시 적용하고 테스트하기 위해 해당 응용 프로그램으로 돌아갈 기회가 없었습니다. – hacker