Solved

How to track errors for Smtp Async?

Posted on 2010-09-22
4
704 Views
Last Modified: 2013-11-26
This is my first time sending asynchronously, and so I followed the instructions in the video at http://www.asp.net/general/videos/how-do-i-send-email-asynchronously-with-aspnet

Afterward, I could successfully send asynchronously. However, what I can't figure out is how to track errors. Specifically, I want to track the # of undeliverable messages, due to bad email addresses.

I tried the following, but it doesn't update the intNumErrors counter from the SmtpClient_Oncompleted event handler, even though intNumErrors is a public variable. How can I track# of errrors?
Imports System.Net.Mail

Partial Class Test_AsynchSmtp
  Inherits System.Web.UI.Page
  Dim intNumErrors As Integer

  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    intNumErrors = 0
  End Sub

  Protected Sub btnSend_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSend.Click
    Dim strTo As String = "Bogus@Example.com"
    SendAsynchMail(strTo)
    strTo = "Legit@Example.com"
    SendAsynchMail(strTo)
  End Sub

  Sub SendAsynchMail(ByVal strTo As String)
    Dim mailMessage As New MailMessage
    mailMessage.From = New MailAddress("no-reply@example.com")
    mailMessage.To.Add(New MailAddress(strTo))
    mailMessage.Subject = "My ASYNCH test subject"
    mailMessage.Body = "This is my ASYNCH test body"

    Dim smtpClient As New SmtpClient()
    Dim userState As Object = mailMessage
    AddHandler smtpClient.SendCompleted, AddressOf SmtpClient_Oncompleted
    smtpClient.SendAsync(mailMessage, userState)
  End Sub

  Public Sub SmtpClient_Oncompleted(ByVal send As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs)
    Dim mailMessage As MailMessage
    mailMessage = CType(e.UserState, MailMessage)

    If Not (e.Error Is Nothing) Then
      intNumErrors += 1
    End If
  End Sub

End Class

Open in new window

0
Comment
Question by:cdakz
  • 3
4 Comments
 
LVL 15

Accepted Solution

by:
crisco96 earned 500 total points
ID: 33742760
You need to make sure your intNumErrors is threadsafe: http://devcity.net/Articles/160/2/article.aspx

I would suggest using the interlocked class to increment the variable.

Additionally I don't think you can track if an email is undeliverable through asp.net, that would probably need to happen on your smtp server (I could be wrong).
0
 

Author Comment

by:cdakz
ID: 33746608
The threadsafe topic is new to me, so I'll have to bone up on that.

However, I do know that we can track undeliverable addresses, at least through the standard (i.e. synchronous) .Send method.

Anybody else know anything else about this?
0
 

Author Comment

by:cdakz
ID: 33746718
The threadsafe info is new to me, so I''l have to bone up on that.

However, when sending smtp via the standard .Send method (i.e. not asynchronous) does report undeliverable addresses.

What I *think* I need to do is check the SmtpException property, but I can't figure out how to get to it.

Here's what  http://msdn.microsoft.com/en-us/library/x5x13z6h.aspx says about it:
If you receive an SmtpException exception, check the StatusCode property to find the reason the operation failed. The SmtpException can also contain an inner exception that indicates the reason the operation failed.

When sending e-mail using SendAsync to multiple recipients, if the SMTP server accepts some recipients as valid and rejects others, a SmtpException is thrown with a NullReferenceException for the inner exception. If this occurs, SendAsync fails to send e-mail to any of the recipients.
0
 

Author Closing Comment

by:cdakz
ID: 33897582
The lead on using a threadsafe counter was helpful, but didn't fully address my needs.
0

Featured Post

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question