}
} catch (UsersRepositoryException e) {
throw new MessagingException("Unable to accessUsersRepository", e);
}
MailboxSession session;
try {
session = mailboxManager.createSystemSession(user, new MailetContextLog(getMailetContext()));
} catch (BadCredentialsException e) {
throw new MessagingException("Unable to authenticate to mailbox", e);
} catch (MailboxException e) {
throw new MessagingException("Can not access mailbox", e);
}
// Start processing request
mailboxManager.startProcessingRequest(session);
// This allows Sieve scripts to use a standard delimiter
// regardless of mailbox implementation
String destination = urlPath.replace('/', session.getPathDelimiter());
if (destination == null || "".equals(destination)) {
destination = this.folder;
}
if (destination.startsWith(session.getPathDelimiter() + ""))
destination = destination.substring(1);
// Use the MailboxSession to construct the MailboxPath - See
// JAMES-1326
final MailboxPath path = new MailboxPath(MailboxConstants.USER_NAMESPACE, user, destination);
try {
if (this.folder.equalsIgnoreCase(destination) && !(mailboxManager.mailboxExists(path, session))) {
mailboxManager.createMailbox(path, session);
}
final MessageManager mailbox = mailboxManager.getMailbox(path, session);
if (mailbox == null) {
final String error = "Mailbox for user " + user + " was not found on this server.";
throw new MessagingException(error);
}
mailbox.appendMessage(new MimeMessageInputStream(mail), new Date(), session, true, null);
} catch (MailboxException e) {
throw new MessagingException("Unable to access mailbox.", e);
} finally {
session.close();
try {
mailboxManager.logout(session, true);
} catch (MailboxException e) {
throw new MessagingException("Can logout from mailbox", e);
}