Example usage for javax.mail.internet MimeMessage getHeader

List of usage examples for javax.mail.internet MimeMessage getHeader

Introduction

In this page you can find the example usage for javax.mail.internet MimeMessage getHeader.

Prototype

@Override
public String[] getHeader(String name) throws MessagingException 

Source Link

Document

Get all the headers for this header_name.

Usage

From source file:mitm.application.djigzo.james.mailets.PDFEncryptTest.java

@Test
public void testUserTemplateEncryptPDFNoTemplateProperty() throws Exception {
    MockMailetConfig mailetConfig = new MockMailetConfig("test");

    SendMailEventListenerImpl listener = new SendMailEventListenerImpl();

    mailetConfig.getMailetContext().setSendMailEventListener(listener);

    Mailet mailet = new PDFEncrypt();

    String template = FileUtils.readFileToString(new File("test/resources/templates/pdf-attachment.ftl"));

    autoTransactDelegator.setProperty("someOtherUser@EXAMPLE.com", "pdfTemplate", template);

    mailetConfig.setInitParameter("log", "starting");
    mailetConfig.setInitParameter("template", "encrypted-pdf.ftl");
    mailetConfig.setInitParameter("templateProperty", "pdfTemplate");
    mailetConfig.setInitParameter("encryptedProcessor", "encryptedProcessor");
    mailetConfig.setInitParameter("notEncryptedProcessor", "notEncryptedProcessor");
    mailetConfig.setInitParameter("passwordMode", "multiple");

    mailet.init(mailetConfig);/*from w ww .ja  va 2  s  .c o  m*/

    MockMail mail = new MockMail();

    mail.setState(Mail.DEFAULT);

    Passwords passwords = new Passwords();

    PasswordContainer container = new PasswordContainer("test1", "test ID");

    passwords.put("m.brinkers@pobox.com", container);

    container = new PasswordContainer("test2", "test ID 2");

    passwords.put("123@example.com", container);

    new DjigzoMailAttributesImpl(mail).setPasswords(passwords);

    MimeMessage message = MailUtils.loadMessage(new File(testBase, "mail/normal-message-with-attach.eml"));

    mail.setMessage(message);

    Collection<MailAddress> recipients = new LinkedList<MailAddress>();

    recipients.add(new MailAddress("m.bRINKERs@pobox.com"));
    recipients.add(new MailAddress("123@EXAMPLE.com"));

    mail.setRecipients(recipients);

    mail.setSender(new MailAddress("sender@example.com"));

    mailet.service(mail);

    MailUtils.validateMessage(mail.getMessage());

    TestUtils.saveMessages(tempDir, "testUserTemplateEncryptPDFNoTemplateProperty", listener.getMessages());

    assertEquals(2, listener.getMessages().size());
    assertEquals("encryptedProcessor", listener.getStates().get(0));
    assertEquals("encryptedProcessor", listener.getStates().get(1));
    assertEquals(1, listener.getRecipients().get(0).size());
    assertTrue(listener.getRecipients().get(0).contains(new MailAddress("m.bRINKERs@pobox.com")));
    assertEquals(1, listener.getRecipients().get(1).size());
    assertTrue(listener.getRecipients().get(1).contains(new MailAddress("123@EXAMPLE.com")));
    assertEquals("sender@example.com", listener.getSenders().get(0).toString());
    assertEquals("sender@example.com", listener.getSenders().get(1).toString());
    assertEquals(Mail.DEFAULT, mail.getState());

    MimeMessage message0 = listener.getMessages().get(0);
    MimeMessage message1 = listener.getMessages().get(1);

    assertNotNull(message0);
    assertNotNull(message1);

    assertNull(message0.getHeader("X-pdf-template-test"));
    assertNull(message1.getHeader("X-pdf-template-test"));

    MailUtils.validateMessage(listener.getMessages().get(0));
    MailUtils.validateMessage(listener.getMessages().get(1));
}

From source file:mitm.application.djigzo.james.mailets.PDFEncryptTest.java

@Test
public void testUserTemplateEncryptPDF() throws Exception {
    MockMailetConfig mailetConfig = new MockMailetConfig("test");

    SendMailEventListenerImpl listener = new SendMailEventListenerImpl();

    mailetConfig.getMailetContext().setSendMailEventListener(listener);

    Mailet mailet = new PDFEncrypt();

    String template = FileUtils.readFileToString(new File("test/resources/templates/pdf-attachment.ftl"));

    autoTransactDelegator.setProperty("test@EXAMPLE.com", "pdfTemplate", template);

    mailetConfig.setInitParameter("log", "starting");
    mailetConfig.setInitParameter("template", "encrypted-pdf.ftl");
    mailetConfig.setInitParameter("templateProperty", "pdfTemplate");
    mailetConfig.setInitParameter("encryptedProcessor", "encryptedProcessor");
    mailetConfig.setInitParameter("notEncryptedProcessor", "notEncryptedProcessor");
    mailetConfig.setInitParameter("passwordMode", "multiple");

    mailet.init(mailetConfig);/*  ww  w  . ja va2 s  .  c om*/

    MockMail mail = new MockMail();

    mail.setState(Mail.DEFAULT);

    Passwords passwords = new Passwords();

    PasswordContainer container = new PasswordContainer("test1", "test ID");

    passwords.put("m.brinkers@pobox.com", container);

    container = new PasswordContainer("test2", "test ID 2");

    passwords.put("123@example.com", container);

    new DjigzoMailAttributesImpl(mail).setPasswords(passwords);

    MimeMessage message = MailUtils.loadMessage(new File(testBase, "mail/normal-message-with-attach.eml"));

    mail.setMessage(message);

    Collection<MailAddress> recipients = new LinkedList<MailAddress>();

    recipients.add(new MailAddress("m.bRINKERs@pobox.com"));
    recipients.add(new MailAddress("123@EXAMPLE.com"));

    mail.setRecipients(recipients);

    mail.setSender(new MailAddress("sender@example.com"));

    mailet.service(mail);

    MailUtils.validateMessage(mail.getMessage());

    TestUtils.saveMessages(tempDir, "testUserTemplateEncryptPDF", listener.getMessages());

    assertEquals(2, listener.getMessages().size());
    assertEquals("encryptedProcessor", listener.getStates().get(0));
    assertEquals("encryptedProcessor", listener.getStates().get(1));
    assertEquals(1, listener.getRecipients().get(0).size());
    assertTrue(listener.getRecipients().get(0).contains(new MailAddress("m.bRINKERs@pobox.com")));
    assertEquals(1, listener.getRecipients().get(1).size());
    assertTrue(listener.getRecipients().get(1).contains(new MailAddress("123@EXAMPLE.com")));
    assertEquals("sender@example.com", listener.getSenders().get(0).toString());
    assertEquals("sender@example.com", listener.getSenders().get(1).toString());
    assertEquals(Mail.DEFAULT, mail.getState());

    MimeMessage message0 = listener.getMessages().get(0);
    MimeMessage message1 = listener.getMessages().get(1);

    assertNotNull(message0);
    assertNotNull(message1);

    assertEquals("test", StringUtils.join(message0.getHeader("X-pdf-template-test")));
    assertEquals("test", StringUtils.join(message1.getHeader("X-pdf-template-test")));

    MailUtils.validateMessage(listener.getMessages().get(0));
    MailUtils.validateMessage(listener.getMessages().get(1));
}

From source file:mitm.application.djigzo.james.mailets.PDFEncryptTest.java

@Test
public void testUserTemplateEncryptPDFSetPassThroughProcessor() throws Exception {
    MockMailetConfig mailetConfig = new MockMailetConfig("test");

    SendMailEventListenerImpl listener = new SendMailEventListenerImpl();

    mailetConfig.getMailetContext().setSendMailEventListener(listener);

    Mailet mailet = new PDFEncrypt();

    String template = FileUtils.readFileToString(new File("test/resources/templates/pdf-attachment.ftl"));

    autoTransactDelegator.setProperty("test@EXAMPLE.com", "pdfTemplate", template);

    mailetConfig.setInitParameter("log", "starting");
    mailetConfig.setInitParameter("template", "encrypted-pdf.ftl");
    mailetConfig.setInitParameter("templateProperty", "pdfTemplate");
    mailetConfig.setInitParameter("encryptedProcessor", "encryptedProcessor");
    mailetConfig.setInitParameter("notEncryptedProcessor", "notEncryptedProcessor");
    mailetConfig.setInitParameter("passwordMode", "multiple");
    mailetConfig.setInitParameter("passThroughProcessor", "newState");

    mailet.init(mailetConfig);/*from   www  .  java 2 s  . c  om*/

    MockMail mail = new MockMail();

    mail.setState(Mail.DEFAULT);

    Passwords passwords = new Passwords();

    PasswordContainer container = new PasswordContainer("pass1", "id1");

    passwords.put("m.brinkers@pobox.com", container);

    container = new PasswordContainer("pass2", "id2");

    passwords.put("123@example.com", container);

    new DjigzoMailAttributesImpl(mail).setPasswords(passwords);

    MimeMessage message = MailUtils.loadMessage(new File(testBase, "mail/normal-message-with-attach.eml"));

    mail.setMessage(message);

    Collection<MailAddress> recipients = new LinkedList<MailAddress>();

    recipients.add(new MailAddress("m.bRINKERs@pobox.com"));
    recipients.add(new MailAddress("123@EXAMPLE.com"));

    mail.setRecipients(recipients);

    mail.setSender(new MailAddress("sender@example.com"));

    mailet.service(mail);

    MailUtils.validateMessage(mail.getMessage());

    TestUtils.saveMessages(tempDir, "testUserTemplateEncryptPDF", listener.getMessages());

    assertEquals(2, listener.getMessages().size());
    assertEquals("encryptedProcessor", listener.getStates().get(0));
    assertEquals("encryptedProcessor", listener.getStates().get(1));
    assertEquals(1, listener.getRecipients().get(0).size());
    assertTrue(listener.getRecipients().get(0).contains(new MailAddress("m.bRINKERs@pobox.com")));
    assertEquals(1, listener.getRecipients().get(1).size());
    assertTrue(listener.getRecipients().get(1).contains(new MailAddress("123@EXAMPLE.com")));
    assertEquals("sender@example.com", listener.getSenders().get(0).toString());
    assertEquals("sender@example.com", listener.getSenders().get(1).toString());
    assertEquals("newState", mail.getState());

    MimeMessage message0 = listener.getMessages().get(0);
    MimeMessage message1 = listener.getMessages().get(1);

    assertNotNull(message0);
    assertNotNull(message1);

    assertEquals("test", StringUtils.join(message0.getHeader("X-pdf-template-test")));
    assertEquals("test", StringUtils.join(message1.getHeader("X-pdf-template-test")));

    MailUtils.validateMessage(message0);
    MailUtils.validateMessage(message1);

    checkEncryption(message0, "pass1", false);
    checkEncryption(message1, "pass2", false);
}

From source file:mitm.application.djigzo.james.mailets.PDFEncryptTest.java

@Test
public void testReply() throws Exception {
    MockMailetConfig mailetConfig = new MockMailetConfig("test");

    SendMailEventListenerImpl listener = new SendMailEventListenerImpl();

    mailetConfig.getMailetContext().setSendMailEventListener(listener);

    Mailet mailet = new PDFEncrypt();

    String template = FileUtils.readFileToString(new File("test/resources/templates/pdf-attachment.ftl"));

    autoTransactDelegator.setProperty("test@EXAMPLE.com", "pdfTemplate", template);

    autoTransactDelegator.setProperty("test@example.com", "user.serverSecret", "123", true /* encrypt */);
    autoTransactDelegator.setProperty("test@example.com", "user.pdf.replyAllowed", "true");
    autoTransactDelegator.setProperty("test@example.com", "user.pdf.replyURL", "http://127.0.0.1");

    autoTransactDelegator.setProperty("m.brinkers@pobox.com", "user.pdf.replyAllowed", "true");

    mailetConfig.setInitParameter("log", "starting");
    mailetConfig.setInitParameter("template", "encrypted-pdf.ftl");
    mailetConfig.setInitParameter("templateProperty", "pdfTemplate");
    mailetConfig.setInitParameter("encryptedProcessor", "encryptedProcessor");
    mailetConfig.setInitParameter("notEncryptedProcessor", "notEncryptedProcessor");
    mailetConfig.setInitParameter("passwordMode", "multiple");
    mailetConfig.setInitParameter("ownerPasswordMode", "random");
    mailetConfig.setInitParameter("openPermission", "allowPrinting");
    mailetConfig.setInitParameter("openPermission", "allowAssembly");
    mailetConfig.setInitParameter("viewerPreference", "centerWindow");
    mailetConfig.setInitParameter("viewerPreference", "fitWindow");

    mailet.init(mailetConfig);/*from   www.j a v  a  2 s.c  om*/

    MockMail mail = new MockMail();

    mail.setState(Mail.DEFAULT);

    Passwords passwords = new Passwords();

    PasswordContainer container = new PasswordContainer("test1", "test ID");

    passwords.put("m.brinkers@pobox.com", container);

    container = new PasswordContainer("test2", "test ID 2");

    passwords.put("123@example.com", container);

    new DjigzoMailAttributesImpl(mail).setPasswords(passwords);

    MimeMessage message = MailUtils.loadMessage(new File(testBase, "mail/normal-message-with-attach.eml"));

    mail.setMessage(message);

    Collection<MailAddress> recipients = new LinkedList<MailAddress>();

    recipients.add(new MailAddress("m.bRINKERs@pobox.com"));
    recipients.add(new MailAddress("123@EXAMPLE.com"));

    mail.setRecipients(recipients);

    mail.setSender(new MailAddress("sender@example.com"));

    mailet.service(mail);

    MailUtils.validateMessage(mail.getMessage());

    TestUtils.saveMessages(tempDir, "testUserTemplateEncryptPDF", listener.getMessages());

    assertEquals(2, listener.getMessages().size());
    assertEquals("encryptedProcessor", listener.getStates().get(0));
    assertEquals("encryptedProcessor", listener.getStates().get(1));
    assertEquals(1, listener.getRecipients().get(0).size());
    assertTrue(listener.getRecipients().get(0).contains(new MailAddress("m.bRINKERs@pobox.com")));
    assertEquals(1, listener.getRecipients().get(1).size());
    assertTrue(listener.getRecipients().get(1).contains(new MailAddress("123@EXAMPLE.com")));
    assertEquals("sender@example.com", listener.getSenders().get(0).toString());
    assertEquals("sender@example.com", listener.getSenders().get(1).toString());
    assertEquals(Mail.DEFAULT, mail.getState());

    MimeMessage message0 = listener.getMessages().get(0);
    MimeMessage message1 = listener.getMessages().get(1);

    assertNotNull(message0);
    assertNotNull(message1);

    assertEquals("test", StringUtils.join(message0.getHeader("X-pdf-template-test")));
    assertEquals("test", StringUtils.join(message1.getHeader("X-pdf-template-test")));

    MailUtils.validateMessage(message0);
    MailUtils.validateMessage(message1);

    checkEncryption(message0, "test1", true);
    checkEncryption(message1, "test2", false);
}

From source file:davmail.imap.ImapConnection.java

protected void appendEnvelope(StringBuilder buffer, ExchangeSession.Message message) throws IOException {
    buffer.append(" ENVELOPE (");

    try {//from w ww. j  a  v a  2s. co m
        MimeMessage mimeMessage = message.getMimeMessage();
        // Envelope for date, subject, from, sender, reply-to, to, cc, bcc,in-reply-to, message-id
        appendEnvelopeHeader(buffer, mimeMessage.getHeader("Date"));
        appendEnvelopeHeader(buffer, mimeMessage.getHeader("Subject"));
        appendMailEnvelopeHeader(buffer, mimeMessage.getHeader("From"));
        appendMailEnvelopeHeader(buffer, mimeMessage.getHeader("Sender"));
        appendMailEnvelopeHeader(buffer, mimeMessage.getHeader("Reply-To"));
        appendMailEnvelopeHeader(buffer, mimeMessage.getHeader("To"));
        appendMailEnvelopeHeader(buffer, mimeMessage.getHeader("CC"));
        appendMailEnvelopeHeader(buffer, mimeMessage.getHeader("BCC"));
        appendEnvelopeHeader(buffer, mimeMessage.getHeader("In-Reply-To"));
        appendEnvelopeHeader(buffer, mimeMessage.getHeader("Message-Id"));

    } catch (MessagingException me) {
        DavGatewayTray.warn(me);
        // send fake envelope
        buffer.append(" NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL");
    }
    buffer.append(')');
}

From source file:mitm.common.security.smime.SMIMEBuilderImplTest.java

@Test
public void testEncryptReceivedHeadersOrder() throws Exception {
    MimeMessage message = loadMessage("text-message-with-received-headers.eml");

    SMIMEBuilder builder = new SMIMEBuilderImpl(message);

    builder.addRecipient(encryptionCertificate, SMIMERecipientMode.ISSUER_SERIAL);

    builder.encrypt(SMIMEEncryptionAlgorithm.DES_EDE3_CBC);

    MimeMessage newMessage = builder.buildMessage();

    File file = new File(tempDir, "text-message-with-received-headers-encrypted.eml");

    FileOutputStream output = new FileOutputStream(file);

    MailUtils.writeMessage(newMessage, output);

    String[] returnPaths = newMessage.getHeader("Return-Path");

    assertEquals(2, returnPaths.length);
    assertEquals("<return1@example.com>", returnPaths[0]);
    assertEquals("<return2@example.com>", returnPaths[1]);

    String[] received = newMessage.getHeader("Received");
    assertEquals(7, received.length);/* ww  w .j  a v a  2  s.c o m*/
    assertEquals("from secure.example.com (unknown [192.168.0.6])\r\n\tby example.com (Postfix) "
            + "with ESMTP id 0183D43843\r\n\tfor <martijn@example.com>; Sat, 22 Aug 2009 18:30:27 +0200 (CEST)",
            received[0]);
    assertEquals(
            "from test (desktop.box [192.168.178.20])\r\n\tby host.example.com (Postfix) with "
                    + "SMTP id 9883623F5\r\n\tfor <martijn@example.com>; Sat, 22 Aug 2009 12:30:24 -0400 (EDT)",
            received[6]);
}

From source file:davmail.exchange.ExchangeSession.java

protected void convertResentHeader(MimeMessage mimeMessage, String headerName) throws MessagingException {
    String[] resentHeader = mimeMessage.getHeader("Resent-" + headerName);
    if (resentHeader != null) {
        mimeMessage.removeHeader("Resent-" + headerName);
        mimeMessage.removeHeader(headerName);
        for (String value : resentHeader) {
            mimeMessage.addHeader(headerName, value);
        }/*from   ww  w .j  a  v a  2  s.c  om*/
    }
}

From source file:edu.stanford.muse.email.EmailFetcherStats.java

/**
 * fetch given message idx's in given folder -- @performance critical
 *
 * @param offset - the original offset of the first message in the messages array, important to initialize
 *               for proper assignment of unique id or doc Id
 *///from w w w . j av  a 2  s .c o m
//private void fetchUncachedMessages(String sanitizedFName, Folder folder, DocCache cache, List<Integer> msgIdxs) throws MessagingException, FileNotFoundException, IOException, GeneralSecurityException {
private void fetchAndIndexMessages(Folder folder, Message[] messages, int offset, int totalMessages)
        throws MessagingException, IOException, GeneralSecurityException {
    //mark the processing of new batch
    if (offset == 0)
        fetchStartTime = System.currentTimeMillis();

    currentStatus = JSONUtils.getStatusJSON(
            (emailStore instanceof MboxEmailStore) ? "Parsing " + folder.getName() + " (can take a while)..."
                    : "Reading " + folder.getName() + "...");

    // bulk fetch of all message headers
    int n = messages.length;

    // eliminate any messages the archive already has
    messages = removeMessagesAlreadyInArchive(archive, messages);

    log.info(n - messages.length + " message(s) already in the archive");

    ArrayList<EmailDocument> emails = new ArrayList<EmailDocument>();

    // for performance, we need to do bulk prefetches, instead of fetching 1 message at a time
    // prefetchedMessages will be a temp cache of prefetched messages
    int first_i_prefetched = -1, last_i_prefetched = -1;
    List<?> prefetchedMessages = null; // the type of this can be either list<string> if text only, otherwise list<mimemmessage>

    long highestUID = archive.getLastUIDForFolder(fetchedFolderInfo.accountKey, fetchedFolderInfo.longName);
    long lastAssignedUID = highestUID;
    boolean bodyTextOnly = !fetchConfig.downloadAttachments;
    try {
        archive.openForWrite();
        for (int i = 0; i < messages.length; i++) {
            // critical step: (thanks, yourkit!)
            // null out the ref to the previous message, otherwise it stays in memory, and the heap effectively needs to be as big as the size of all messages
            if (i > 0)
                messages[i - 1] = null;

            if (isCancelled)
                break;

            Message m = messages[i];
            MimeMessage mm = (MimeMessage) m;

            if (i >= last_i_prefetched) {
                // critical perf. step: do a bulk imap prefetch
                // the prefetch will fetch as many messages as possible up to a max buffer size, and return the messages prefetched
                // last_i_prefetched tracks what is the last index into idxs that we have prefetched.
                // when we run out of prefetched messages, we do another bulk prefetch

                prefetchedMessages = do_imap_prefetch(messages, i, folder, bodyTextOnly);
                if (prefetchedMessages != null) {
                    first_i_prefetched = i;
                    last_i_prefetched = i + prefetchedMessages.size();
                }
            }

            int pctDone = ((i + offset) * 100) / totalMessages;
            long elapsedMillis = System.currentTimeMillis() - fetchStartTime;
            long unprocessedSecs = Util.getUnprocessedMessage(i + offset, totalMessages, elapsedMillis);
            int N_TEASERS = 50; // 50 ok here, because it takes a long time to fetch and process messages, so teaser computation is relatively not expensive
            int nTriesForThisMessage = 0;
            currentStatus = getStatusJSONWithTeasers(
                    "Reading " + Util.commatize(totalMessages) + " messages from " + folder.getName() + "...",
                    pctDone, elapsedMillis / 1000, unprocessedSecs, emails, N_TEASERS);

            int messageNum = mm.getMessageNumber();

            try {
                long unique_id;

                // if we have uid, that's even better
                // don't use uid's for mbox, it has a bug and always gives -1
                // see http://james.apache.org/server/rfclist/imap4/rfc2060.txt for uid spec
                if (folder instanceof UIDFolder && !(emailStore instanceof MboxEmailStore)) {
                    long uid = ((UIDFolder) folder).getUID(m);
                    unique_id = uid;
                } else
                    unique_id = lastAssignedUID + 1 + i + offset; // +1 since i starts from 0 (but lastAssignedUID can be -1 -- is that safe? -sgh)

                if (unique_id > highestUID)
                    highestUID = unique_id;

                String unique_id_as_string = Long.toString(unique_id);

                // well, we already converted to emaildoc above during removeMessagesAlreadyInArchive
                // not a serious perf. concern now, but revisit if needed
                EmailDocument ed = convertToEmailDocument(mm, unique_id_as_string); // this messageNum is mostly for debugging, it should not be used for equals etc.
                // need to check this again, because there might be duplicates such within the set we are currently processing.
                if (archive.containsDoc(ed)) {
                    stats.nMessagesAlreadyPresent++;
                    dataErrors.add("Duplicate message: " + ed); // note: report.jsp depends on this specific string
                    continue;
                }

                MimeMessage originalMessage = mm; // this is the mm that has all the headers etc.
                List<Blob> attachmentsList = new ArrayList<Blob>();

                // if we already have it prefetched, use the prefetched version
                List<String> contents = null;

                if (first_i_prefetched >= 0 && prefetchedMessages != null) {
                    if (!fetchConfig.downloadAttachments) {
                        // text only means the prefetchedMessages are stored directly as a list of strings
                        String content = (String) prefetchedMessages.get(i - first_i_prefetched); // note: this_mm only has the prefetched content, but not the headers
                        contents = new ArrayList<String>();

                        try {
                            // a special for yahoo which routinely uses quoted-printable. content looks like  =0A0D.... = etc.
                            if (mm.isMimeType("multipart/alternative")) {
                                Multipart mm_mp = (Multipart) mm.getContent();
                                Part p0 = mm_mp.getBodyPart(0);
                                if (p0 instanceof com.sun.mail.imap.IMAPBodyPart) {
                                    String encoding = ((com.sun.mail.imap.IMAPBodyPart) p0).getEncoding();
                                    if ("quoted-printable".equals(encoding)) {
                                        content = new String(
                                                Util.getBytesFromStream(javax.mail.internet.MimeUtility.decode(
                                                        new java.io.ByteArrayInputStream(content.getBytes()),
                                                        "quoted-printable")));
                                    }
                                }
                            }
                        } catch (Exception e) {
                            Util.print_exception("Error trying to parse encoding of multipart", e, log);
                        }

                        contents.add(content);
                    } else {
                        // subtle issue here: the contentType of the prefetchedMessage needs to be be set to the original_mm's content-type.
                        // this was found for cases where the original message is multipart-alternative with a text and html part.
                        // if we don't set prefetchedMessage's content type, it gets a mime type of text/plain and a body = the entire multipart including both parts.
                        // found on sgh's sent mail w/subject: "text to add in help" from  Fri, 7 Jun 2013
                        MimeMessage prefetchedMessage = (MimeMessage) prefetchedMessages
                                .get(i - first_i_prefetched);
                        String contentTypeHeaders[] = originalMessage.getHeader("Content-Type");
                        String contentTypeHeader = null;
                        if (contentTypeHeaders != null && contentTypeHeaders.length == 1)
                            contentTypeHeader = contentTypeHeaders[0];

                        if (!Util.nullOrEmpty(contentTypeHeader)) // we do care about body structure, hang on to it
                            prefetchedMessage.setHeader("Content-Type", contentTypeHeader);
                        mm = prefetchedMessage;
                    }
                    prefetchedMessages.set(i - first_i_prefetched, null); // null out to save memory
                }

                if (contents == null)
                    contents = processMessagePart(messageNum, originalMessage, mm, attachmentsList);

                // if mm is not prefetched, it is the same as original_mm
                // will also work, but will be slow as javamail accesses and fetches each mm separately, instead of using the bulk prefetched version
                // even when prefetched, the processMessagePart is somewhat expensive because the attachments have to be extracted etc.

                // we could overlap processMessagePart with do_imap_prefetch by prefetching in a separate thread, since prefetch is network limited.
                // but profiling shows processMessagePart takes only 1/4th the time of do_imap_prefetch so overlapping would be a relatively small gain.
                // not worth the effort right now.
                ed.attachments = attachmentsList;
                if (fetchConfig.downloadAttachments)
                    ed.attachmentsYetToBeDownloaded = false; // we've already downloaded our attachments

                // concat all the contents parts
                StringBuilder sb = new StringBuilder();
                for (String s : contents) {
                    sb.append(s);
                    sb.append("\n");
                }

                String contentStr = sb.toString();
                if (!messageLooksOk(contentStr)) {
                    dataErrors.add("Skipping message as it seems to have very long words: " + ed);
                    continue;
                }

                if (contentStr.length() > Config.MAX_TEXT_SIZE_TO_ANNOTATE) {
                    dataErrors.add("Skipping message as it seems to be very long: " + contentStr.length()
                            + " chars, while the max size message that will be annotated for display is "
                            + Config.MAX_TEXT_SIZE_TO_ANNOTATE + " chars. Message = " + ed);
                    // but we continue, don't skip the message entirely. See issue #111
                }

                contentStr = IndexUtils.normalizeNewlines(contentStr); // just get rid of \r's

                archive.addDoc(ed, contentStr);

                List<LinkInfo> linkList = new ArrayList<LinkInfo>();
                // linkList might be used only for slant
                IndexUtils.populateDocLinks(ed, contentStr, linkList, true);
                ed.links = linkList;
                stats.nMessagesAdded++;
            } catch (Exception ex) {
                // sometimes we get unexpected folder closed, so try again
                boolean retry = false;
                if (ex instanceof javax.mail.FolderClosedException) {
                    log.warn("Oops, thread " + threadID + " got the folder closed in its face! "
                            + ex.getMessage());

                    // sometimes we get this exception about folder closed
                    // retry up to 3 times, then give up
                    if (nTriesForThisMessage < 3) {
                        retry = true;
                        log.info("Re-opening email store; attempt #" + (nTriesForThisMessage + 1)
                                + " for message " + i);
                        nTriesForThisMessage++;
                        messages = openFolderAndGetMessages();
                        fetchHeaders(messages);
                        --i; // adjust the message index n try again
                    }
                }

                if (!retry) {
                    // we sometimes see UnsupportedEncodingException with x-utf8utf8 mime type and ParseException
                    // nothing much can be done, just create a dummy doc and add it to the cache
                    nErrors++;
                    stats.nErrors++;
                    EmailDocument ed = new EmailDocument(Integer.toString(messageNum));
                    log.warn("Exception reading message from " + folder_name() + " Message #" + messageNum + " "
                            + ex.getMessage() + "\n" + Util.stackTrace(ex));

                    ed.setErrorString(Util.stackTrace(ex));
                }
            }
        }
    } catch (Throwable t) {
        Util.print_exception(t, log);
    } finally {
        //            if (cancelled && false) // TODO: disable for now as currently only indexes are rolled back and allDocs/blobs are not rolled back in sync yet
        //               archive.rollbackIndexWrites();
        //            else
        currentStatus = JSONUtils.getStatusJSON("Saving archive...");
        archive.close();
    }

    fetchedFolderInfo.lastSeenUID = highestUID;
    log.info("at end of fetch, folder info is " + fetchedFolderInfo);

    log.info("emailfetcher thread completed, archive has " + archive.getAllDocs().size() + " docs");
}

From source file:com.zimbra.cs.service.mail.ToXML.java

/** Encodes an Invite stored within a calendar item object into <m> element
 *  with <mp> elements./*from   w ww. j a  v a  2s  . com*/
 * @param parent  The Element to add the new <tt>&lt;m></tt> to.
 * @param ifmt    The SOAP request's context.
 * @param calItem The calendar item to serialize.
 * @param iid     The requested item; the contained subpart will be used to
 *                pick the Invite out of the calendar item's blob & metadata.
 * @param part    If non-null, we'll serialuize this message/rfc822 subpart
 *                of the specified Message instead of the Message itself.
 * @param maxSize The maximum amount of content to inline (<=0 is unlimited).
 * @param wantHTML  <tt>true</tt> to prefer HTML parts as the "body",
 *                  <tt>false</tt> to prefer text/plain parts.
 * @param neuter  Whether to rename "src" attributes on HTML <img> tags.
 * @param headers Extra message headers to include in the returned element.
 * @param serializeType If <tt>false</tt>, always serializes as an
 *                      <tt>&lt;m></tt> element.
 * @return The newly-created <tt>&lt;m></tt> Element, which has already
 *         been added as a child to the passed-in <tt>parent</tt>.
 * @throws ServiceException */
public static Element encodeInviteAsMP(Element parent, ItemIdFormatter ifmt, OperationContext octxt,
        CalendarItem calItem, String recurIdZ, ItemId iid, String part, int maxSize, boolean wantHTML,
        boolean neuter, Set<String> headers, boolean serializeType, boolean wantExpandGroupInfo)
        throws ServiceException {
    int invId = iid.getSubpartId();
    Invite[] invites = calItem.getInvites(invId);
    boolean isPublic = calItem.isPublic();
    boolean showAll = isPublic || allowPrivateAccess(octxt, calItem);

    boolean wholeMessage = (part == null || part.trim().isEmpty());

    Element m;
    if (wholeMessage) {
        // We want to return the MODIFIED_CONFLICT fields to enable conflict detection on modify.
        int fields = NOTIFY_FIELDS | Change.CONFLICT;
        m = encodeMessageCommon(parent, ifmt, octxt, calItem, fields, serializeType);
        m.addAttribute(MailConstants.A_ID, ifmt.formatItemId(calItem, invId));
    } else {
        m = parent.addElement(MailConstants.E_MSG);
        m.addAttribute(MailConstants.A_ID, ifmt.formatItemId(calItem, invId));
        m.addAttribute(MailConstants.A_PART, part);
    }

    try {
        MimeMessage mm = calItem.getSubpartMessage(invId);
        if (mm != null) {
            if (!wholeMessage) {
                MimePart mp = Mime.getMimePart(mm, part);
                if (mp == null) {
                    throw MailServiceException.NO_SUCH_PART(part);
                }
                Object content = Mime.getMessageContent(mp);
                if (!(content instanceof MimeMessage)) {
                    throw MailServiceException.NO_SUCH_PART(part);
                }
                mm = (MimeMessage) content;
            } else {
                part = "";
            }
            if (showAll) {
                addEmails(m, Mime.parseAddressHeader(mm, "From"), EmailType.FROM);
                addEmails(m, Mime.parseAddressHeader(mm, "Sender"), EmailType.SENDER);
                addEmails(m, Mime.parseAddressHeader(mm, "Reply-To"), EmailType.REPLY_TO);
                addEmails(m, Mime.parseAddressHeader(mm, "To"), EmailType.TO);
                addEmails(m, Mime.parseAddressHeader(mm, "Cc"), EmailType.CC);
                addEmails(m, Mime.parseAddressHeader(mm, "Bcc"), EmailType.BCC);

                String subject = Mime.getSubject(mm);
                if (subject != null) {
                    m.addAttribute(MailConstants.E_SUBJECT, StringUtil.stripControlCharacters(subject),
                            Element.Disposition.CONTENT);
                }
                String messageID = mm.getMessageID();
                if (messageID != null && !messageID.trim().isEmpty()) {
                    m.addAttribute(MailConstants.E_MSG_ID_HDR, StringUtil.stripControlCharacters(messageID),
                            Element.Disposition.CONTENT);
                }
                if (!wholeMessage) {
                    m.addAttribute(MailConstants.A_SIZE, mm.getSize());
                }
                java.util.Date sent = mm.getSentDate();
                if (sent != null) {
                    m.addAttribute(MailConstants.A_SENT_DATE, sent.getTime());
                }
            }
        }

        Element invElt = m.addElement(MailConstants.E_INVITE);
        setCalendarItemType(invElt, calItem.getType());
        encodeTimeZoneMap(invElt, calItem.getTimeZoneMap());
        if (invites.length > 0) {
            if (showAll) {
                encodeCalendarReplies(invElt, calItem, invites[0], recurIdZ);
            }
            for (Invite inv : invites) {
                encodeInviteComponent(invElt, ifmt, octxt, calItem, (ItemId) null, inv, NOTIFY_FIELDS, neuter);
            }
        }

        //encodeAlarmTimes(invElt, calItem);

        if (mm != null && showAll) {
            if (headers != null) {
                for (String name : headers) {
                    String[] values = mm.getHeader(name);
                    if (values == null) {
                        continue;
                    }
                    for (int i = 0; i < values.length; i++) {
                        m.addKeyValuePair(name, values[i], MailConstants.A_HEADER,
                                MailConstants.A_ATTRIBUTE_NAME);
                    }
                }
            }

            List<MPartInfo> parts = Mime.getParts(mm, getDefaultCharset(calItem));
            if (parts != null && !parts.isEmpty()) {
                Set<MPartInfo> bodies = Mime.getBody(parts, wantHTML);
                addParts(m, parts.get(0), bodies, part, maxSize, neuter, true, getDefaultCharset(calItem),
                        true);
            }
        }

        if (wantExpandGroupInfo) {
            Account authedAcct = octxt.getAuthenticatedUser();
            Account requestedAcct = calItem.getMailbox().getAccount();
            encodeAddrsWithGroupInfo(m, requestedAcct, authedAcct);
        }
    } catch (IOException ex) {
        throw ServiceException.FAILURE(ex.getMessage(), ex);
    } catch (MessagingException ex) {
        throw ServiceException.FAILURE(ex.getMessage(), ex);
    }
    return m;
}

From source file:edu.stanford.muse.email.EmailFetcherStats.java

/**
 * Key method for importing email: converts a javamail obj. to our own data structure (EmailDocument)
 *//*w  ww  . java  2 s.co m*/
//public EmailDocument convertToEmailDocument(MimeMessage m, int num, String url) throws MessagingException, IOException
private EmailDocument convertToEmailDocument(MimeMessage m, String id) throws MessagingException, IOException {
    // get the date.
    // prevDate is a hack for the cases where the message is lacking an explicit Date: header. e.g.
    //      From hangal Sun Jun 10 13:46:46 2001
    //      To: ewatkins@stanford.edu
    //      Subject: Re: return value bugs
    // though the date is on the From separator line, the mbox provider fails to parse it and provide it to us.
    // so as a hack, we will assign such messages the same date as the previous one this fetcher has seen! ;-)
    // update: having the exact same date causes the message to be considered a duplicate, so just increment
    // the timestamp it by 1 millisecond!
    // a better fix would be to improve the parsing in the provider

    boolean hackyDate = false;
    Date d = m.getSentDate();
    if (d == null)
        d = m.getReceivedDate();
    if (d == null) {
        if (prevDate != null) {
            long newTime = prevDate.getTime() + 1L; // added +1 so that this email is not considered the same object as the prev. one if they are in the same thread
            d = new Date(newTime);
            dataErrors.add("No date for message id:" + id + ": " + EmailUtils.formatMessageHeader(m)
                    + " assigned approximate date");
        } else {
            d = INVALID_DATE; // wrong, but what can we do... :-(
            dataErrors.add("No date for message id:" + id + ": " + EmailUtils.formatMessageHeader(m)
                    + " assigned deliberately invalid date");
        }
        hackyDate = true;
    } else {
        Calendar c = new GregorianCalendar();
        c.setTime(d);
        int yy = c.get(Calendar.YEAR);
        if (yy < 1960 || yy > 2020) {
            dataErrors.add("Probably bad date: " + Util.formatDate(c) + " message: "
                    + EmailUtils.formatMessageHeader(m));
            hackyDate = true;
        }
    }

    if (hackyDate && prevDate != null) {
        long newTime = prevDate.getTime() + 1L; // added +1 so that this email is not considered the same object as the prev. one if they are in the same thread
        d = new Date(newTime);
        Util.ASSERT(!d.equals(prevDate));
    }

    Calendar c = new GregorianCalendar();
    c.setTime(d != null ? d : new Date());

    prevDate = d;

    Address to[] = null, cc[] = null, bcc[] = null;
    Address[] from = null;
    try {
        //          allrecip = m.getAllRecipients(); // turns out to be too expensive because it looks for newsgroup headers for imap
        // assemble to, cc, bcc into a list and copy it into allrecip
        List<Address> list = new ArrayList<Address>();
        from = m.getFrom();
        to = m.getRecipients(Message.RecipientType.TO);
        if (to != null)
            list.addAll(Arrays.asList(to));
        cc = m.getRecipients(Message.RecipientType.CC);
        if (cc != null)
            list.addAll(Arrays.asList(cc));
        bcc = m.getRecipients(Message.RecipientType.BCC);
        if (bcc != null)
            list.addAll(Arrays.asList(bcc));

        // intern the strings in these addresses to save memory cos they are repeated often in a large archive
        internAddressList(from);
        internAddressList(to);
        internAddressList(cc);
        internAddressList(bcc);
    } catch (AddressException ae) {
        String s = "Bad address in folder " + folder_name() + " message id" + id + " " + ae;
        dataErrors.add(s);
    }

    // take a deep breath. This object is going to live longer than most of us.
    EmailDocument ed = new EmailDocument(id, email_source(), folder_name(), to, cc, bcc, from, m.getSubject(),
            m.getMessageID(), c.getTime());

    String[] headers = m.getHeader("List-Post");
    if (headers != null && headers.length > 0) {
        // trim the headers because they usually look like: "<mailto:prpl-devel@lists.stanford.edu>"
        ed.sentToMailingLists = new String[headers.length];
        int i = 0;
        for (String header : headers) {
            header = header.trim();
            header = header.toLowerCase();

            if (header.startsWith("<") && header.endsWith(">"))
                header = header.substring(1, header.length() - 1);
            if (header.startsWith("mailto:") && !"mailto:".equals(header)) // defensive check in case header == "mailto:"
                header = header.substring(("mailto:").length());
            ed.sentToMailingLists[i++] = header;
        }
    }
    if (hackyDate) {
        String s = "Guessed date " + Util.formatDate(c) + " for message id: " + id + ": " + ed.getHeader();
        dataErrors.add(s);
        ed.hackyDate = true;
    }

    // check if the message has attachments.
    // if it does and we're not downloading attachments, then we mark the ed as such.
    // otherwise we had a problem where a message header (and maybe text) was downloaded but without attachments in one run
    // but in a subsequent run where attachments were needed, we thought the message was already cached and there was no
    // need to recompute it, leaving the attachments field in this ed incorrect.
    List<String> attachmentNames = getAttachmentNames(m, m);
    if (!Util.nullOrEmpty(attachmentNames)) {
        ed.attachmentsYetToBeDownloaded = true; // will set it to false later if attachments really were downloaded (not sure why)
        //         log.info ("added " + attachmentNames.size() + " attachments to message: " + ed);
    }
    return ed;
}