如何让javamail支持http代理

How to let javamail support http proxy

我发现javamail只支持socks。有什么解决方案可以用来支持 http 代理吗?

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;
}
}

C te Jawamile Fak:

... Without such a SOCKS server, if you want to use JavaMail to access mail servers outside the firewall indirectly, you might be able to use a program such as Corkscrew or connect to tunnel TCP connections through an HTTP proxy server. JavaMail does not support direct access through an HTTP proxy web server.

javamail api 1.6 支持 web 服务器代理

设置这些属性

mail.protocol.proxy.host

mail.protocol.proxy.port

对于 smtp 设置为

mail.smtp.proxy.host

mail.smtp.proxy.port

根据最新版本的 Javamail API 1.6.2,JavaMail 支持通过 Web 代理服务器访问邮件服务器,并支持向代理服务器进行身份验证。请在下面查看我的代码。

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("umesh@yahoo.com", "test123");
    }

}

该实现仅支持 Web 代理的基本身份验证。您可以在 com.sun.mail.util.SocketFetcher.

中找到源代码

既然javamail已经支持NTLM认证,web代理支持NTLM认证就不难了。