如何让javamail支持http代理

Cai*_*ain 2 java jakarta-mail 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;
}
}
Run Code Online (Sandbox Code Playgroud)

小智 6

根据 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");
    }

}
Run Code Online (Sandbox Code Playgroud)