views:

70

answers:

4

Hi,

I am using VB.net code and SQL server 2005.

I am havng below code for sending email in my vb.net code.

 Protected Sub ibtnSendInvites_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ibtnSendInvites.Click
        Try
            Dim emailList As New List(Of String)
            For Each curRow As GridViewRow In GridView1.Rows
                Dim chkSelect As CheckBox = CType(curRow.Cells(1).FindControl("chkSelect"), CheckBox)
                Dim emailLabel As Label = CType(curRow.Cells(1).FindControl("lblEmailAddress"), Label)
                If chkSelect.Checked Then
                    emailList.Add(emailLabel.Text)
                End If
            Next
            For Each email As String In emailList
                Dim SelectDelegateMessage As String = "Please confirm your booking for this course"
                Dim SelectDelegateSubject As String = "User-Point Course Booking Invitation Email"
                Dim SelectDelegateFrom As String = WebConfigurationManager.AppSettings("UserPointEmailFromAddress").ToString()
                SendEmail.SendMessage(SelectDelegateSubject, SelectDelegateMessage, SelectDelegateFrom, email, "")
            Next
            GridView1.DataBind()
        Catch ex As Exception
            'Throw New Exception("Cannot Insert Duplicate Record", ex)
        End Try

    End Sub

Now my problem is that some time if emailaddress is not valid or SMTP server is responding my application get hangs. I want to log my errors generated by my SMTP server in table or any log file it would be good if I can send back email to admin with the details of error occured.

Please suggest!

Thanks.

Best Regards, MS

A: 

I would recommend logging not only these errors but all errors thrown in your application. It is a crucial weapon in your debugging arsenal. I typically log time, stack trace, error message, error type (ie IOException, SQLException, etc), and I also have a column where I can log details about the app at the time of the error (for example the SMTP server name in your case).

I store these in a database table simply because that is easy to query and do historical analysis on.

Matt Wrock
Good point, it is a good idea to log "all" errors. Once you get a bit serious about this however, you may want to introduce some logic to avoid sending logging too much and certainly about sending too many notifications, in the case of bursts of errors. It is a good idea to have a utility method called say "ErrorHandler()" where all logging (and other features such as notification of admins etc.) takes place. In this fashion no change to the program when business logic about error handling are changed.
mjv
I would filter out some errors like possibly 404 errors and ThreadAbortExceptions. However, I have found that more is more and it is worth logging everything. I have a site that gets > 10m requests a day and although thre are times where I may have thousands of errors loged a second due to network problems, having this logged proves invaluable in reporting issues. I routinely truncate my errors table when it gets too big. Also, my notification system does not email me on every error. I get notifications when there are > X errors in the last Y minutes.
Matt Wrock
A: 

Before errors while sending mail can be logged, or before the admin can be notified of such errors, they need to be detected!

After looking in vain online for a detailed description of this .NET SendMail.SendMessage() method, I figured it may just be as quick to implement one's own version of a mail-sending method, maybe by shamelessly copying this one from CodeProject.

So unless you know better and have access to a documentation which can inform you about the return codes (if any) of the SendMessage() method you are using now, you first need to switch to one that you can rely on.

Once this method is in place, you would need to test the return value. I'm anticipating that you could have a numeric return code, plus a clear text detail ("invalid email address", "SMTP not responding"...).

When the return value indicates an error condition, you would - Log this, to a text file, or to a database table. The details may vary base on the specific table but by way of ADO or some other db connection you'd send a query such as this:

    "INSERT INTO dbLogs.dbo.tblEmailErrors 
     (EventTime, EmailAddr, ErrorCode, ErrorMessage)
     VALUES ('<some date from say method Now()',  '<the email addr>', 
     <the error code>, '<the error clear text message>' "

Then you'd also notify the admins, for more severe situations only, I assume (no need to bother 'em for bogus email address cases). That could be a dilemma for in case of error you would possibly not be able to send an email [to the admins] using the same channel... This may imply that you'd use another smtp server, if possible on the very server where you are running the program, as so to limit dependencies or that you'd use another communication system altogher (direct dialing to pager..., Tweeter message...).

-

mjv
A: 

If you already have access to a Sql server why not use the SQL sp_send_dbmail procedure? It handles all SMTP stuff and logs the errors for you. Is also asynchronous and handles retried.

Is it very unwise to do something as expensive as smtp traffic on the UI thread. You should just queue a user work item to the thread pool and free the UI thread to handle more events.

There are also various off-the-shelf exception handling and logging modules, like log4net.

Remus Rusanu
Thanks Dear Can I have example code for the above issue using SQL send email. Please help!
MKS
A: 

In line with the other comments that highlight the importance of logging all errors, take a look at log4net. It has various logging targets (appenders), including database logging.

ProKiner