mail sessions - too many active sessions

Viewed 45

I'm trying to develope a mail program to download/read emails from an Internet-server (e.g. gmail, gmx, ...). Therefore I need:

  • a Message[]-Array to store/copy the INBOX-folder from the server
  • a Store-object to connect to the pop3-Server.
  • a Map<String,List<?>> to keep track of three different lists, storing the sender-adress, the sent-date and the subject of the mail, respectively.

Since each of this methods needs to access the pop3-server, a store-object is created and connected to the pop-server. When creating the store-object seperately in each method, I get a "javax.mail.AuthenticationFailedException" (too many axctive sessions). I guess, that for each mail in the Message[]-Array, a single session-Object is created, thus getting about 1.800 sessions. Is there a way to create the store-object or the session kind-of globally an pass the same instance to each method? And, especially, to use one store-Object for each message in the Array?

Thanks for any tips.

PS.: Dunno if necessary to be mentioned, but I'm using javax.mail on an eclipse-IDE

1 Answers

Wow, thanks for the immediate answere! So here's what I'm doing right now:

Setting server-properties:

protected Properties getServerProperties(String protocol, String host, String port){
    Properties properties = new Properties();

    properties.put(String.format("mail.%s.host", protocol), host);
    properties.put(String.format("mail.%s.port", protocol), port);
    
    properties.setProperty(String.format("mail.%s.socketFactory.class", protocol), "javax.net.ssl.SSLSocketFactory");
    properties.setProperty(String.format("mail.%s.socketFactory.fallback", protocol), "false");
    properties.setProperty(String.format("mail.%s.socketFactory.port", protocol), String.valueOf(port));
    properties.setProperty("mail.pop3.port", "995");
    properties.setProperty("mail.pop3.host", "pop.gmx.net");
    properties.setProperty("mail.pop3.user", "****");
    properties.setProperty("mail.pop3.ssl.protocols", "TLSv1.2");   

    return properties;
}

Creating Message[]-array:

public Message[] return_messages(String protocol, String host, String port, String userName, String password) throws MessagingException {
    Properties properties = getServerProperties(protocol, host, port);
    Session session = Session.getInstance(properties, auth);
    Store store = session.getStore(protocol);
    store.connect("pop.gmx.net", userName, password);
    
    folderInbox = store.getFolder("INBOX");
    folderInbox.open(Folder.READ_ONLY);
    
    Message[] messages = folderInbox.getMessages();
    return messages;
}

Open Mail-Folder

public Store open_mail_folder(String protocol, String host, String port, String userName, String password) throws MessagingException {
    Properties properties = getServerProperties(protocol, host, port);
    Session session = Session.getInstance(properties, auth);
    Store store = session.getStore(protocol);
    store.connect("pop.gmx.net", userName, password);
    return store;
}

Storing Mails in a list

Map<String,List<?>> map = new HashMap<String, List<?>>();
//@SuppressWarnings("static-access")
public Map<String, List<?>> downloadEmails(String protocol, String host, String port, String userName, String password) throws IOException {
    Properties properties = getServerProperties(protocol, host, port);
    Session session = Session.getInstance(properties, auth);
        
    try {
        Store store = null;
        
        if(store == null) {
            store = session.getStore(protocol);
            store.connect("pop.gmx.net", userName, password);
            }
                    
        folderInbox = store.getFolder("INBOX");
        folderInbox.open(Folder.READ_ONLY);
        
        Message[] messages = folderInbox.getMessages();
        
        List<String> fromList = new ArrayList<String>();
        List<Date> dateList = new ArrayList<Date>();
        List<String> subjectList = new ArrayList<String>();
        
        for(int i=0;i<messages.length;i++) {
            Address[] from = messages[i].getFrom();
            
            String from_temp = (String)(((InternetAddress)from[0]).getAddress());
            fromList.add(from_temp);
            
            Date date = messages[i].getSentDate();
            dateList.add(date);
            
            String subject = messages[i].getSubject();
            subjectList.add(subject);
        }
        
        map.put("fromList", fromList);
        map.put("dateList", dateList);
        map.put("subjectList", subjectList);
                    
        String result = "";
        StringBuilder textBuilder = new StringBuilder();
            
    }catch (NoSuchProviderException ex) {
        //for debugging only
        System.out.println("No provider for protocol " + protocol);
        ex.printStackTrace();
    }catch (MessagingException ex) {
        System.out.println("Could not connect to the message store");
        ex.printStackTrace();
    }
    
    return map;
}

Furthermore, since the connection to the pop-server seems to be closed automatically after some time, I'm running a thread periodically to check, whether the connection is still opend or, if not, to re-connect:

public class Session_relog extends Mail_Reader implements Runnable {
static int session_count;

public Session_relog() {
    // TODO Auto-generated constructor stub
}

@Override
public void run() {
    // TODO Auto-generated method stub
    while(true) {
        try {
            Thread.sleep(30,000);
            if(super.isopen_mail_folder("pop3", "pop.gmx.net", "955", "****", "****")==false) {
                try {
                    super.open_mail_folder("pop3", "pop.gmx.net", "995", "****", "****");
                } catch (MessagingException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            else {
                Thread.sleep(30,000); 
                continue;
                }
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (MessagingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

}

Related