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
Solved

How to track errors for Smtp Async?

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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

839 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