views:

26

answers:

2

I have a PC which connects to internet through a proxy server. I'm able to browse google.com and all other sites. However i'm unable to ping google.com, smtp.gmail.com or any other websites . I'm trying send some automated mails through a java program, but every time it fails to connect to smtp.gmail.com. Is there any proxy configurations i need to make in my java program..??

A: 

Yes.If you are behind a firewall, you need to configure proxy. To do so, use this code piece.

Properties systemProperties = System.getProperties();
systemProperties.setProperty("http.proxyHost", "a.b.c.d");//replace a.b.c.d with your proxy IP
systemProperties.setProperty("http.proxyPort", "80");

Optionally,You might want to override DefaultAuthenticator. If your proxy requires authentication, you might have to do this as well.

Authenticator.setDefault(new Authenticator() {
   protected PasswordAuthentication getPasswordAuthentication() {
   return new PasswordAuthentication("user","password".toCharArray());
   }
});
chedine
A: 

Short answer is: set properties http.proxyHost and http.proxyPort either using -D when invoking Java, or using Properties.

For details, have a look at Java Networking and Proxies

MarcoS