Solved

How to track errors for Smtp Async?

Posted on 2010-09-22
4
700 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

776 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