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.
Best Regards, MS
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...).