2009-04-22 3 views
1

전자 메일을 비동기 적으로 보내려고하고 있으며 전자 메일에 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 

답변

2

이유는 다른 관점이있을 때, 나는 다음과 같은 오류가 발생합니다 SendAsync() 메서드가 완료 될 때 OnCompleted 처리기가 호출 되었기 때문에 SmtpClient가 실제로 네트워크를 통해 전자 메일을 보내기 전에 나타나는 것처럼 보이지만 작동하지 않습니다. 그러나 네트워크 배달과 함께 발생하는 파일 배달은 기본적으로 SendAsync()와 동기화됩니다.

메시지가 실제로 전송되었을 때 OnCompleted가 실제로 호출되어야하기 때문에 이것은 SmtpClient의 버그와 거의 같은 것처럼 보입니다.

+0

근래의 HAV는 GC에 의해 자동으로 배치하면서 MSG 객체 요구 처리의 기준 ..

건배를 추가한다는 것이다 Microsoft에 제출하십시오. 그게 내가 어디든지 데려다 주는지 알게 될거야. – hacker

+0

Microsoft는 오류없이 정상적으로 작동 할 수 있다고 대답하고 표시했습니다. 전자 메일을 비동기 적으로 다시 적용하고 테스트하기 위해 해당 응용 프로그램으로 돌아갈 기회가 없었습니다. – hacker

0

매우 비슷한 문제가있었습니다. 동일한 오류 메시지이지만 약간 다른 코드 구조. 필자의 경우 main 함수 안에 mailmessage 객체를 저장하고있었습니다. OnCompleted 이벤트가 실행될 때까지 개체는 이미 사라졌습니다.

SendAsync 다음에 코드를보고 mailmessage 개체를 해제하는지 확인하십시오. 예를 들어, using 문 내부에서 작성하는 경우, async 이벤트가 실행되기 전에 해제됩니다.

+0

나는 처음에는 MailMessage를 finally 블록에 배치했기 때문에 확인했다. 안타깝게도 AlternateView는 처리되지 않고 있으며 OnCompleted 블록의 끝에 MailMessage가 처리됩니다. MailMessage dispose 호출에 도달하기 전에 오류가 발생합니다. 내가 생각하고있는 한가지는 GC가 너무 빨리 객체를 수집하고 있다는 것입니다. – hacker

0

콜백에 액세스하려면 희미한 이미지를 클래스 수준에 두어야합니다.

private msg As System.Net.Mail.MailMessage 
private hAV As System.Net.Mail.AlternateView 

private sub yoursub 
    msg = new System.Net.Mail.MailMessage(.. 
    hAV = new ... 
end sub 

내 생각 AlternateViews.Add 단지 I가 전송되었습니다

관련 문제