use of javax.mail.Store in project opennms by OpenNMS.
the class JavaReadMailer method retrieveMessages.
/*
* TODO: Need readers that:
* - use FetchProfiles
* - make use of pre-fetch nature of getMessages()
* - A reader for the entire system could be implemented using events/event listeners
* TODO: Need to make this more efficient... probably needs state so that message contents can be retrieved from the
* store after they are read via this message.
*/
/**
* <p>retrieveMessages</p>
*
* @param term a {@link javax.mail.search.SearchTerm} object.
* @return a {@link java.util.List} object.
* @throws org.opennms.javamail.JavaMailerException if any.
*/
public List<Message> retrieveMessages(SearchTerm term) throws JavaMailerException {
Message[] msgs;
Folder mailFolder = null;
final ReadmailHost readmailHost = getReadmailHost(m_config);
final UserAuth userAuth = getUserAuth(m_config);
try {
Store store = m_session.getStore(readmailHost.getReadmailProtocol().getTransport());
store.connect(readmailHost.getHost(), (int) readmailHost.getPort(), userAuth.getUserName(), userAuth.getPassword());
mailFolder = store.getFolder(m_config.getMailFolder());
mailFolder.open(Folder.READ_WRITE);
msgs = mailFolder.search(term);
} catch (NoSuchProviderException e) {
throw new JavaMailerException("No provider matching:" + readmailHost.getReadmailProtocol().getTransport() + " from config:" + m_config.getName(), e);
} catch (MessagingException e) {
throw new JavaMailerException("Problem reading messages from configured mail store", e);
}
List<Message> msgList = Arrays.asList(msgs);
return msgList;
}
use of javax.mail.Store in project epadd by ePADD.
the class EmailStore method getNMessages.
/**
* returns # of messages in each of the given folders
*/
public int[] getNMessages(String[] fnames) throws MessagingException {
Store store = connect();
int[] x = new int[fnames.length];
for (int i = 0; i < x.length; i++) {
Pair<Folder, Integer> pair = openFolder(store, fnames[i]);
Folder f = pair.getFirst();
if (f == null) {
x[i] = -1;
continue;
}
int count = pair.getSecond();
if (count != -1)
f.close(false);
x[i] = count;
}
try {
store.close();
} catch (Exception e) {
log.warn("Exception in closing folder " + this + ":" + e);
}
return x;
}
use of javax.mail.Store in project epadd by ePADD.
the class EmailStore method getNMessages.
/**
* returns # of messages in the given folder
*/
public int getNMessages(String fname) throws MessagingException {
// first check if we've already cached it
if (this.folderInfos != null)
for (FolderInfo fi : this.folderInfos) if (fi.longName.equals(fname))
return fi.messageCount;
Store store = connect();
Pair<Folder, Integer> pair = openFolder(store, fname);
Folder f = pair.getFirst();
if (f == null)
return -1;
int count = pair.getSecond();
if (count != -1)
f.close(false);
store.close();
return count;
}
use of javax.mail.Store in project epadd by ePADD.
the class ImapPopEmailStore method connect.
// connects to the store, returns it as well as stores it in this.store
public Store connect() throws MessagingException {
if (// should be at least imap or pop
Util.nullOrEmpty(connectOptions.protocol))
return null;
// Get a Session object
// can customize javamail properties here, see e.g. http://java.sun.com/products/javamail/javadocs/com/sun/mail/imap/package-summary.html
// login form will prepend the magic string xoauth, if oauth is being used
String oauthToken = "";
// defined in loginform
String OAUTH_MAGIC_STRING = "xoauth";
if (connectOptions.password.startsWith(OAUTH_MAGIC_STRING)) {
if ("xoauth".length() < connectOptions.password.length()) {
oauthToken = connectOptions.password.substring(OAUTH_MAGIC_STRING.length());
mstoreProps.put("mail.imaps.sasl.enable", "true");
mstoreProps.put("mail.imaps.sasl.mechanisms", "XOAUTH2");
// mstoreProps.put(OAuth2SaslClientFactory.OAUTH_TOKEN_PROP, oauthToken);
log.info("Using oauth login for store " + this);
}
}
session = Session.getInstance(mstoreProps, null);
// session.setDebug(DEBUG);
Store st = session.getStore(connectOptions.protocol);
// no password if oauth
st.connect(connectOptions.server, connectOptions.port, connectOptions.userName, Util.nullOrEmpty(oauthToken) ? connectOptions.password : "");
this.store = st;
return st;
}
use of javax.mail.Store in project epadd by ePADD.
the class VerifyEmailSetup method run.
public static Pair<Boolean, String> run() {
PrintStream savedOut = System.out;
InputStream savedIn = System.in;
try {
// Get a Properties object
Properties props = System.getProperties();
// Get a Session object
Session session = Session.getInstance(props, null);
session.setDebug(true);
String filename = System.getProperty("java.io.tmpdir") + File.separatorChar + "verifyEmailSetup";
PrintStream ps = new PrintStream(new FileOutputStream(filename));
System.setOut(ps);
System.setErr(ps);
// Get a Store object
Store store = null;
store = session.getStore("imaps");
// not the real password. unfortunately, the checkmuse a/c will get blocked by google.
store.connect("imap.gmail.com", 993, "checkmuse", "");
// Folder folder = store.getFolder("[Gmail]/Sent Mail");
// int totalMessages = folder.getMessageCount();
// System.err.println (totalMessages + " messages!");
ps.close();
String contents = Util.getFileContents(filename);
System.out.println(contents);
return new Pair<>(Boolean.TRUE, contents);
} catch (AuthenticationFailedException e) {
/* its ok if auth failed. we only want to check if IMAPS network route is blocked.
when network is blocked, we'll get something like
javax.mail.MessagingException: No route to host;
nested exception is:
java.net.NoRouteToHostException: No route to host
...
*/
log.info("Verification succeeded: " + Util.stackTrace(e));
return new Pair<>(Boolean.TRUE, "");
} catch (Exception e) {
log.warn("Verification failed: " + Util.stackTrace(e));
// stack track reveals too much about our code... Util.stackTrace(e));
return new Pair<>(Boolean.FALSE, e.toString());
} finally {
System.setOut(savedOut);
System.setIn(savedIn);
}
}
Aggregations