Solved

How to track errors for Smtp Async?

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

758 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now