added protocol support
This commit is contained in:
@ -255,8 +255,10 @@ public abstract class DefaultMailReader implements MailReader {
|
|||||||
int messageCount;
|
int messageCount;
|
||||||
try {
|
try {
|
||||||
|
|
||||||
|
LOGGER.debug ("getting message count from folder {}", sourceFolder);
|
||||||
folder.open(Folder.READ_ONLY);
|
folder.open(Folder.READ_ONLY);
|
||||||
messageCount = folder.getMessageCount();
|
messageCount = folder.getMessageCount();
|
||||||
|
LOGGER.debug("{} messages found infolder", messageCount);
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
throw new MailReaderException("Error while getting messageCount. Reason:" + ex.getMessage(), ex);
|
throw new MailReaderException("Error while getting messageCount. Reason:" + ex.getMessage(), ex);
|
||||||
} finally {
|
} finally {
|
||||||
|
|||||||
@ -0,0 +1,41 @@
|
|||||||
|
package de.muehlencord.shared.network.mail;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Joern Muehlencord <joern at muehlencord.de>
|
||||||
|
*/
|
||||||
|
public enum MailProtocol {
|
||||||
|
|
||||||
|
IMAP("imap"),
|
||||||
|
IMAPS("imaps");
|
||||||
|
|
||||||
|
private final static Map<String, MailProtocol> protocolNameMap = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
|
static {
|
||||||
|
for (MailProtocol currentProtocol : MailProtocol.values()) {
|
||||||
|
protocolNameMap.put(currentProtocol.getName(), currentProtocol);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static MailProtocol getByProtocolName(String protocolName) {
|
||||||
|
if (protocolNameMap.containsKey(protocolName)) {
|
||||||
|
return protocolNameMap.get(protocolName);
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private MailProtocol(String protocolName) {
|
||||||
|
this.name = protocolName;
|
||||||
|
}
|
||||||
|
|
||||||
|
private final String name;
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -5,19 +5,32 @@ package de.muehlencord.shared.network.mail;
|
|||||||
* @author Joern Muehlencord
|
* @author Joern Muehlencord
|
||||||
*/
|
*/
|
||||||
public class MailReaderConfiguration {
|
public class MailReaderConfiguration {
|
||||||
/** server for smtp sessions */
|
|
||||||
private String smtpHost = null;
|
|
||||||
/** server for incoming emails - e.g. the imap or pop3 server */
|
|
||||||
private String readerHost = null;
|
|
||||||
/** the username to connect with */
|
|
||||||
private String userName = null;
|
|
||||||
/** the password to connect with */
|
|
||||||
private String password = null;
|
|
||||||
|
|
||||||
// TODO add checkConfig to asure the config is valid for the reader it is attached to
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* server for smtp sessions
|
||||||
|
*/
|
||||||
|
private String smtpHost = null;
|
||||||
|
/**
|
||||||
|
* server for incoming emails - e.g. the imap or pop3 server
|
||||||
|
*/
|
||||||
|
private String readerHost = null;
|
||||||
|
/**
|
||||||
|
* the username to connect with
|
||||||
|
*/
|
||||||
|
private String userName = null;
|
||||||
|
/**
|
||||||
|
* the password to connect with
|
||||||
|
*/
|
||||||
|
private String password = null;
|
||||||
|
/**
|
||||||
|
* the protocol to use, currently supported imap and imaps
|
||||||
|
*/
|
||||||
|
private MailProtocol protocol = null;
|
||||||
|
|
||||||
|
// TODO add checkConfig to asure the config is valid for the reader it is attached to
|
||||||
/**
|
/**
|
||||||
* creates a new config
|
* creates a new config
|
||||||
|
*
|
||||||
* @param mailHost the smtp host to connect to
|
* @param mailHost the smtp host to connect to
|
||||||
* @param host the mailreader host (imap, exchange, pop, ...) to connect to
|
* @param host the mailreader host (imap, exchange, pop, ...) to connect to
|
||||||
* @param user the username to connect with
|
* @param user the username to connect with
|
||||||
@ -58,4 +71,12 @@ public class MailReaderConfiguration {
|
|||||||
return password;
|
return password;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public MailProtocol getProtocol() {
|
||||||
|
return protocol;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setProtocol(MailProtocol protocol) {
|
||||||
|
this.protocol = protocol;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,11 +1,13 @@
|
|||||||
package de.muehlencord.shared.network.mail.imap;
|
package de.muehlencord.shared.network.mail.imap;
|
||||||
|
|
||||||
import de.muehlencord.shared.network.mail.DefaultMailReader;
|
import de.muehlencord.shared.network.mail.DefaultMailReader;
|
||||||
|
import de.muehlencord.shared.network.mail.MailProtocol;
|
||||||
import de.muehlencord.shared.network.mail.MailReaderConfiguration;
|
import de.muehlencord.shared.network.mail.MailReaderConfiguration;
|
||||||
import de.muehlencord.shared.network.mail.MailReaderConnectionException;
|
import de.muehlencord.shared.network.mail.MailReaderConnectionException;
|
||||||
import javax.mail.Session;
|
import javax.mail.Session;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Implementation of MaiLReader to connect to an IMAP server
|
* Implementation of MaiLReader to connect to an IMAP server
|
||||||
*
|
*
|
||||||
@ -13,7 +15,9 @@ import org.slf4j.LoggerFactory;
|
|||||||
*/
|
*/
|
||||||
public class ImapMailReader extends DefaultMailReader {
|
public class ImapMailReader extends DefaultMailReader {
|
||||||
|
|
||||||
/** the logger object */
|
/**
|
||||||
|
* the logger object
|
||||||
|
*/
|
||||||
private final static Logger LOGGER = LoggerFactory.getLogger(ImapMailReader.class);
|
private final static Logger LOGGER = LoggerFactory.getLogger(ImapMailReader.class);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -32,8 +36,13 @@ public class ImapMailReader extends DefaultMailReader {
|
|||||||
String hostName = getConfiguration().getReaderHost();
|
String hostName = getConfiguration().getReaderHost();
|
||||||
String userName = getConfiguration().getUserName();
|
String userName = getConfiguration().getUserName();
|
||||||
String password = getConfiguration().getPassword();
|
String password = getConfiguration().getPassword();
|
||||||
|
MailProtocol protocol = getConfiguration().getProtocol();
|
||||||
|
if (protocol == null) {
|
||||||
|
protocol = MailProtocol.IMAP;
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
setStore(session.getStore("imap")); // TODO add ssl support
|
setStore(session.getStore(protocol.getName()));
|
||||||
getStore().connect(hostName, userName, password);
|
getStore().connect(hostName, userName, password);
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
String hint = "Error while connecting to mailbox " + getConnectionShortCut();
|
String hint = "Error while connecting to mailbox " + getConnectionShortCut();
|
||||||
|
|||||||
Reference in New Issue
Block a user