Search code examples
javajakarta-mail

Javamail Changing Port Number


Ok i've been trying to put together some code that will read the inbox of a shared mailbox.

I was able to find this code

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

the point that is causing me issues is that even though I've specified port 110 it keeps changing to 995.

Here is the debugging from JavaMail that I get

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

after which i get the following error

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

I've looked at a number of posts and none seem to address this issue and i cannot find a reference to it in the API.


Solution

  • You specified pop3s without setting a port for it, so it used the default of 995. See 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.
    

    I'll leave it to @BillShannon to tell you how to specify a non-default pop3s port.

    Or possibly you really meant to use pop3?