Link to home
Start Free TrialLog in
Avatar of mjlthomas
mjlthomas

asked on

System.Web.MAIL -> TRY CATCH and send error message to a string to be appended to a log

Mostly I'm lazy today and don't want to think.

How do I trap exceptions from System.Web.MAIL.SmtpMail and put them in a string format.

Avatar of mjlthomas
mjlthomas

ASKER

General Code example as Follows:

Dim myMail As New MailMessage
            With myMail
                .From = "some@where.com"
                .To = emails & "; some@where.com"
                .Subject = RPTName & " for " & dt
                .Body = dt + " " + tm
                Dim myAttachment As New MailAttachment("S:\Crystal.Net\Exports\" & RPTName & "." & ExportType, MailEncoding.UUEncode)
                .Attachments.Add(myAttachment)
                .Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1") 'basic authentication
                .Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "A User Name") 'set your username here
                .Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "A Password") 'set your password here
                .Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout", "240")
            End With
            SmtpMail.SmtpServer = "SERVERNAME"
            SmtpMail.Send(myMail)
            StatusBox.Text = Microsoft.VisualBasic.TimeOfDay & ": Message Relayed to Server:    " & RPTName & vbCrLf & "       Message Recipients: " & emails & vbCrLf & StatusBox.Text
            SendReportFlag = True
ASKER CERTIFIED SOLUTION
Avatar of jrandallsexton
jrandallsexton

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Try
   MyCode
Catch ex as System.Exception
      'ex.ToString   <---- this makes the exception to string then you can do whatever you want
      msgbox( ex.ToString )
End Try
Remove my "Catch ex as smtp" --> that was supposed to have been cut out by me.  Whoops.