|
-
Jan 30th, 2010, 01:24 PM
#1
Thread Starter
Junior Member
How to ping server before sending mail with smtp
Hi,
below is my code
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ProgressBar1.Value = 5
Dim strTo As String = setting.tosett.ToString
ProgressBar1.Value = 10
Dim strFrom As String = setting.fromsett.ToString
ProgressBar1.Value = 15
Dim strSubject As String = setting.subjectsett.ToString & ", " & setting.poslovnajedinicasett.ToString
ProgressBar1.Value = 20
Dim strBody As String = Me.txtbody.Text & ". Time when request is sent: " & Date.Today & TimeOfDay & ". Sender of request: " & TextBox3.Text
ProgressBar1.Value = 25
Dim mailMessage As New System.Net.Mail.MailMessage(strFrom, strTo,
strSubject, strBody)
ProgressBar1.Value = 30
'mailMessage.IsBodyHtml.ToString()
Dim smtpserver As New SmtpClient(setting.serveripsett, setting.serverportsett.ToString)
ProgressBar1.Value = 35
smtpserver.Credentials = New System.Net.NetworkCredential(setting.serveripsett.ToString, setting.serverportsett.ToString)
ProgressBar1.Value = 40
smtpserver.DeliveryMethod = Net.Mail.SmtpDeliveryMethod.Network
ProgressBar1.Value = 50
Try
If My.Computer.Network.Ping(setting.serveripsett.ToString) Then
smtpserver.Send(mailMessage)
ProgressBar1.Value = 60
Else
MsgBox("Server is not available. Please check your VPN connection!")
End If
Catch ex As Exception
ProgressBar1.Value = 70
'me.txtinfo.Text = (ex.ToString)
MsgBox("Error while submiting request: " & ex.Source & ex.Message & ex.ToString)
'MsgBox(ex.ToString, MsgBoxStyle.Critical, "Error:")
ProgressBar1.Value = 5
Exit Sub
End Try
ProgressBar1.Value = 100
MessageBox.Show("Request is submited.")
ProgressBar1.Value = 0
This is the part of the code which is bothering me:
Code:
If My.Computer.Network.Ping(setting.serveripsett.ToString) Then
smtpserver.Send(mailMessage)
ProgressBar1.Value = 60
Else
MsgBox("Server is not available. Please check your VPN connection!")
End If
So i want the program to ping the server before sending the mail; if server is available then send mail if not then message box with warning appears notifying user to check VPN connection and sending of mail is canceled.
Do you have any advice how to do this?
-
Jan 30th, 2010, 04:44 PM
#2
Re: How to ping server before sending mail with smtp
View the tutorial in my sig on how to ping. it checks for a valid network connection first, then pings, then returns the ping result.
you would then test if the ping result was "online" or "offline" and perform the necessary tasks.
comments welcome if the tutorial helps at al
-
Jan 30th, 2010, 04:50 PM
#3
Re: How to ping server before sending mail with smtp
1 - Some mail servers do not reply to ICMP messages, so ping is meaningless.
2 - A successful ping doesn't mean that the email will be sent successfully.
So just send the email and handle the exception if it isn't sent.
-
Jan 30th, 2010, 04:50 PM
#4
Re: How to ping server before sending mail with smtp
I dont get it, you already seem to have the code for exactly what you want to do...
-
Jan 30th, 2010, 04:51 PM
#5
Re: How to ping server before sending mail with smtp
 Originally Posted by dbasnett
1 - Some mail servers do not reply to ICMP messages, so ping is meaningless.
2 - A successful ping doesn't mean that the email will be sent successfully.
So just send the email and handle the exception if it isn't sent.
Yeah but as he mentioned a VPN it seems likely that this is an internal mail server and he already knows it responds to pings
-
Jan 30th, 2010, 04:54 PM
#6
Fanatic Member
Re: How to ping server before sending mail with smtp
 Originally Posted by stateofidleness
View the tutorial in my sig on how to ping. it checks for a valid network connection first, then pings, then returns the ping result.
Hi, I'm interested in making my applications make a connection to my website to lookup license details and check availability of updates etc.
Doesn't this kind of activity make windows pop up any security warnings ? I mean an application making connections to the web is going to make the firewall throw up alarms isn't it?
Or is it just a case of the user having to acknowledge it the first time the warning appears ? Or is there some way to do it without warning appearing ?
Thanks
-
Jan 30th, 2010, 04:55 PM
#7
Re: How to ping server before sending mail with smtp
 Originally Posted by IanS
Hi, I'm interested in making my applications make a connection to my website to lookup license details and check availability of updates etc.
Doesn't this kind of activity make windows pop up any security warnings ? I mean an application making connections to the web is going to make the firewall throw up alarms isn't it?
Or is it just a case of the user having to acknowledge it the first time the warning appears ? Or is there some way to do it without warning appearing ?
Thanks
I think you should start your own thread for this as it doesnt really seem to be at all related to this one
-
Jan 30th, 2010, 05:01 PM
#8
Re: How to ping server before sending mail with smtp
 Originally Posted by chris128
Yeah but as he mentioned a VPN it seems likely that this is an internal mail server and he already knows it responds to pings 
In my opinion it is a huge waste of bandwidth, and even if the ping is successful you still have to catch exceptions. Consider this:
1 - Successful ping
2 - Start sending email
3 - Network goes down
4 - email fails
or
1 - Network down
2 - Ping fails
3 - Network comes back up
4 - Cancel sending because ping failed
A (un)successful ping is only that, nothing more.
-
Jan 30th, 2010, 05:20 PM
#9
Re: How to ping server before sending mail with smtp
Yeah you certainly still need to handle exceptions when sending the mail but I dont see any harm in just checking the server is online first because if it is not then you can display a friendly message to the user, rather than just showing them whatever error message SmtpClient comes back with (I think if the server does not respond at all you just get something generic like "Failure sending mail")
-
Jan 30th, 2010, 05:51 PM
#10
Re: How to ping server before sending mail with smtp
"but I don't see any harm in just checking the server is online"
The point I am trying to make is that a successful ping means that an echo reply was received. That is all that it means. You could get an answer and yet the server could be down.
This question is asked, in many flavors, a lot, like ping a web site before sending an http request, or testing if the internet is up. For the most part it is just a colossal waste.
-
Jan 30th, 2010, 05:58 PM
#11
Re: How to ping server before sending mail with smtp
i'd argue that it is not a waste to be honest.
isn't more of a bandwidth waste to send a large email and have it fail (possibly mid-send), than to send a few packets in a ping to determine if it is down or not?
Before I send a 4MB email with attachments, I'd like to know that the server is online to accept it. If it isn't, then it will get bounced as well, which is even more bandwidth.
In a chat program, you don't just keep sending IM's to people that are offline. The chat program doesn't just "handle" the errors.. it tells you that the user is offline, then gives you the option to send the message anyway (some clients).
same concept.
also, @IanS, start your own thread for that question, but depending on your security settings, I assume it could prompt, but I have not encountered it myself yet
-
Jan 30th, 2010, 06:05 PM
#12
Re: How to ping server before sending mail with smtp
 Originally Posted by stateofidleness
i'd argue that it is not a waste to be honest.
isn't more of a bandwidth waste to send a large email and have it fail (possibly mid-send), than to send a few packets in a ping to determine if it is down or not?...
If what you were doing was sending one large gigantic packet I would probably agree. But that ain't how it works.
I have seen stats that say ICMP traffic is 2-3% on the backbone.
-
Jan 30th, 2010, 06:13 PM
#13
Re: How to ping server before sending mail with smtp
when you ping, once you get the reply, it's over.. no more traffic.
if i'm sending a huge email (packets a time) and it fails half way through, not only does the email fail, but i put a lot of traffic on the network for some period of time that did absolutely no good (4MB file failing half way through is 2MB of bandwidth)
2MB of bandwidth > one ping request
-
Jan 30th, 2010, 06:25 PM
#14
Re: How to ping server before sending mail with smtp
 Originally Posted by stateofidleness
when you ping, once you get the reply, it's over.. no more traffic.
if i'm sending a huge email (packets a time) and it fails half way through, not only does the email fail, but i put a lot of traffic on the network for some period of time that did absolutely no good (4MB file failing half way through is 2MB of bandwidth)
2MB of bandwidth > one ping request
How does ping help that situation? The ping was good, you start sending, the net goes down... You might have been 64 bytes closer to the end if you hadn't bothered with the ping.
-
Jan 30th, 2010, 06:31 PM
#15
Re: How to ping server before sending mail with smtp
Although I do agree with you stateofidleness in that sending a ping first is worth doing a lot of the time, I dont quite agree with your specific argument - sending a ping first and getting a reply does not mean that sending the email will not fail half way through. All it means is that the initial connection is likely to succeed (although as dbasnett points out, just because an SMTP server returns a ping request it does not mean that the SMTP service is running and responding). So basically you could send a ping, get a reply, and the server could still die half way through you sending your 4 MB of data.
Also
Before I send a 4MB email with attachments, I'd like to know that the server is online to accept it. If it isn't, then it will get bounced as well, which is even more bandwidth.
The 4 MB of data only gets transferred after the server has accepted the initial SMTP connection and has accepted the sender and the recipient(s) specified in the email. So it would not get as far as sending any large amount of data if the server was offline to begin with.
-
Jan 30th, 2010, 06:33 PM
#16
Re: How to ping server before sending mail with smtp
but if the ping was bad, you just saved yourself 2MB.
half full / half empty I suppose.
Do you look both ways before crossing the street, or do you just walk out there and hope there's no "exception" to handle
-
Jan 30th, 2010, 06:37 PM
#17
Re: How to ping server before sending mail with smtp
 Originally Posted by chris128
The 4 MB of data only gets transferred after the server has accepted the initial SMTP connection and has accepted the sender and the recipient(s) specified in the email. So it would not get as far as sending any large amount of data if the server was offline to begin with.
ah! I don't dispute that it could fail halfway through even with a good ping, but wouldn't it be wise to ensure there's a valid network connection before sending ANY kind of traffic? (aka IsNetworkAvailable, not necessarily a ping)
-
Jan 30th, 2010, 06:41 PM
#18
Re: How to ping server before sending mail with smtp
One day the ISP's are going to look at the ?%, most of it abuse (like pinging a server that responds to pings and has mail on it), and they will put a stop to it. They will go to the edge routers and shut ICMP off. I have a friend that is a big somebody at a major ISP who already has the plan in place, has for years. It isn't someone pinging from the command prompt that is causing the problem, it is all this automated stuff.
If the network is down your real work will fail just as rapidly as a ping, and without any more overhead. Before you respond, try this. Close your browser, get a command prompt. Ping www.vbforums.com
When the "network" comes back up post your response.
All IsNetworkAvailable does is tell you if your PC has a link.
-
Jan 30th, 2010, 06:48 PM
#19
Re: How to ping server before sending mail with smtp
touche.
but for argument's sake, if the plan is to check the status of network devices (that you can not connect to per se), what is the solution?
Problem: I need to know if the digital video recorder (has IP 1.1.1.1) at location XYZ is online.
Solution: ?
-
Jan 30th, 2010, 07:00 PM
#20
Re: How to ping server before sending mail with smtp
 Originally Posted by stateofidleness
touche.
but for argument's sake, if the plan is to check the status of network devices (that you can not connect to per se), what is the solution?
Problem: I need to know if the digital video recorder (has IP 1.1.1.1) at location XYZ is online.
Solution: ?
Well, if you "can not connect" then problem solved. It is unlikely, unless you are a network administrator, that you need to know if it is online. You might need to transfer a video, and in that case just do so.
I keep saying this and no one is listening, if you have some network task to perform, just do it. If you need to send mail, send it, if you need to download a file, download it.
I wouldn't build a piece of software, other than a net admin tool, that depended upon ICMP.
If I were going to do something, that might give me a warm fuzzy, I'd resolve the host name. If it isn't cached it will cause a DNS lookup and if I don't fool around, whatever I am really doing will not need to resolve it.
-
Jan 30th, 2010, 07:06 PM
#21
Re: How to ping server before sending mail with smtp
wel that's what I'm gettin at. a net admin tool that checks the status of network devices. a 'ping' is the only viable solution that i can come up with.
there are good uses for icmp requests, and i agree, they may be "over-utilized" so-to-speak, but blocking icmp traffic at ISP's will kill even the legitimate uses.
-
Jan 30th, 2010, 07:22 PM
#22
Re: How to ping server before sending mail with smtp
 Originally Posted by stateofidleness
wel that's what I'm gettin at. a net admin tool that checks the status of network devices. a 'ping' is the only viable solution that i can come up with.
there are good uses for icmp requests, and i agree, they may be "over-utilized" so-to-speak, but blocking icmp traffic at ISP's will kill even the legitimate uses.
The plan I saw(several years ago) was for edge devices only, their internal stuff will still work. My message is don't do it if you don't have to, and pass it on.
-
Jan 30th, 2010, 07:24 PM
#23
Re: How to ping server before sending mail with smtp
roger that. consider me a 'passer' 
might have to do a blog entry about this topic as it has some interesting perspectives. i'd like you to chime in (possibly in more detail about the ISP thing) if i do.
-
Jan 30th, 2010, 07:33 PM
#24
Re: How to ping server before sending mail with smtp
-
Jan 30th, 2010, 07:38 PM
#25
Re: How to ping server before sending mail with smtp
BTW - I just looked at your website. Very nice, short and succinct. San Antonio is a favorite of mine. I helped USAA build a SONET network on their campus, and did some major partying with some of them. Oh the stories.
-
Jan 31st, 2010, 03:07 AM
#26
Thread Starter
Junior Member
Re: How to ping server before sending mail with smtp
 Originally Posted by stateofidleness
View the tutorial in my sig on how to ping. it checks for a valid network connection first, then pings, then returns the ping result.
you would then test if the ping result was "online" or "offline" and perform the necessary tasks.
comments welcome if the tutorial helps at al
it implements the same code for ping i used. I used this code on start of application and it works fine; if destination is reachable then start application if not then don´t start it and warn the user about problem. This same code is in my post above and now i want to use it to check destination before mail is sent.
 Originally Posted by dbasnett
1 - Some mail servers do not reply to ICMP messages, so ping is meaningless.
2 - A successful ping doesn't mean that the email will be sent successfully.
So just send the email and handle the exception if it isn't sent.
my server replies to icmp. I have a vpn network with central location and 23 remote locations and for those users on remote location network traffic is limited. They can use only network resources which are inside the vpn. This means no internet for them or any other kind of network traffic which is outside organisation. Also i handle exception here:
Code:
Catch ex As Exception
ProgressBar1.Value = 70
'me.txtinfo.Text = (ex.ToString)
MsgBox("Error while submiting request: " & ex.Source & ex.Message & ex.ToString)
'MsgBox(ex.ToString, MsgBoxStyle.Critical, "Error:")
but still i want to program check is server available before the mail is sent and it should warn the user with msgbox if not.
 Originally Posted by chris128
I dont get it, you already seem to have the code for exactly what you want to do...
You are right! i have the code but it´s not working as it should. When server is down now it goes like this:
1. it warns user with message box that server is not reachable
2. When i click ok on the message box which warns user that server is not reachable then message box appears saying that request is submited
3. It does not catch exception
And it should go like this:
1. it warns user with message box that server is not reachable
2. then it should cancel sending of e-mail
3. It should catch exception
Last edited by Ramiel; Jan 31st, 2010 at 07:46 AM.
-
Jan 31st, 2010, 06:16 AM
#27
Fanatic Member
Re: How to ping server before sending mail with smtp
 Originally Posted by chris128
I think you should start your own thread for this as it doesnt really seem to be at all related to this one
Not at all related ? I beg to differ.
OK - let me rephrase my question - This topic is discussing how to check to see if there's an internet connection and then pinging a remote mail server.
Isn't that going to trigger alarms in the firewall or can than be done without any alarms ? Is the thread starter aware of that, how would they handle or avoid that ?
-
Jan 31st, 2010, 07:43 AM
#28
Thread Starter
Junior Member
Re: How to ping server before sending mail with smtp
 Originally Posted by IanS
Not at all related ? I beg to differ.
OK - let me rephrase my question - This topic is discussing how to check to see if there's an internet connection and then pinging a remote mail server.
Isn't that going to trigger alarms in the firewall or can than be done without any alarms ? Is the thread starter aware of that, how would they handle or avoid that ?
let me explain my situation little more detailed. As i said central and remote locations are connected with VPN; on remote locations every kind of internet traffic is disabled only thing they can do is to share network resources with central location. By this because internet is disabled on remote locations there are no AV software on the clients, also there are no firewalls except on servers in central location. For users on remote location is forbiden to use usb ports and cd-rom. Ping of smtp server (exchange server) will not alarm the firewall
-
Jan 31st, 2010, 09:44 AM
#29
Re: How to ping server before sending mail with smtp
An alternative to pinging.
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim s As String = DoGetHostEntry(TextBox1.Text)
If s <> "" Then
'success - do your mail, web, file stuff here
TextBox2.Text = s
Else
'failure
TextBox2.Text = "Net down, bad name, dns down"
End If
End Sub
Public Function DoGetHostEntry(ByVal hostName As String) As String
'DNS searches the local cache first. The following
'talks about how long entries are stored in local cache.
'
'http://www.helpwithwindows.com/WindowsXP/tune-24.html
'
'If you decide to change the value, use common sense please.
'
Dim host As System.Net.IPHostEntry = Nothing
Dim success As Boolean = False
Dim retval As String = ""
Try
host = System.Net.Dns.GetHostEntry(hostName)
success = True
Catch ex As Exception
flushDNS() 'if failed, cause all subsequent DNS requests to resolve
Finally
If success Then
Dim ip As System.Net.IPAddress() = host.AddressList
If ip.Length <= 0 Then
flushDNS() 'if failed, cause all subsequent DNS requests to resolve
Else
retval = ip(0).ToString
End If
End If
End Try
Return retval
'
End Function
Private Sub flushDNS()
'Use some common sense about flushing the local DNS cache!
Dim pi As New ProcessStartInfo
pi.FileName = "ipconfig.exe"
pi.Arguments = "/flushdns"
'pi.CreateNoWindow = True
Dim p As New Process
p.StartInfo = pi
p.Start()
End Sub
-
Jan 31st, 2010, 10:22 AM
#30
Re: How to ping server before sending mail with smtp
 Originally Posted by stateofidleness
but if the ping was bad, you just saved yourself 2MB.
No because like I said you dont transfer any large amount of data in an SMTP conversation until after the client has said hello to the server and the server has responded, then the client has specified the sender/recipient and the server has responded - so if the server is never online in the first place then the client will realise that the server did not respond to the hello and will not proceed any further, the most data that will have been sent will have been literally just a few bytes.
dbasnett - I dont quite understand how looking up an entry in the DNS cache is an alternative to pinging :S
-
Jan 31st, 2010, 10:26 AM
#31
Re: How to ping server before sending mail with smtp
 Originally Posted by IanS
Not at all related ? I beg to differ.
OK - let me rephrase my question - This topic is discussing how to check to see if there's an internet connection and then pinging a remote mail server.
Isn't that going to trigger alarms in the firewall or can than be done without any alarms ? Is the thread starter aware of that, how would they handle or avoid that ?
OK, that totally depends on what firewall software the user is using and how they have it set up. Some firewalls let any traffic go outbound without alerting the user, its when traffic is attempting to come in that it alerts the user - but this varies between firewalls and in most of them the user can configure it to alert them all the time if they wanted to. At the end of the day, if your application is trying to send an email then its obviously got to send some data outbound over the network so even if the user does get an alert from their firewall software saying that your program is trying to send some data then any of them that have any common sense will just let it through.
-
Jan 31st, 2010, 10:39 AM
#32
Re: How to ping server before sending mail with smtp
 Originally Posted by chris128
dbasnett - I dont quite understand how looking up an entry in the DNS cache is an alternative to pinging :S
Because it has to happen before your real work occurs. Like I keep saying...
Instead of checking, just perform the action (web request, mail, ftp, etc.) and be prepared for the request to fail, which you have to do anyway, even if your check was successful.
Consider the following:
1 - check, and it is OK
2 - start to perform action
3 - network goes down
4 - action fails
5 - lot of good your check did
If the network is down your action will fail just as rapidly as a ping, etc.
1 - start to perform action
2 - if the net is down(or goes down) the action will fail
-
Jan 31st, 2010, 10:46 AM
#33
Re: How to ping server before sending mail with smtp
Yeah I get that, but what use is it doing a DNS lookup before your action (sending an email etc) ?
-
Jan 31st, 2010, 10:54 AM
#34
Re: How to ping server before sending mail with smtp
It was just an alternative to ping'ing, which serves no useful function (unless you are net admin).
Should we start a new thread in General Developer to discuss this further?
-
Jan 31st, 2010, 11:42 AM
#35
Re: How to ping server before sending mail with smtp
-
Jan 31st, 2010, 12:22 PM
#36
Thread Starter
Junior Member
Re: How to ping server before sending mail with smtp
Nice to see that my thread started a discussion but can someone help me with my code? i googled it but i have not found anything that could help me. I want to use simple ping but i made a mistake somewhere in the code so it´s not working as it should. I would appreciate if someone can give me some kind of advice or example to see what i made wrong.
-
Jan 31st, 2010, 12:25 PM
#37
Re: How to ping server before sending mail with smtp
Your code looks fine - the only thing I can see that might be making you think it is not working is the fact that this line is after everthing else and will be called no matter what (ie the messagebox will show even if the mail was not sent)
Code:
MessageBox.Show("Request is submited.")
You should move that line to just below these two lines:
Code:
smtpserver.Send(mailMessage)
ProgressBar1.Value = 60
Then the messagebox will only be shown if the call to smtpserver.Send did not throw an exception
-
Jan 31st, 2010, 12:32 PM
#38
Re: How to ping server before sending mail with smtp
 Originally Posted by Ramiel
Nice to see that my thread started a discussion but can someone help me with my code? i googled it but i have not found anything that could help me. I want to use simple ping but i made a mistake somewhere in the code so it´s not working as it should. I would appreciate if someone can give me some kind of advice or example to see what i made wrong.
So you disagree with what I have be saying. I guess that means you should remove the Send from the Try-Catch, after all everything is great after the ping.
-
Jan 31st, 2010, 12:32 PM
#39
Thread Starter
Junior Member
Re: How to ping server before sending mail with smtp
 Originally Posted by Ramiel
.....
You are right! i have the code but it´s not working as it should. When server is down now it goes like this:
1. it warns user with message box that server is not reachable
2. When i click ok on the message box which warns user that server is not reachable then message box appears saying that request is submited
3. It does not catch exception
And it should go like this:
1. it warns user with message box that server is not reachable
2. then it should cancel sending of e-mail
3. It should catch exception
part of the problem is solved.
Now i have:
1. Message box appears saying that server is not available and this is ok
2. Problem: It does not catch exception
-
Jan 31st, 2010, 12:39 PM
#40
Thread Starter
Junior Member
Re: How to ping server before sending mail with smtp
 Originally Posted by dbasnett
So you disagree with what I have be saying. I guess that means you should remove the Send from the Try-Catch, after all everything is great after the ping.
not at all. I don´t disagree with you completely but this application i´m making is a very simple app and there is no need to implement some code for which i don´t have need. Also one more thing is name resolution which is limited in vpn on remote locations so pc´s/servers between central location and remote locations can access to each other only by ip address but not by the name of the host
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|