Javamail 更改端口号

标签 java jakarta-mail

好吧,我一直在尝试编写一些代码来读取共享邮箱的收件箱。

我找到了这段代码

try {
        // create properties field
        Properties properties = new Properties();

        properties.put("mail.pop3.host", host);
        properties.put("mail.pop3.port", "110");
        Session emailSession = Session.getInstance(properties, null);
        emailSession.setDebug(true);

        // create the POP3 store object and connect with the pop server
        Store store = emailSession.getStore("pop3s");

        store.connect(host, username, password);

        // create the folder object and open it
        Folder emailFolder = store.getFolder("INBOX");
        emailFolder.open(Folder.READ_ONLY);

        // retrieve the messages from the folder in an array and print it
        Message[] messages = emailFolder.getMessages();
        System.out.println("messages.length---" + messages.length);

        for (int i = 0, n = messages.length; i < n; i++) {
            Message message = messages[i];
            System.out.println("---------------------------------");
            System.out.println("Email Number " + (i + 1));
            System.out.println("Subject: " + message.getSubject());
            System.out.println("From: " + message.getFrom()[0]);
            System.out.println("Text: " + message.getContent().toString());

        }

        // close the store and folder objects
        emailFolder.close(false);
        store.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

导致我出现问题的一点是,即使我指定了端口 110,它也会不断更改为 995。

这是我从 JavaMail 得到的调试结果

DEBUG: setDebug: JavaMail version 1.5.2
DEBUG: getProvider() returning javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle]
DEBUG POP3: mail.pop3s.rsetbeforequit: false
DEBUG POP3: mail.pop3s.disabletop: false
DEBUG POP3: mail.pop3s.forgettopheaders: false
DEBUG POP3: mail.pop3s.cachewriteto: false
DEBUG POP3: mail.pop3s.filecache.enable: false
DEBUG POP3: mail.pop3s.keepmessagecontent: false
DEBUG POP3: mail.pop3s.starttls.enable: false
DEBUG POP3: mail.pop3s.starttls.required: false
DEBUG POP3: mail.pop3s.apop.enable: false
DEBUG POP3: mail.pop3s.disablecapa: false
DEBUG POP3: connecting to host "removed", port 995, isSSL true

之后我收到以下错误

com.sun.mail.util.MailConnectException: Couldn't connect to host, port: removed, 995; timeout -1;

我查看了很多帖子,但似乎都没有解决这个问题,而且我在 API 中找不到对此的引用。

最佳答案

您指定了 pop3s ,但没有为其设置端口,因此它使用默认值 995。请参阅 here :

mail.pop3.ssl.enable    boolean     If set to true, use SSL to connect and use the SSL port by default. Defaults to false for the "pop3" protocol and true for the "pop3s" protocol.

我将让 @BillShannon 告诉您如何指定非默认 pop3s 端口。

或者您可能真的想使用pop3

关于Javamail 更改端口号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25776810/

相关文章:

java - AJP 连接器和 Tomcat 8.5.54 之间的网关超时问题

javax.activation.UnsupportedDataTypeException : no object DCH for MIME type text/calendar;method=REQUEST

java - 如何使用 javax mail 归档电子邮件

java - TomEE + OpenJPA + IntelliJ 实体不持久

java - 原始和原始包装器的差异最大值

java - 如何避免调用paint()方法时重新初始化一维数组

java - 当元素中仅包含部分字符串时,如何从列表中获取索引?

Java 邮件 : Unable to send email via Yahoo

java - 获取 Pop3message 的负大小

java - 使用 GMailSender 从 Android 应用程序发送带有附件的电子邮件在 Transport.send 处失败