aspose file tools*
The moose likes Other JSE/JEE APIs and the fly likes Java Mail behind Proxy Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "Java Mail behind Proxy" Watch "Java Mail behind Proxy" New topic
Author

Java Mail behind Proxy

Niall Loughnane
Ranch Hand

Joined: Dec 07, 2006
Posts: 208
Hi,

I am trying to send an email with Java Mail but i am behind a proxy and the email is being unable to be sent,

The Java code that i have is:


the exception that I am getting is:
"
javax.mail.MessagingException: Unknown SMTP host: smtp.gmail.com;
nested exception is:
java.net.UnknownHostException: smtp.gmail.com
"



When I send a Http request using HttpClient i use:




Is there a way to set the proxy within the javax.mail.* package?



Thanks in Advance,

Niall
Nitin Surana
Ranch Hand

Joined: Jan 21, 2011
Posts: 129

As long as I remember, Java Mail API till date has no support for proxy.
Niall Loughnane
Ranch Hand

Joined: Dec 07, 2006
Posts: 208
Hi Nitin,

Thanks,

that is what i was thinking,

but do you know of any library that can send an email with and does support being behind a proxy?

Thanks,

Niall
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19759
    
  20

http://www.oracle.com/technetwork/java/faq-135477.html#proxy wrote:
Q: How do I configure JavaMail to work through my proxy server?
A: JavaMail does not currently support accessing mail servers through a web proxy server. One of the major reasons for using a proxy server is to allow HTTP requests from within a corporate network to pass through a corporate firewall. The firewall will typically block most access to the Internet, but will allow requests from the proxy server to pass through. In addition, a mail server inside the corporate network will perform a similar function for email, accepting messages via SMTP and forwarding them to their ultimate destination on the Internet, and accepting incoming messages and sending them to the appropriate internal mail server.

If your proxy server supports the SOCKS V4 or V5 protocol (http://www.socks.nec.com/aboutsocks.html, RFC1928) and allows anonymous connections, you can tell the Java runtime to direct all TCP socket connections to the SOCKS server. See the Networking Properties guide for the latest documentation of the socksProxyHost and socksProxyPort properties. These are system-level properties, not JavaMail session properties. They can be set from the command line when the application is invoked, for example: java -DsocksProxyHost=myproxy .... This facility can be used to direct the SMTP, IMAP, and POP3 communication from JavaMail to the SOCKS proxy server. Note that setting these properties directs all TCP sockets to the SOCKS proxy, which may have negative impact on other aspects of your application.

Without such a SOCKS server, if you want to use JavaMail to directly access mail servers outside the firewall, the firewall will need to be configured to allow such access. JavaMail does not support access through a HTTP proxy web server.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Niall Loughnane
Ranch Hand

Joined: Dec 07, 2006
Posts: 208
Thanks,

HttpClient allows to send data through proxy servers so it is weird that JavaMail does not,

They are both the transfer of data.

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19759
    
  20

But:
- they are not using the same protocol (HTTP vs SMTP)
- they are not part of the same library (Apache HttpClient vs JavaMail)
- they are not written by the same people
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18881
    
    8

Seems to me that the simplest thing to do would be to have the network people configure the proxy to allow you to connect directly to the gmail server.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java Mail behind Proxy