Example usage for javax.mail Folder appendMessages

List of usage examples for javax.mail Folder appendMessages

Introduction

In this page you can find the example usage for javax.mail Folder appendMessages.

Prototype

public abstract void appendMessages(Message[] msgs) throws MessagingException;

Source Link

Document

Append given Messages to this folder.

Usage

From source file:org.socraticgrid.displaymaildata.DisplayMailDataHandler.java

/**
 *
 * @param request/*from   w  ww. ja v  a  2s  .  c o  m*/
 *        where request.action = [ "Send" , "Update" ,"Read" , "Save" ]
 * @return
 */
private SetMessageResponseType sendImapSSLMail(SetMessageRequestType request) {

    //System.out.println("===> sendImapSSLMail: Action= " + request.getAction());

    SetMessageResponseType response = new SetMessageResponseType();
    IMAPSSLStore sslStore = null;
    Folder folder = null; //the email server folder the msg is CURRENTLY in.

    String userType = "";
    String[] access = new String[2];

    Properties prop = initializeMailProperties();
    Session session = Session.getDefaultInstance(prop);

    //Get email address and password from LDAP
    String userId = request.getUserId();
    ContactDTO foundContact = null;
    try {
        foundContact = findContactByUserId(userId);
    } catch (Exception e) {
        log.error("Contact record not found for userid: " + request.getUserId());
        response.setMessage("Contact record not found for userid: " + request.getUserId());
        response.setSuccessStatus(false);
        return response;
    }

    access = retrieveMailAccess(foundContact.getCommonName(), foundContact.getUid());

    if ((access[0] == null) || access[0].isEmpty()) {
        log.error("Contact record not found for user: " + userId);
        response.setMessage("Contact record not found for user: " + userId);
        response.setSuccessStatus(false);
        return response;
    }

    // Use the sslStore to send/change the message
    try {
        //action = Save
        if (request.getAction().equalsIgnoreCase("Save")) {
            response = saveDraft(request, access, sslStore,
                    this.mapKmrLocationToImapFolder("Drafts", this.host), session);
            response.setSuccessStatus(true);
            //return response;
        }
        //action = Send
        else if (request.getAction().equalsIgnoreCase("Send")) {
            // create and send msg to recipient(s).
            Message[] msgArr = createMessage(session, access[0], request);
            sendMessagesTOCCBCC(msgArr, request, session);

            // Store a copy to sender's Sent folder
            folder = getImapFolder(session, sslStore, access,
                    this.mapKmrLocationToImapFolder("Sent", this.host));
            folder.appendMessages(msgArr);

            response.setSuccessStatus(true);
        }
        //action = ..any other..
        else {
            folder = getImapFolder(session, sslStore, access,
                    this.mapKmrLocationToImapFolder(request.getLocation(), this.host));
            folder.open(Folder.READ_WRITE);

            //--------------------------------------------
            // Find the message by the given Message-ID
            //--------------------------------------------
            IMAPMessage imapMessage = (IMAPMessage) findMsgByMessageId(folder, request.getMessageId());

            //--------------------------------------------
            // Process the message
            //--------------------------------------------
            if (imapMessage != null) {
                System.out.println("===> sendImapSSLMail:Updating:   action=" + request.getAction());
                System.out.println("===> sendImapSSLMail:Updating:   folder=" + folder.getName());
                System.out.println("===> sendImapSSLMail:Updating:ImapMsgID=" + imapMessage.getMessageID());

                updateImapSSLMail(request, folder, imapMessage);

                System.out.println("===> sendImapSSLMail: Done Setting " + request.getAction() + " for msgId="
                        + request.getMessageId());

                response.setSuccessStatus(true);

            } else {
                String statMsg = "Msg NOT found for Message-ID=" + request.getMessageId();
                System.out.println("===> sendImapSSLMail: " + statMsg);

                response.setSuccessStatus(false);
                response.setMessage(statMsg);
            }

        }

    } catch (Exception e) {
        log.error(e.getMessage());
        response.setMessage("Error sending mail with Zimbra mail server: " + e.getMessage());
        response.setSuccessStatus(false);
        return response;
    } finally {
        if (folder != null && folder.isOpen()) {
            try {
                folder.close(false);
            } catch (MessagingException me) {
                log.error("Error closing folder");
            }
        }
        if (sslStore != null) {
            try {
                sslStore.close();
            } catch (MessagingException me) {
                log.error("Error closing SSLStore");
            }
        }
    }

    return response;

}

From source file:org.socraticgrid.displaymaildata.DisplayMailDataHandler.java

private SetMessageResponseType saveDraft(SetMessageRequestType request, String[] access, IMAPSSLStore sslStore,
        String destinationFolder, Session session) throws MessagingException {
    SetMessageResponseType response = new SetMessageResponseType();
    Folder folder = getImapFolder(session, sslStore, access, destinationFolder);
    folder.open(Folder.READ_WRITE);/*from   ww w .j  a  va2  s. c o  m*/
    IMAPMessage imapMessage = null;
    Message[] msgArr = null;
    try {
        msgArr = createMessage(session, access[0], request);
        folder.appendMessages(msgArr);
    } catch (Exception e) {
        log.error("Error creating draft message");
        response.setSuccessStatus(false);
        response.setMessage("Error saving draft message for user = " + request.getUserId());
        e.printStackTrace();
        return response;
    }
    response.setSuccessStatus(true);
    response.setMessage(" ");
    return response;
}

From source file:org.xwiki.contrib.mail.internal.AbstractMailStore.java

/**
 * @param folder//from  w  ww . j ava  2s.c  o  m
 * @param message
 * @throws MessagingException
 */
public void write(String folder, Message message) throws MessagingException {
    // getLogger().info("Delivering " + message + " to " + this.location + " / " + folder);

    final Store store = getJavamailStore(true);
    store.connect();
    final Folder mailFolder = store.getDefaultFolder().getFolder(folder);
    if (!mailFolder.exists()) {
        mailFolder.create(Folder.HOLDS_MESSAGES);
    }
    mailFolder.open(Folder.READ_WRITE);
    // If message is already archived, do nothing
    final Message existingMessage = read(folder, message.getHeader("Message-ID")[0]);
    if (existingMessage == null) {
        // The Store Provider may add some headers to the message to store, but IMAPMessage are read-only
        // So we clone the message before storing it
        final MimeMessage cloned = cloneEmail(message);
        mailFolder.appendMessages(new Message[] { cloned });
    }

    mailFolder.close(true);
    store.close();
}