final MailboxPath path = new MailboxPath(MailboxConstants.USER_NAMESPACE, user, destination);
try {
if ("INBOX".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 (IOException e) {
throw new MessagingException("Failed to write mail message", e);
} finally {
session.close();
mailboxManager.logout(session, true);