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