'How to let javamail support http proxy

I found that javamail only support socks. Is there any solution I can use to support http proxy?

public class MailConnectionTest {
 public static void main(String args[]) throws MessagingException {
   Properties props = MailConnectionTest.getProperties();
   Session session = Session.getDefaultInstance(props, null);
   String protocol = "pop3";
   String host = "pop.163.com";
   String username = "email username";
   String password = "1Qaz2wsx3edc&";
   Store store = session.getStore(protocol);
   store.connect(host, username, password);
   System.out.println("Success");
}
private static Properties getProperties() {
 Properties props = System.getProperties();
 props.put("mail.debug", "false");
 // Proxy
 props.put("proxySet", "true");
 props.put("http.proxyHost", "proxyAdderss");
 props.put("http.proxyPort", "8080");
 return props;
}
}


Solution 1:[1]

As per the latest release of Javamail API 1.6.2 , JavaMail supports accessing mail servers through a web proxy server and also authenticating to the proxy server. Please see my code below.

import java.io.IOException;
import java.net.Authenticator;
import java.net.MalformedURLException;
import java.net.PasswordAuthentication;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.mail.BodyPart;
import javax.mail.Flags;
import javax.mail.Flags.Flag;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.search.FlagTerm;

public class ReadMailProxy {

    public static void receiveMail(String userName, String password) {
        try {
            String proxyIP = "124.124.124.14";
            String proxyPort = "4154";
            String proxyUser = "test";
            String proxyPassword = "test123";
            Properties prop = new Properties();
            prop.setProperty("mail.imaps.proxy.host", proxyIP);
            prop.setProperty("mail.imaps.proxy.port", proxyPort);
            prop.setProperty("mail.imaps.proxy.user", proxyUser);
            prop.setProperty("mail.imaps.proxy.password", proxyPassword);

            Session eSession = Session.getInstance(prop);

            Store eStore = eSession.getStore("imaps");
            eStore.connect("imap.mail.yahoo.com", userName, password);

            Folder eFolder = eStore.getFolder("Inbox");
            eFolder.open(Folder.READ_WRITE);
            Message messages[] = eFolder.search(new FlagTerm(new Flags(Flags.Flag.SEEN), false));
            System.out.println(messages.length);
            for (int i = messages.length - 3; i < messages.length - 2; i++) {
                Message message = messages[i];
                System.out.println("Email Number::" + (i + 1));
                System.out.println("Subject::" + message.getSubject());
                System.out.println("From::" + message.getFrom()[0]);
                System.out.println("Date::" + message.getSentDate());

                try {
                    Multipart multipart = (Multipart) message.getContent();

                    for (int x = 0; x < multipart.getCount(); x++) {
                        BodyPart bodyPart = multipart.getBodyPart(x);

                        String disposition = bodyPart.getDisposition();

                        if (disposition != null && (disposition.equals(BodyPart.ATTACHMENT))) {
                            System.out.println("Mail have some attachment : ");

                            DataHandler handler = bodyPart.getDataHandler();
                            System.out.println("file name : " + handler.getName());
                        } else {
                            System.out.println(bodyPart.getContent());
                        }

                    }
                } catch (Exception e) {
                    System.out.println("Content: " + message.getContent().toString());
                }

                message.setFlag(Flag.SEEN, true);
            }
            eFolder.close(true);
            eStore.close();

        } catch (NoSuchProviderException e) {
            e.printStackTrace();
        } catch (MessagingException e) {
            e.printStackTrace();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public static void main(String[] args) {
        receiveMail("[email protected]", "test123");
    }

}

Solution 2:[2]

javamail api 1.6 supports web server proxy

set these properties

mail.protocol.proxy.host

mail.protocol.proxy.port

for smtp set as

mail.smtp.proxy.host

mail.smtp.proxy.port

Solution 3:[3]

The implementation only support basic authentication for web proxy. You can find the source code in com.sun.mail.util.SocketFetcher.

Since javamail support NTLM authentication already, it is not hard to support NTLM authentication for web proxy.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Katighar Umesh
Solution 2 Tamilvanan T
Solution 3 Tim