I've been working on a project that is almost done. It works great except for handling two types of possible errors in the section that emails an attachment. If there are no errors, it works perfectly. If one or the other error is present it still works. But if BOTH errors are present, it crashes. I have indicated "CRASHES HERE" in the first Catch Block.
How I'm testing:
Scenario #1: Works
A valid format email address is stored in the FromAddress variable
My network connection is enabled
Scenario #2: Works
A valid format email address is stored in the FromAddress variable
My network connection is disabled
Scenario #3: Works
A invalid format email address is stored in the FromAddress variable
(The program successfully changes the email address)
My network connection is enabled
Scenario #4: EPIC FAIL
A invalid format email address is stored in the FromAddress variable
My network connection is disabled
Any help would be great:
How I'm testing:
Scenario #1: Works
A valid format email address is stored in the FromAddress variable
My network connection is enabled
Scenario #2: Works
A valid format email address is stored in the FromAddress variable
My network connection is disabled
Scenario #3: Works
A invalid format email address is stored in the FromAddress variable
(The program successfully changes the email address)
My network connection is enabled
Scenario #4: EPIC FAIL
A invalid format email address is stored in the FromAddress variable
My network connection is disabled
Any help would be great:
VB.NET:
Try
Dim Attachment As New System.Net.Mail.Attachment(MyFile)
Dim Email As New System.Net.Mail.MailMessage(FromAddress, "ToAddress")
Email.Subject = "Here's my File"
Email.Body = "~File Attached~"
Dim MailClient As New System.Net.Mail.SmtpClient()
Dim Authentication As New System.Net.NetworkCredential("username", "password")
MailClient.Host = "MyMailServerName"
MailClient.UseDefaultCredentials = False
MailClient.Credentials = Authentication
Email.Attachments.Add(Attachment)
MailClient.Send(Email)
'Error handling: If FromAddress value is bad format replace with "me@some-domain.com"
'and try to send again: m
Catch AddressError As System.FormatException
MsgBox("eMail Address ERROR found")
Dim Attachment As New System.Net.Mail.Attachment(MyFile)
Dim Email As New System.Net.Mail.MailMessage("me@some-domain.com", "ToAddress")
Email.Subject = "Here's my File"
Email.Body = "~File Attached~"
Dim MailClient As New System.Net.Mail.SmtpClient()
Dim Authentication As New System.Net.NetworkCredential("username", "password")
MailClient.Host = "MyMailServerName" 'ex: mail.some-domain.com
MailClient.UseDefaultCredentials = False
MailClient.Credentials = Authentication
Email.Attachments.Add(Attachment)
MailClient.Send(Email) 'CRASHES HERE
'Error handling: Catch anything else (communications error)
Catch 'CommunicationError As Exception
MsgBox("ERROR Found, message did not send")
End Try