How to bypass proxy wifi for java mail api

A common issue!

To bypass a proxy WiFi for Java Mail API, you can use the following approaches:

  1. Set the proxy settings programmatically:

You can set the proxy settings using the mail.smtp.proxy property in your Java code. Here's an example:

Properties props = new Properties();
props.put("mail.smtp.proxy", "http://your-proxy-server:port");
props.put("mail.smtp.proxySet", "true");
Session session = Session.getInstance(props);

Replace your-proxy-server with the hostname or IP address of your proxy server, and port with the port number.

  1. Use a system property:

You can set the http.proxySet and http.proxyHost system properties to bypass the proxy for the Java Mail API. Here's an example:

System.setProperty("http.proxySet", "true");
System.setProperty("http.proxyHost", "your-proxy-server");
System.setProperty("http.proxyPort", "port");

Replace your-proxy-server with the hostname or IP address of your proxy server, and port with the port number.

  1. Use a JavaMail session property:

You can set the mail.smtp.session property to bypass the proxy for the Java Mail API. Here's an example:

Properties props = new Properties();
props.put("mail.smtp.session", "true");
Session session = Session.getInstance(props);
  1. Use a custom MailSession:

You can create a custom MailSession that bypasses the proxy. Here's an example:

MailSession session = new MailSession("your-proxy-server", "port");
session.setProxySet(true);

Replace your-proxy-server with the hostname or IP address of your proxy server, and port with the port number.

  1. Use a third-party library:

You can use a third-party library like Apache HttpClient or OkHttp to bypass the proxy for the Java Mail API. These libraries provide more advanced proxy configuration options.

Remember to replace your-proxy-server and port with the actual values for your proxy server.