if (StringUtils.isBlank(provider)) {
provider = DEFAULT_PROVIDER;
}
account = new Account(ctx);
} catch (IllegalArgumentException e) {
throw new JobException(this, e);
}
// Connect to the server
Properties sessionProperties = new Properties();
Session session = Session.getDefaultInstance(sessionProperties, null);
Store store = null;
Folder inbox = null;
try {
// Connect to the server
try {
store = session.getStore(provider);
store.connect(account.getHost(), account.getLogin(), account.getPassword());
} catch (NoSuchProviderException e) {
throw new JobException(this, "Unable to connect using unknown e-mail provider '" + provider + "'", e);
} catch (MessagingException e) {
throw new JobException(this, "Error connecting to " + provider + " account " + account, e);
}
// Open the account's inbox
try {
inbox = store.getFolder(INBOX);
if (inbox == null)
throw new JobException(this, "No inbox found at " + account);
inbox.open(Folder.READ_WRITE);
} catch (MessagingException e) {
throw new JobException(this, "Error connecting to inbox at " + account, e);
}
// Get the messages from the server
try {
for (Message message : inbox.getMessages()) {
if (!message.isSet(Flag.SEEN)) {
try {
Page page = aggregate(message, site);
message.setFlag(Flag.DELETED, true);
repository.put(page, true);
logger.info("E-Mail message published at {}", page.getURI());
} catch (Exception e) {
logger.info("E-Mail message discarded: {}", e.getMessage());
message.setFlag(Flag.SEEN, true);
// TODO: Reply to sender if the "from" field exists
}
}
}
} catch (MessagingException e) {
throw new JobException(this, "Error loading e-mail messages from inbox", e);
}
// Close the connection
// but don't remove the messages from the server
} finally {
if (inbox != null) {
try {
inbox.close(true);
} catch (MessagingException e) {
throw new JobException(this, "Error closing inbox", e);
}
}
if (store != null) {
try {
store.close();
} catch (MessagingException e) {
throw new JobException(this, "Error closing connection to e-mail server", e);
}
}
}
}