What exception? If you send a ping and the server does not respond then there will be no exception, the Ping method will just return False.
Printable View
Code:'here is your code basically
Try
If My.Computer.Network.Ping("someaddress") Then
smtpserver.Send(mailMessage)
Else
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
Exit Sub
End Try
'so what do THINK you save over this
Try
smtpserver.Send(mailMessage)
Catch ex As Exception
MessageBox.Show(ex.Message)
Exit Sub
End Try
I already told you what he gets out of it - rather than the user seeing "Failure sending mail" they will see something like "Could not ping the server, check the VPN connection"
Yeah, there are hundreds of examples on the internet (and these forums) of how to write to a file so do a search. Also, you can get the folder that the application is running in by using Application.StartupPath
Unless these are computer knowledgeable people does it really matter? Either you send the mail or you don't, and for most users anything more than "Success" or "Failure" is overkill.Code:Try
smtpserver.Send(mailMessage)
Catch ex As Exception
MessageBox.Show("Could not ping the server, check the VPN connection")
Exit Sub
End Try
Unless these are computer knowledgeable people does it really matter? Either you send the mail or you don't, and for most users anything more than "Success" or "Failure" is overkill. But go ahead keep fooling yourself.
Maybe for this one user it might make sense, so long as they control all the VPN locations. But what happens if they go to some hotel and try to VPN in and the ISP for the hotel has ICMP blocked?
But an error message is not just for the user's sake is it - if that were the case then why do we ever get anything more than "Success" or "Failure" in any program we use? the purpose of an error message is to tell you what the problem is - if the user does not understand it or know what to do with it then they call the IT department. I work in an IT department and trust me its a lot easier when a user rings up with a specific error message rather than a generic error where we then have to connect to their machine to check some log file to find out what the problem actually is.
So if you logged the relevant SMTP send exception, the one you contend will be eliminated by the ping, you wouldn't have a good idea as to what the problem was?
SmtpException
The connection to the SMTP server failed.
-or-
Authentication failed.
-or-
The operation timed out.
My sister, who works for the VA, was having trouble with her system and email one day. She picked up the phone to call IT, like the error message instructed her to do, and guess what, the phones didn't work because they used VoIP. She thought it was very funny. ;)
No because that is not what you get, if I do this:
I get an alert saying "Failure sending mail". Now you could use ex.InnerException.Message instead but then you have got to add checking to make sure there actually is an InnerException etc and it just seems so much easier to send a ping first as its such a quick and small thing. I'm not saying that sending a ping is a good idea in all cases, I just think in this particular scenario it is not doing any harm.Code:Try
Dim s As New Net.Mail.SmtpClient("mail.doesnotexist.com")
s.Send("[email protected]", "[email protected]", "jjj", "jdfo")
Catch ex As SmtpException
MessageBox.Show(ex.Message)
End Try
You were joking, right?
Code:Dim stpw As New Stopwatch
Try
'FYI - mail.doesnotexist.com exists
Dim s As New Net.Mail.SmtpClient("mail.foo.bar")
s.Send("[email protected]", "[email protected]", "jjj", "jdfo")
Stop
Catch ex As Net.Mail.SmtpException
'I get an alert saying "Failure sending mail".
'Now you could use ex.InnerException.Message instead but then
'you have got to add checking to make sure
'there actually is an InnerException etc and
'it just SEEMS so much easier to send a ping first as its such
'a QUICK and small thing.
stpw.Start()
If Not ex.InnerException Is Nothing Then
stpw.Stop()
Debug.WriteLine(ex.InnerException.Message)
stpw.Start()
End If
stpw.Stop()
Debug.WriteLine(stpw.ElapsedTicks.ToString & " check inner exception")
End Try
stpw.Reset()
stpw.Start()
My.Computer.Network.Ping("mail.doesnotexist.com")
stpw.Stop()
Debug.WriteLine(stpw.ElapsedTicks.ToString & " ping")
Blog entry for discussion:
http://stateofidleness.com/?p=326
i like the concept of eliminating wan icmp requests and chris, i know you have a deeper knowledge of the intricacies of the email process (as you're working on the smtp class) so I'd love for you to post as well!
tagged for later reading.
I guess Post #54 is being ignored, huh.
hard to argue with:
very nice example!Code:A first chance exception of type 'System.Net.Mail.SmtpException' occurred in System.dll
The remote name could not be resolved: 'mail.foo.bar'
6832 check inner exception
3709888448 ping
A first chance exception of type 'System.Net.Mail.SmtpException' occurred in System.dll
The remote name could not be resolved: 'mail.foo.bar'
5012 check inner exception
2941554672 ping
A first chance exception of type 'System.Net.Mail.SmtpException' occurred in System.dll
The remote name could not be resolved: 'mail.foo.bar'
4844 check inner exception
2307386879 ping
I found out the hard way that mail.doesnotexist.com does exist. The code kept passing the catch??????? I received a bunch of mail not delivered messages in my inbox.
haha it didnt exist for me... I just got good old Failure Sending Mail each time I tried to do it
Oh and just because the ping takes more time does not mean it is not useful.
How is it useful, other than as a network admin?
A funny thing happened on my way to a debate:
Code:Dim srvrName As String = "mail.foo.bar"
Dim smtpserver As New Net.Mail.SmtpClient(srvrName)
Dim mailMessage As System.Net.Mail.MailMessage
Private Sub foobar()
'The argument for PING
Try
If My.Computer.Network.Ping(srvrName) Then
smtpserver.Send("[email protected]", "[email protected]", "jjj", "jdfo")
MessageBox.Show("Mail sent successfully")
Else
MessageBox.Show("Could not ping someaddress")
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
'Exit Sub
End Try
'The opposing view - the one I support
Try
smtpserver.Send("[email protected]", "[email protected]", "jjj", "jdfo")
MessageBox.Show("Mail sent successfully")
Catch ex As Exception
If Not ex.InnerException Is Nothing Then
MessageBox.Show(ex.InnerException.Message)
Else
MessageBox.Show("Failure sending mail")
End If
'Exit Sub
End Try
End Sub