javax.mail.MessageRemovedException Java Examples

The following examples show how to use javax.mail.MessageRemovedException. 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: Core.java    From FairEmail with GNU General Public License v3.0 6 votes vote down vote up
private static void onSeen(Context context, JSONArray jargs, EntityFolder folder, EntityMessage message, IMAPFolder ifolder) throws MessagingException, JSONException {
    // Mark message (un)seen
    DB db = DB.getInstance(context);

    if (!ifolder.getPermanentFlags().contains(Flags.Flag.SEEN)) {
        db.message().setMessageSeen(message.id, false);
        db.message().setMessageUiSeen(message.id, false);
        return;
    }

    boolean seen = jargs.getBoolean(0);
    if (message.seen.equals(seen))
        return;

    Message imessage = ifolder.getMessageByUID(message.uid);
    if (imessage == null)
        throw new MessageRemovedException();

    imessage.setFlag(Flags.Flag.SEEN, seen);

    db.message().setMessageSeen(message.id, seen);
}
 
Example #2
Source File: Core.java    From FairEmail with GNU General Public License v3.0 6 votes vote down vote up
private static void onFlag(Context context, JSONArray jargs, EntityFolder folder, EntityMessage message, IMAPFolder ifolder) throws MessagingException, JSONException {
    // Star/unstar message
    DB db = DB.getInstance(context);

    if (!ifolder.getPermanentFlags().contains(Flags.Flag.FLAGGED)) {
        db.message().setMessageFlagged(message.id, false);
        db.message().setMessageUiFlagged(message.id, false, null);
        return;
    }

    boolean flagged = jargs.getBoolean(0);
    if (message.flagged.equals(flagged))
        return;

    Message imessage = ifolder.getMessageByUID(message.uid);
    if (imessage == null)
        throw new MessageRemovedException();

    imessage.setFlag(Flags.Flag.FLAGGED, flagged);

    db.message().setMessageFlagged(message.id, flagged);
}
 
Example #3
Source File: Core.java    From FairEmail with GNU General Public License v3.0 6 votes vote down vote up
private static void onAnswered(Context context, JSONArray jargs, EntityFolder folder, EntityMessage message, IMAPFolder ifolder) throws MessagingException, JSONException {
    // Mark message (un)answered
    DB db = DB.getInstance(context);

    if (!ifolder.getPermanentFlags().contains(Flags.Flag.ANSWERED)) {
        db.message().setMessageAnswered(message.id, false);
        db.message().setMessageUiAnswered(message.id, false);
        return;
    }

    boolean answered = jargs.getBoolean(0);
    if (message.answered.equals(answered))
        return;

    // This will be fixed when moving the message
    if (message.uid == null)
        return;

    Message imessage = ifolder.getMessageByUID(message.uid);
    if (imessage == null)
        throw new MessageRemovedException();

    imessage.setFlag(Flags.Flag.ANSWERED, answered);

    db.message().setMessageAnswered(message.id, answered);
}
 
Example #4
Source File: Core.java    From FairEmail with GNU General Public License v3.0 6 votes vote down vote up
private static void onKeyword(Context context, JSONArray jargs, EntityFolder folder, EntityMessage message, IMAPFolder ifolder) throws MessagingException, JSONException {
    // Set/reset user flag
    DB db = DB.getInstance(context);
    // https://tools.ietf.org/html/rfc3501#section-2.3.2
    String keyword = jargs.getString(0);
    boolean set = jargs.getBoolean(1);

    if (TextUtils.isEmpty(keyword))
        throw new IllegalArgumentException("keyword/empty");

    if (!ifolder.getPermanentFlags().contains(Flags.Flag.USER)) {
        db.message().setMessageKeywords(message.id, DB.Converters.fromStringArray(null));
        return;
    }

    if (message.uid == null)
        throw new IllegalArgumentException("keyword/uid");

    Message imessage = ifolder.getMessageByUID(message.uid);
    if (imessage == null)
        throw new MessageRemovedException();

    Flags flags = new Flags(keyword);
    imessage.setFlags(flags, set);
}
 
Example #5
Source File: Core.java    From FairEmail with GNU General Public License v3.0 6 votes vote down vote up
private static void onBody(Context context, JSONArray jargs, EntityFolder folder, EntityMessage message, IMAPFolder ifolder) throws MessagingException, IOException {
    // Download message body
    DB db = DB.getInstance(context);

    if (message.content)
        return;

    // Get message
    Message imessage = ifolder.getMessageByUID(message.uid);
    if (imessage == null)
        throw new MessageRemovedException();

    MessageHelper helper = new MessageHelper((MimeMessage) imessage, context);
    MessageHelper.MessageParts parts = helper.getMessageParts();
    String body = parts.getHtml(context);
    File file = message.getFile(context);
    Helper.writeText(file, body);
    db.message().setMessageContent(message.id,
            true,
            HtmlHelper.getLanguage(context, body),
            parts.isPlainOnly(),
            HtmlHelper.getPreview(body),
            parts.getWarnings(message.warning));
}
 
Example #6
Source File: NotificationManagementServiceComponent.java    From carbon-identity with Apache License 2.0 6 votes vote down vote up
/**
 * Will register message sending modules dynamically. This method is used to bind the notification sending
 * modules in to msg mgt component
 *
 * @param module MessageSendingModule
 */
protected void addNotificationSendingModule(NotificationSendingModule module) throws MessageRemovedException {

    ModuleConfiguration moduleConfiguration;
    if (StringUtils.isEmpty(module.getModuleName())) {
        if (log.isDebugEnabled()) {
            log.debug("Cannot register module without a valid module name");
        }
        return;
    }
    if (log.isDebugEnabled()) {
        log.debug("Registering a message sending module " + module.getModuleName());
    }

    if (configBuilder != null) {
        moduleConfiguration = configBuilder.getModuleConfigurations(module.getModuleName());
    } else {
        moduleConfiguration = new ModuleConfiguration();
    }
    try {
        module.init(moduleConfiguration);
        notificationSendingModules.add(module);
    } catch (NotificationManagementException e) {
        log.error("Error while initializing Notification sending module " + module.getModuleName(), e);
    }
}
 
Example #7
Source File: Core.java    From FairEmail with GNU General Public License v3.0 5 votes vote down vote up
private static void onHeaders(Context context, JSONArray jargs, EntityFolder folder, EntityMessage message, IMAPFolder ifolder) throws MessagingException {
    // Download headers
    DB db = DB.getInstance(context);

    if (message.headers != null)
        return;

    IMAPMessage imessage = (IMAPMessage) ifolder.getMessageByUID(message.uid);
    if (imessage == null)
        throw new MessageRemovedException();

    MessageHelper helper = new MessageHelper(imessage, context);
    db.message().setMessageHeaders(message.id, helper.getHeaders());
}
 
Example #8
Source File: Core.java    From FairEmail with GNU General Public License v3.0 5 votes vote down vote up
private static void onRaw(Context context, JSONArray jargs, EntityFolder folder, EntityMessage message, IMAPFolder ifolder) throws MessagingException, IOException, JSONException {
    // Download raw message
    DB db = DB.getInstance(context);

    if (message.raw == null || !message.raw) {
        IMAPMessage imessage = (IMAPMessage) ifolder.getMessageByUID(message.uid);
        if (imessage == null)
            throw new MessageRemovedException();

        File file = message.getRawFile(context);
        try (OutputStream os = new BufferedOutputStream(new FileOutputStream(file))) {
            imessage.writeTo(os);
        }

        db.message().setMessageRaw(message.id, true);
    }

    if (jargs.length() > 0) {
        // Cross account move
        long tid = jargs.getLong(0);
        EntityFolder target = db.folder().getFolder(tid);
        if (target == null)
            throw new FolderNotFoundException();

        Log.i(folder.name + " queuing ADD id=" + message.id + ":" + target.id);

        EntityOperation operation = new EntityOperation();
        operation.account = target.account;
        operation.folder = target.id;
        operation.message = message.id;
        operation.name = EntityOperation.ADD;
        operation.args = jargs.toString();
        operation.created = new Date().getTime();
        operation.id = db.operation().insertOperation(operation);
    }
}
 
Example #9
Source File: Core.java    From FairEmail with GNU General Public License v3.0 5 votes vote down vote up
private static void onAttachment(Context context, JSONArray jargs, EntityFolder folder, EntityMessage message, EntityOperation op, IMAPFolder ifolder) throws JSONException, MessagingException, IOException {
    // Download attachment
    DB db = DB.getInstance(context);

    long id = jargs.getLong(0);

    // Get attachment
    EntityAttachment attachment = db.attachment().getAttachment(id);
    if (attachment == null)
        attachment = db.attachment().getAttachment(message.id, (int) id); // legacy
    if (attachment == null)
        throw new IllegalArgumentException("Local attachment not found");
    if (attachment.available)
        return;

    // Get message
    Message imessage = ifolder.getMessageByUID(message.uid);
    if (imessage == null)
        throw new MessageRemovedException();

    // Get message parts
    MessageHelper helper = new MessageHelper((MimeMessage) imessage, context);
    MessageHelper.MessageParts parts = helper.getMessageParts();

    // Download attachment
    parts.downloadAttachment(context, attachment);
}
 
Example #10
Source File: NotificationManagementServiceComponent.java    From carbon-identity-framework with Apache License 2.0 5 votes vote down vote up
/**
 * Will register message sending modules dynamically. This method is used to bind the notification sending
 * modules in to msg mgt component
 *
 * @param module MessageSendingModule
 */
@Reference(
        name = "ldap.tenant.manager.listener.service",
        service = NotificationSendingModule.class,
        cardinality = ReferenceCardinality.MULTIPLE,
        policy = ReferencePolicy.DYNAMIC,
        unbind = "removeNotificationSendingModule")
protected void addNotificationSendingModule(NotificationSendingModule module) throws MessageRemovedException {
    ModuleConfiguration moduleConfiguration;
    if (StringUtils.isEmpty(module.getModuleName())) {
        if (log.isDebugEnabled()) {
            log.debug("Cannot register module without a valid module name");
        }
        return;
    }
    if (log.isDebugEnabled()) {
        log.debug("Registering a message sending module " + module.getModuleName());
    }
    if (configBuilder != null) {
        moduleConfiguration = configBuilder.getModuleConfigurations(module.getModuleName());
    } else {
        moduleConfiguration = new ModuleConfiguration();
    }
    try {
        module.init(moduleConfiguration);
        notificationSendingModules.add(module);
    } catch (NotificationManagementException e) {
        log.error("Error while initializing Notification sending module " + module.getModuleName(), e);
    }
}
 
Example #11
Source File: Log.java    From FairEmail with GNU General Public License v3.0 4 votes vote down vote up
static String formatThrowable(Throwable ex, String separator, boolean sanitize) {
    if (sanitize) {
        if (ex instanceof MessageRemovedException)
            return null;

        if (ex instanceof AuthenticationFailedException &&
                ex.getCause() instanceof SocketException)
            return null;

        if (ex instanceof MessagingException &&
                ("connection failure".equals(ex.getMessage()) ||
                        "failed to create new store connection".equals(ex.getMessage())))
            return null;

        if (ex instanceof MessagingException &&
                ex.getCause() instanceof ConnectionException &&
                ex.getCause().getMessage() != null &&
                (ex.getCause().getMessage().contains("Read error") ||
                        ex.getCause().getMessage().contains("Write error") ||
                        ex.getCause().getMessage().contains("Unexpected end of ZLIB input stream") ||
                        ex.getCause().getMessage().contains("Socket is closed")))
            return null;

        // javax.mail.MessagingException: AU3 BAD User is authenticated but not connected.;
        //   nested exception is:
        //  com.sun.mail.iap.BadCommandException: AU3 BAD User is authenticated but not connected.
        // javax.mail.MessagingException: AU3 BAD User is authenticated but not connected.;
        //   nested exception is:
        // 	com.sun.mail.iap.BadCommandException: AU3 BAD User is authenticated but not connected.
        // 	at com.sun.mail.imap.IMAPFolder.logoutAndThrow(SourceFile:1156)
        // 	at com.sun.mail.imap.IMAPFolder.open(SourceFile:1063)
        // 	at com.sun.mail.imap.IMAPFolder.open(SourceFile:977)
        // 	at eu.faircode.email.ServiceSynchronize.monitorAccount(SourceFile:890)
        // 	at eu.faircode.email.ServiceSynchronize.access$1500(SourceFile:85)
        // 	at eu.faircode.email.ServiceSynchronize$7$1.run(SourceFile:627)
        // 	at java.lang.Thread.run(Thread.java:764)
        // Caused by: com.sun.mail.iap.BadCommandException: AU3 BAD User is authenticated but not connected.
        // 	at com.sun.mail.iap.Protocol.handleResult(SourceFile:415)
        // 	at com.sun.mail.imap.protocol.IMAPProtocol.select(SourceFile:1230)
        // 	at com.sun.mail.imap.IMAPFolder.open(SourceFile:1034)

        if (ex instanceof MessagingException &&
                ex.getCause() instanceof BadCommandException &&
                ex.getCause().getMessage() != null &&
                ex.getCause().getMessage().contains("User is authenticated but not connected"))
            return null;

        if (ex instanceof IOException &&
                ex.getCause() instanceof MessageRemovedException)
            return null;

        if (ex instanceof ConnectionException)
            return null;

        if (ex instanceof StoreClosedException ||
                ex instanceof FolderClosedException || ex instanceof FolderClosedIOException)
            return null;

        if (ex instanceof IllegalStateException &&
                ("Not connected".equals(ex.getMessage()) ||
                        "This operation is not allowed on a closed folder".equals(ex.getMessage())))
            return null;
    }

    StringBuilder sb = new StringBuilder();
    if (BuildConfig.DEBUG)
        sb.append(ex.toString());
    else
        sb.append(ex.getMessage() == null ? ex.getClass().getName() : ex.getMessage());

    Throwable cause = ex.getCause();
    while (cause != null) {
        if (BuildConfig.DEBUG)
            sb.append(separator).append(cause.toString());
        else
            sb.append(separator).append(cause.getMessage() == null ? cause.getClass().getName() : cause.getMessage());
        cause = cause.getCause();
    }

    return sb.toString();
}
 
Example #12
Source File: EmailUtil.java    From product-es with Apache License 2.0 4 votes vote down vote up
/**
 * This method read verification e-mail from Gmail inbox and returns the verification URL.
 *
 * @return  verification redirection URL.
 * @throws  Exception
 */
public static String readGmailInboxForVerification() throws Exception {
    boolean isEmailVerified = false;
    long waitTime = 10000;
    String pointBrowserURL = "";
    Properties props = new Properties();
    props.load(new FileInputStream(new File(
            TestConfigurationProvider.getResourceLocation("GREG") + File.separator + "axis2" + File.separator
                    + "smtp.properties")));
    Session session = Session.getDefaultInstance(props, null);
    Store store = session.getStore("imaps");
    store.connect("smtp.gmail.com", emailAddress, java.nio.CharBuffer.wrap(emailPassword).toString());

    Folder inbox = store.getFolder("inbox");
    inbox.open(Folder.READ_WRITE);
    Thread.sleep(waitTime);
    long startTime = System.currentTimeMillis();
    long endTime = 0;
    int count = 1;
    while (endTime - startTime < 180000 && !isEmailVerified) {
        Message[] messages = inbox.getMessages();

        for (Message message : messages) {
            if (!message.isExpunged()) {
                try {
                    log.info("Mail Subject:- " + message.getSubject());
                    if (message.getSubject().contains("EmailVerification")) {
                        pointBrowserURL = getBodyFromMessage(message);
                        isEmailVerified = true;
                    }

                    // Optional : deleting the mail
                    message.setFlag(Flags.Flag.DELETED, true);
                } catch (MessageRemovedException e){
                    log.error("Could not read the message subject. Message is removed from inbox");
                }
            }
        }
        endTime = System.currentTimeMillis();
        Thread.sleep(waitTime);
        endTime += count*waitTime;
        count++;
    }
    inbox.close(true);
    store.close();
    return pointBrowserURL;
}
 
Example #13
Source File: EmailUtil.java    From product-es with Apache License 2.0 4 votes vote down vote up
/**
 * This method read e-mails from Gmail inbox and find whether the notification of particular type is found.
 *
 * @param   notificationType    Notification types supported by publisher and store.
 * @return  whether email is found for particular type.
 * @throws  Exception
 */
public static boolean readGmailInboxForNotification(String notificationType) throws Exception {
    boolean isNotificationMailAvailable = false;
    long waitTime = 10000;
    Properties props = new Properties();
    props.load(new FileInputStream(new File(
            TestConfigurationProvider.getResourceLocation("GREG") + File.separator + "axis2" + File.separator
                    + "smtp.properties")));
    Session session = Session.getDefaultInstance(props, null);
    Store store = session.getStore("imaps");
    store.connect("smtp.gmail.com", emailAddress, java.nio.CharBuffer.wrap(emailPassword).toString());

    Folder inbox = store.getFolder("inbox");
    inbox.open(Folder.READ_WRITE);
    Thread.sleep(waitTime);

    long startTime = System.currentTimeMillis();
    long endTime = 0;
    int count = 1;
    while (endTime - startTime < 180000 && !isNotificationMailAvailable) {
        Message[] messages = inbox.getMessages();

        for (Message message : messages) {
            if(!message.isExpunged()) {
                try {
                    log.info("Mail Subject:- " + message.getSubject());

                    if (message.getSubject().contains(notificationType)) {
                        isNotificationMailAvailable = true;

                    }
                    // Optional : deleting the  mail
                    message.setFlag(Flags.Flag.DELETED, true);
                } catch (MessageRemovedException e) {
                    log.error("Could not read the message subject. Message is removed from inbox");
                }
            }

        }
        endTime = System.currentTimeMillis();
        Thread.sleep(waitTime);
        endTime += count*waitTime;
        count++;
    }
    inbox.close(true);
    store.close();
    return isNotificationMailAvailable;
}
 
Example #14
Source File: EmailUtil.java    From product-es with Apache License 2.0 4 votes vote down vote up
/**
 * This method read verification e-mail from Gmail inbox and returns the verification URL.
 *
 * @return  verification redirection URL.
 * @throws  Exception
 */
public static String readGmailInboxForVerification() throws Exception {
    boolean isEmailVerified = false;
    long waitTime = 10000;
    String pointBrowserURL = "";
    Properties props = new Properties();
    props.load(new FileInputStream(new File(
            TestConfigurationProvider.getResourceLocation("GREG") + File.separator + "axis2" + File.separator
                    + "smtp.properties")));
    Session session = Session.getDefaultInstance(props, null);
    Store store = session.getStore("imaps");
    store.connect("smtp.gmail.com", emailAddress, java.nio.CharBuffer.wrap(emailPassword).toString());

    Folder inbox = store.getFolder("inbox");
    inbox.open(Folder.READ_WRITE);
    Thread.sleep(waitTime);
    long startTime = System.currentTimeMillis();
    long endTime = 0;
    int count = 1;
    while (endTime - startTime < 180000 && !isEmailVerified) {
        Message[] messages = inbox.getMessages();

        for (Message message : messages) {
            if (!message.isExpunged()) {
                try {
                    log.info("Mail Subject:- " + message.getSubject());
                    if (message.getSubject().contains("EmailVerification")) {
                        pointBrowserURL = getBodyFromMessage(message);
                        isEmailVerified = true;
                    }

                    // Optional : deleting the mail
                    message.setFlag(Flags.Flag.DELETED, true);
                } catch (MessageRemovedException e){
                    log.error("Could not read the message subject. Message is removed from inbox");
                }
            }
        }
        endTime = System.currentTimeMillis();
        Thread.sleep(waitTime);
        endTime += count*waitTime;
        count++;
    }
    inbox.close(true);
    store.close();
    return pointBrowserURL;
}
 
Example #15
Source File: EmailUtil.java    From product-es with Apache License 2.0 4 votes vote down vote up
/**
 * This method read e-mails from Gmail inbox and find whether the notification of particular type is found.
 *
 * @param   notificationType    Notification types supported by publisher and store.
 * @return  whether email is found for particular type.
 * @throws  Exception
 */
public static boolean readGmailInboxForNotification(String notificationType) throws Exception {
    boolean isNotificationMailAvailable = false;
    long waitTime = 10000;
    Properties props = new Properties();
    props.load(new FileInputStream(new File(
            TestConfigurationProvider.getResourceLocation("GREG") + File.separator + "axis2" + File.separator
                    + "smtp.properties")));
    Session session = Session.getDefaultInstance(props, null);
    Store store = session.getStore("imaps");
    store.connect("smtp.gmail.com", emailAddress, java.nio.CharBuffer.wrap(emailPassword).toString());

    Folder inbox = store.getFolder("inbox");
    inbox.open(Folder.READ_WRITE);
    Thread.sleep(waitTime);

    long startTime = System.currentTimeMillis();
    long endTime = 0;
    int count = 1;
    while (endTime - startTime < 180000 && !isNotificationMailAvailable) {
        Message[] messages = inbox.getMessages();

        for (Message message : messages) {
            if(!message.isExpunged()) {
                try {
                    log.info("Mail Subject:- " + message.getSubject());

                    if (message.getSubject().contains(notificationType)) {
                        isNotificationMailAvailable = true;

                    }
                    // Optional : deleting the  mail
                    message.setFlag(Flags.Flag.DELETED, true);
                } catch (MessageRemovedException e) {
                    log.error("Could not read the message subject. Message is removed from inbox");
                }
            }

        }
        endTime = System.currentTimeMillis();
        Thread.sleep(waitTime);
        endTime += count*waitTime;
        count++;
    }
    inbox.close(true);
    store.close();
    return isNotificationMailAvailable;
}