?
Solved

How to track errors for Smtp Async?

Posted on 2010-09-22
4
Medium Priority
?
710 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 15

Accepted Solution

by:
crisco96 earned 1500 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

Is Your Team Achieving Their Full Potential?

74% of employees feel they are not achieving their full potential. With Linux Academy, not only will you strengthen your team's core competencies but also their knowledge of of the newest IT topics.

With new material every week, we'll make sure that you stay ahead of the game.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

771 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