com.icegreen.greenmail.store.FolderException Java Examples

The following examples show how to use com.icegreen.greenmail.store.FolderException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ImapProtocolTest.java    From greenmail with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetMessageByUnknownUidInEmptyINBOX() throws MessagingException, FolderException {
    greenMail.getManagers()
            .getImapHostManager()
            .getInbox(user)
            .deleteAllMessages();
    store.connect("foo@localhost", "pwd");
    try {
        IMAPFolder folder = (IMAPFolder) store.getFolder("INBOX");
        folder.open(Folder.READ_ONLY);
        Message message = folder.getMessageByUID(666);
        assertNull(message);
    } finally {
        store.close();
    }
}
 
Example #2
Source File: AbstractImapFolder.java    From alfresco-repository with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Sets flags for the message with the given UID. If {@code addUid} is set to {@code true}
 * {@link FolderListener} objects defined for this folder will be notified.
 * {@code silentListener} can be provided - this listener wouldn't be notified.
 * 
 * @param flags - new flags.
 * @param value - flags value.
 * @param uid - message UID.
 * @param silentListener - listener that shouldn't be notified.
 * @param addUid - defines whether or not listeners be notified.
 */
public void setFlags(
        final Flags flags,
        final boolean value,
        final long uid,
        final FolderListener silentListener,
        final boolean addUid)
        throws FolderException
{
    CommandCallback<Object> command = new CommandCallback<Object>()
    {
        public Object command() throws Throwable
        {
            setFlagsInternal(flags, value, uid, silentListener, addUid);
            return null;
        }
    };
    command.runFeedback();
}
 
Example #3
Source File: AbstractImapFolder.java    From alfresco-repository with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Deletes messages marked with {@link javax.mail.Flags.Flag#DELETED}. Note that this message deletes the messages with current uid
 */
public void expunge(final long uid) throws FolderException
{
    if (isReadOnly())
    {
        throw new FolderException("Can't expunge - Permission denied");
    }
    CommandCallback<Object> command = new CommandCallback<Object>()
    {
        public Object command() throws Throwable
        {
            expungeInternal(uid);
            return null;
        }
    };
    command.runFeedback();
}
 
Example #4
Source File: AlfrescoImapFolder.java    From alfresco-repository with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Marks all messages in the folder as deleted using {@link javax.mail.Flags.Flag#DELETED} flag.
 */
@Override
public void deleteAllMessagesInternal() throws FolderException
{
    if (isReadOnly())
    {
        throw new FolderException("Can't delete all - Permission denied");
    }
    
    for (Map.Entry<Long, FileInfo> entry : searchMails().entrySet())
    {
        imapService.setFlag(entry.getValue(), Flags.Flag.DELETED, true);
        // comment out to physically remove content.
        // fileFolderService.delete(fileInfo.getNodeRef());
    }
}
 
Example #5
Source File: AbstractImapFolder.java    From alfresco-repository with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Deletes messages marked with {@link javax.mail.Flags.Flag#DELETED}. Note that this message deletes all messages with this flag.
 */
public void expunge() throws FolderException
{
    if (isReadOnly())
    {
        throw new FolderException("Can't expunge - Permission denied");
    }
    CommandCallback<Object> command = new CommandCallback<Object>()
    {
        public Object command() throws Throwable
        {
            expungeInternal();
            return null;
        }
    };
    command.runFeedback();
}
 
Example #6
Source File: AbstractImapFolder.java    From alfresco-repository with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Appends message to the folder.
 * 
 * @param message - message.
 * @param flags - message flags.
 * @param internalDate - not used. Current date used instead.
 * @return long
 */
public long appendMessage(final MimeMessage message, final Flags flags, final Date internalDate) throws FolderException
{
    if (isReadOnly())
    {
        throw new FolderException("Can't append message - Permission denied");
    }

    CommandCallback<Long> command = new CommandCallback<Long>()
    {
        public Long command() throws Throwable
        {
            return appendMessageInternal(message, flags, internalDate);
        }
    };
    return command.runFeedback();
 }
 
Example #7
Source File: AlfrescoImapFolder.java    From alfresco-repository with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Sets flags for the message with the given UID. If {@code addUid} is set to {@code true}
 * {@link FolderListener} objects defined for this folder will be notified.
 * {@code silentListener} can be provided - this listener wouldn't be notified.
 * 
 * @param flags - new flags.
 * @param value - flags value.
 * @param uid - message UID.
 * @param silentListener - listener that shouldn't be notified.
 * @param addUid - defines whether or not listeners be notified.
 * @throws MessagingException 
 * @throws FolderException 
 */
@Override
protected void setFlagsInternal(
        Flags flags,
        boolean value,
        long uid,
        FolderListener silentListener,
        boolean addUid)
        throws MessagingException, FolderException 
{
    int msn = getMsn(uid);
    FileInfo fileInfo = searchMails().get(uid);
    imapService.setFlags(fileInfo, flags, value);
    
    Long uidNotification = null;
    if (addUid)
    {
        uidNotification = new Long(uid);
    }
    notifyFlagUpdate(msn, flags, uidNotification, silentListener);

}
 
Example #8
Source File: AlfrescoImapHostManager.java    From alfresco-repository with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Returns an collection of subscribed mailboxes. To appear in search result mailboxes should have
 * {http://www.alfresco.org/model/imap/1.0}subscribed property specified for user. Method searches
 * subscribed mailboxes under mount points defined for a specific user. Mount points include user's
 * IMAP Virtualised Views and Email Archive Views. This method serves LSUB command of the IMAP protocol.
 * 
 * @param user User making the request
 * @param mailboxPattern String name of a mailbox possible including a wildcard.
 * @return Collection of mailboxes matching the pattern.
 * @throws com.icegreen.greenmail.store.FolderException
 */
public Collection<MailFolder> listSubscribedMailboxes(GreenMailUser user, String mailboxPattern)
        throws FolderException
{
    try
    {
        AlfrescoImapUser alfrescoUser = new AlfrescoImapUser(user.getEmail(), user.getLogin(), user.getPassword());
        return registerMailboxes(imapService.listMailboxes(alfrescoUser, getUnqualifiedMailboxPattern(
                alfrescoUser, mailboxPattern), true));
    }
    catch (Throwable e)
    {
        logger.debug(e.getMessage(), e);
        throw new FolderException(e.getMessage());
    }
}
 
Example #9
Source File: AlfrescoImapHostManager.java    From alfresco-repository with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Renames an existing mailbox. The specified mailbox must already exist, the requested name must not exist
 * already but must be able to be created and the user must have rights to delete the existing mailbox and
 * create a mailbox with the new name. Any inferior hierarchical names must also be renamed. If INBOX is renamed,
 * the contents of INBOX are transferred to a new mailbox with the new name, but INBOX is not deleted.
 * If INBOX has inferior mailbox these are not renamed. This method serves RENAME command of the IMAP
 * protocol. <p/> Method searches mailbox under mount points defined for a specific user. Mount points
 * include user's IMAP Virtualised Views and Email Archive Views.
 * 
 * @param user User making the request.
 * @param oldMailboxName String name of the existing folder
 * @param newMailboxName String target new name
 * @throws com.icegreen.greenmail.store.FolderException if an existing folder with the new name.
 * @throws AlfrescoImapFolderException if user does not have rights to create the new mailbox.
 */
public void renameMailbox(GreenMailUser user, String oldMailboxName, String newMailboxName) throws FolderException, AuthorizationException
{
    try
    {
        AlfrescoImapUser alfrescoUser = new AlfrescoImapUser(user.getEmail(), user.getLogin(), user.getPassword());
        String oldFolderPath = getUnqualifiedMailboxPattern(alfrescoUser,
                oldMailboxName);
        String newFolderpath = getUnqualifiedMailboxPattern(alfrescoUser, newMailboxName);
        imapService.renameMailbox(alfrescoUser, oldFolderPath, newFolderpath);
        if (folderCache != null)
        {
            folderCache.remove(oldFolderPath);
            folderCache.remove(newFolderpath);
        }
    }
    catch (Throwable e)
    {
        logger.debug(e.getMessage(), e);
        throw new FolderException(e.getMessage());
    }
}
 
Example #10
Source File: AlfrescoImapHostManager.java    From alfresco-repository with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Deletes an existing MailBox. Specified mailbox must already exist on this server, and the user
 * must have rights to delete it. <p/> This method serves DELETE command of the IMAP protocol.
 * 
 * @param user User making the request.
 * @param mailboxName String name of the target
 * @throws com.icegreen.greenmail.store.FolderException if mailbox has a non-selectable store with children
 */
public void deleteMailbox(GreenMailUser user, String mailboxName) throws FolderException, AuthorizationException
{
    try
    {
        AlfrescoImapUser alfrescoUser = new AlfrescoImapUser(user.getEmail(), user.getLogin(), user.getPassword());
        String folderPath = getUnqualifiedMailboxPattern(alfrescoUser, mailboxName);
        imapService.deleteMailbox(alfrescoUser, folderPath);
        if (folderCache != null)
        {
            folderCache.remove(folderPath);
        }
    }
    catch (Throwable e)
    {
        logger.debug(e.getMessage(), e);
        throw new FolderException(e.getMessage());
    }
}
 
Example #11
Source File: QuitCommand.java    From greenmail with Apache License 2.0 6 votes vote down vote up
@Override
public void execute(Pop3Connection conn, Pop3State state,
                    String cmd) {
    try {
        MailFolder folder = state.getFolder();
        if (folder != null) {
            folder.expunge();

        }

        conn.println("+OK bye see you soon");
        conn.quit();
    } catch (FolderException me) {
        conn.println("+OK Signing off, but message deletion failed");
        conn.quit();
    }
}
 
Example #12
Source File: AbstractImapFolder.java    From alfresco-repository with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Copies message with the given UID to the specified {@link MailFolder}.
 * 
 * @param uid - UID of the message
 * @param toFolder - reference to the destination folder.
 */
public long copyMessage(final long uid, final MailFolder toFolder) throws FolderException
{
    AbstractImapFolder toImapMailFolder = (AbstractImapFolder) toFolder;

    if (toImapMailFolder.isReadOnly())
    {
        throw new FolderException(AlfrescoImapFolderException.PERMISSION_DENIED);
    }

    CommandCallback<Long> command = new CommandCallback<Long>()
    {
        public Long command() throws Throwable
        {
            return copyMessageInternal(uid, toFolder);
        }
    };
    return command.runFeedback();
}
 
Example #13
Source File: CloseCommand.java    From greenmail with Apache License 2.0 6 votes vote down vote up
/**
 * @see CommandTemplate#doProcess
 */
@Override
protected void doProcess(ImapRequestLineReader request,
                         ImapResponse response,
                         ImapSession session)
        throws ProtocolException, FolderException {
    parser.endLine(request);

    if (!session.getSelected().isReadonly()) {
        MailFolder folder = session.getSelected();
        folder.expunge();
    }
    session.deselect();
    
    // Don't send unsolicited responses on close.
    session.unsolicitedResponses(response);
    response.commandComplete(this);
}
 
Example #14
Source File: CapabilityCommand.java    From greenmail with Apache License 2.0 6 votes vote down vote up
/**
 * @see CommandTemplate#doProcess
 */
@Override
protected void doProcess(ImapRequestLineReader request,
                         ImapResponse response,
                         ImapSession session)
        throws ProtocolException, FolderException {
    parser.endLine(request);

    if( session.getHost().getStore().isQuotaSupported()) {
        response.untaggedResponse(CAPABILITY_RESPONSE + SP + "QUOTA");
    }
    else {
        response.untaggedResponse(CAPABILITY_RESPONSE);
    }
    session.unsolicitedResponses(response);
    response.commandComplete(this);
}
 
Example #15
Source File: MailTest.java    From mangooio with Apache License 2.0 6 votes vote down vote up
@Test
public void testHtmlEncoding() throws FolderException, MangooMailerException, MessagingException, IOException {
    //given
    greenMail.purgeEmailFromAllMailboxes();
    assertThat(greenMail.getReceivedMessagesForDomain("westeros.com").length, equalTo(0));
    
    //when
    Mail.newMail()
        .from("John Snow", "[email protected]")
        .to("[email protected]")
        .subject("ÄÜÖ")
        .htmlMessage("This is a body with üäö")
        .send();
    
    //then
    await().atMost(5, TimeUnit.SECONDS).untilAsserted(() -> assertThat(greenMail.getReceivedMessagesForDomain("westeros.com").length, equalTo(1)));
    await().atMost(5, TimeUnit.SECONDS).untilAsserted(() -> assertThat(greenMail.getReceivedMessagesForDomain("westeros.com")[0].getSubject().toString(), equalTo("ÄÜÖ")));
    await().atMost(5, TimeUnit.SECONDS).untilAsserted(() -> assertThat((greenMail.getReceivedMessagesForDomain("westeros.com")[0].getContent()), equalTo("This is a body with üäö\r\n")));
}
 
Example #16
Source File: MailTest.java    From mangooio with Apache License 2.0 6 votes vote down vote up
@Test
public void testBody() throws MangooMailerException, IOException, FolderException, MessagingException, InterruptedException {
    //given
    greenMail.purgeEmailFromAllMailboxes();
    assertThat(greenMail.getReceivedMessagesForDomain("westeros.com").length, equalTo(0));
    
    //when
    Mail.newMail()
        .from("Jon snow", "[email protected]")
        .to("[email protected]")
        .subject("Lord of light")
        .textMessage("what is dead may never die")
        .send();
    
    //then
    await().atMost(5, TimeUnit.SECONDS).untilAsserted(() -> assertThat(greenMail.getReceivedMessagesForDomain("westeros.com").length, equalTo(1)));
    await().atMost(5, TimeUnit.SECONDS).untilAsserted(() -> assertThat(greenMail.getReceivedMessagesForDomain("westeros.com")[0].getContent().toString(), containsString("what is dead may never die")));
}
 
Example #17
Source File: MailTest.java    From mangooio with Apache License 2.0 6 votes vote down vote up
@Test
public void testMultiPartEmailFile() throws MangooMailerException, IOException, FolderException, MessagingException, MangooTemplateEngineException {
    //given
    greenMail.purgeEmailFromAllMailboxes();
    assertThat(greenMail.getReceivedMessagesForDomain("westeros.com").length, equalTo(0));
    File file = new File(UUID.randomUUID().toString());
    file.createNewFile();
    Map<String, Object> content = new HashMap<>();
    content.put("name", "raven");
    content.put("king", "none");
    
    //when
    Mail.newMail()
        .from("Jon Snow", "[email protected]")
        .to("[email protected]")
        .subject("Lord of light")
        .attachment(file)
        .htmlMessage("emails/multipart.ftl", content)
        .send();
    
    //then
    await().atMost(5, TimeUnit.SECONDS).untilAsserted(() -> assertThat(file.delete(), equalTo(true)));
    await().atMost(5, TimeUnit.SECONDS).untilAsserted(() -> assertThat(greenMail.getReceivedMessagesForDomain("westeros.com").length, equalTo(1)));
}
 
Example #18
Source File: MailTest.java    From mangooio with Apache License 2.0 6 votes vote down vote up
@Test
public void testHtmlEmail() throws MangooMailerException, FolderException, IOException, MessagingException, MangooTemplateEngineException {
    //given
    greenMail.purgeEmailFromAllMailboxes();
    assertThat(greenMail.getReceivedMessagesForDomain("thewall.com").length, equalTo(0));
    Map<String, Object> content = new HashMap<>();
    content.put("king", "kong");
    
    //when
    Mail.newMail()
        .from("Jon Snow", "[email protected]")
        .to("[email protected]")
        .subject("Lord of light")
        .htmlMessage("emails/html.ftl", content)
        .send();
    
    //then
    await().atMost(5, TimeUnit.SECONDS).untilAsserted(() -> assertThat(greenMail.getReceivedMessagesForDomain("thewall.com").length, equalTo(1)));
    await().atMost(5, TimeUnit.SECONDS).untilAsserted(() -> assertThat(greenMail.getReceivedMessagesForDomain("thewall.com")[0].getContent().toString(), containsString("kong")));
}
 
Example #19
Source File: AlfrescoImapHostManager.java    From alfresco-repository with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Simply calls {@link #getFolder(GreenMailUser, String)}. <p/> Added to implement {@link ImapHostManager}.
 */
public MailFolder getFolder(final GreenMailUser user, final String mailboxName, boolean mustExist)
        throws FolderException
{
    try
    {
        return getFolder(user, mailboxName);
    }
    catch (AlfrescoImapRuntimeException e)
    {
        if (!mustExist)
        {
            return null;
        }
        else if (e.getCause() instanceof FolderException)
        {
            throw (FolderException) e.getCause();
        }
        throw e;
    }
}
 
Example #20
Source File: FetchCommand.java    From greenmail with Apache License 2.0 5 votes vote down vote up
@Override
protected void doProcess(ImapRequestLineReader request,
                         ImapResponse response,
                         ImapSession session)
        throws ProtocolException, FolderException {
    doProcess(request, response, session, false);
}
 
Example #21
Source File: UidCommand.java    From greenmail with Apache License 2.0 5 votes vote down vote up
@Override
protected void doProcess(ImapRequestLineReader request,
                         ImapResponse response,
                         ImapSession session)
        throws ProtocolException, FolderException {
    String commandName = parser.atom(request);
    ImapCommand command = commandFactory.getCommand(commandName);
    if (!(command instanceof UidEnabledCommand)) {
        throw new ProtocolException("Invalid UID command: '" + commandName + "'");
    }

    ((UidEnabledCommand) command).doProcess(request, response, session, true);
}
 
Example #22
Source File: CopyCommand.java    From greenmail with Apache License 2.0 5 votes vote down vote up
/**
 * @see CommandTemplate#doProcess
 */
@Override
protected void doProcess(ImapRequestLineReader request,
                         ImapResponse response,
                         ImapSession session)
        throws ProtocolException, FolderException {
    doProcess(request, response, session, false);
}
 
Example #23
Source File: CreateCommand.java    From greenmail with Apache License 2.0 5 votes vote down vote up
/**
 * @see CommandTemplate#doProcess
 */
@Override
protected void doProcess(ImapRequestLineReader request,
                         ImapResponse response,
                         ImapSession session)
        throws ProtocolException, FolderException, AuthorizationException {
    String mailboxName = parser.mailbox(request);
    parser.endLine(request);

    session.getHost().createMailbox(session.getUser(), mailboxName);
    session.unsolicitedResponses(response);
    response.commandComplete(this);
}
 
Example #24
Source File: AuthenticationDisabledTest.java    From greenmail with Apache License 2.0 5 votes vote down vote up
@Test(expected = UserException.class)
public void testPop3ConnectAuth() throws MessagingException, UserException, FolderException {
    UserManager userManager = greenMail.getManagers().getUserManager();
    Pop3State status = new Pop3State(userManager);
    userManager.setAuthRequired(true);

    UserImpl user = new UserImpl("[email protected]", "user", "pwd", null);
    status.setUser(user);
    status.authenticate("pass");
}
 
Example #25
Source File: UserImpl.java    From greenmail with Apache License 2.0 5 votes vote down vote up
@Override
public void delete() {
    try {
        Collection<MailFolder> mailfolders = imapHostManager.listMailboxes(this, "*");
        for(MailFolder mf : mailfolders) {
            imapHostManager.deleteMailbox(this, mf.getFullName());
        }
    } catch (FolderException | AuthorizationException e) {
        throw new IllegalStateException("Can not delete user mailboxes " + this, e);
    }
}
 
Example #26
Source File: Pop3State.java    From greenmail with Apache License 2.0 5 votes vote down vote up
public void authenticate(String pass)
        throws UserException, FolderException {
    if (user == null)
        throw new UserException("No user selected");

    if (manager.isAuthRequired()) {
        user.authenticate(pass);
    }
    inbox = imapHostManager.getInbox(user);
}
 
Example #27
Source File: CheckCommand.java    From greenmail with Apache License 2.0 5 votes vote down vote up
/**
 * @see CommandTemplate#doProcess
 */
@Override
protected void doProcess(ImapRequestLineReader request,
                         ImapResponse response,
                         ImapSession session) throws ProtocolException, FolderException {
    parser.endLine(request);
    session.unsolicitedResponses(response);
    response.commandComplete(this);
}
 
Example #28
Source File: UserImpl.java    From greenmail with Apache License 2.0 5 votes vote down vote up
@Override
public void create() {
    try {
        imapHostManager.createPrivateMailAccount(this);
    } catch (FolderException e) {
        throw new IllegalStateException("Can not create user" + this, e);
    }
}
 
Example #29
Source File: ExpungeCommand.java    From greenmail with Apache License 2.0 5 votes vote down vote up
/**
 * @see CommandTemplate#doProcess
 */
@Override
protected void doProcess(ImapRequestLineReader request,
                         final ImapResponse response,
                         ImapSession session)
        throws ProtocolException, FolderException {
    doProcess(request, response, session, false);
}
 
Example #30
Source File: ExpungeCommand.java    From greenmail with Apache License 2.0 5 votes vote down vote up
/**
 * @see UidEnabledCommand#doProcess(ImapRequestLineReader, ImapResponse, ImapSession, boolean)
 */
@Override
public void doProcess(ImapRequestLineReader request, ImapResponse response, ImapSession session, boolean useUids)
        throws ProtocolException, FolderException {
    IdRange[] idSet = null;
    if (useUids) {
        idSet = parser.parseIdRange(request);
    }
    parser.endLine(request);

    if (session.getSelected().isReadonly()) {
        response.commandFailed(this, "Mailbox selected read only.");
    }

    MailFolder folder = session.getSelected();
    if (log.isDebugEnabled() && useUids) {
        log.debug("Expunging messages matching uids {} from {}", IdRange.idRangesToString(idSet) ,folder.getFullName());
    }

    if (useUids) {
        folder.expunge(idSet);
    } else {
        folder.expunge();
    }

    session.unsolicitedResponses(response);
    response.commandComplete(this);
}