Example usage for javax.mail.internet MimeMessage getMessageID

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

Introduction

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

Prototype

public String getMessageID() throws MessagingException 

Source Link

Document

Returns the value of the "Message-ID" header field.

Usage

From source file:com.alvexcore.repo.emails.impl.ExtendedEmailMessage.java

protected void parseMessage(MimeMessage msg, UIDFolder folder) {
    try {/*from   w  w w .  j  a va 2s . c o m*/
        if (uid == -1) {
            uid = folder.getUID(msg);
        }
        if (id == null) {
            id = msg.getMessageID();
        }
        if (inReplyTo == null) {
            String[] headers = msg.getHeader(IN_REPLY_TO);
            if (headers != null) {
                if (headers.length == 1)
                    inReplyTo = headers[0];
                else if (headers.length > 1)
                    throw new EmailMessageException(ERR_EXTRACTING_IN_REPLY_TO,
                            "Not more than one " + IN_REPLY_TO + " header expected");
            }
        }
    } catch (MessagingException e) {
        throw new EmailMessageException(ERR_EXTRACTING_ATTRS, e.getMessage());
    }
}

From source file:com.silverpeas.mailinglist.service.job.MailProcessor.java

/**
 * Process an email, extracting attachments and constructing a Message.
 *
 * @param mail the email to be processed.
 * @param mailingList the mailing list it is going to be affected to.
 * @param event the event which will be send at the end of all processing.
 * @throws MessagingException/*  w w w .  j  av a 2 s .c  o  m*/
 * @throws IOException
 */
public void prepareMessage(MimeMessage mail, MessageListener mailingList, MessageEvent event)
        throws MessagingException, IOException {
    String sender = ((InternetAddress[]) mail.getFrom())[0].getAddress();
    if (!mailingList.checkSender(sender)) {
        return;
    }
    Message message = new Message();
    message.setComponentId(mailingList.getComponentId());
    message.setSender(sender);
    message.setSentDate(mail.getSentDate());
    message.setMessageId(mail.getMessageID());
    String[] referenceId = mail.getHeader(MAIL_HEADER_IN_REPLY_TO);
    if (referenceId == null || referenceId.length == 0) {
        referenceId = mail.getHeader(MAIL_HEADER_REFERENCES);
    }
    if (referenceId == null || referenceId.length == 0) {
        message.setReferenceId(null);
    } else {
        message.setReferenceId(referenceId[0]);
    }
    message.setTitle(mail.getSubject());
    SilverTrace.info("mailingList", "MailProcessor.prepareMessage()", "mailinglist.notification.error",
            "Processing message " + mail.getSubject());
    Object content = mail.getContent();
    if (content instanceof Multipart) {
        processMultipart((Multipart) content, message);
    } else if (content instanceof String) {
        processBody((String) content, mail.getContentType(), message);
    }
    event.addMessage(message);
}

From source file:org.apache.james.transport.mailets.HeadersToHTTP.java

private HashSet<NameValuePair> getNameValuePairs(MimeMessage message)
        throws UnsupportedEncodingException, MessagingException {

    // to_address
    // from/*from   w  w  w .j a  v a2  s  .  c  o m*/
    // reply to
    // subject

    HashSet<NameValuePair> pairs = new HashSet<NameValuePair>();

    if (message != null) {
        if (message.getSender() != null) {
            pairs.add(new BasicNameValuePair("from", message.getSender().toString()));
        }
        if (message.getReplyTo() != null) {
            pairs.add(new BasicNameValuePair("reply_to", Arrays.toString(message.getReplyTo())));
        }
        if (message.getMessageID() != null) {
            pairs.add(new BasicNameValuePair("message_id", message.getMessageID()));
        }
        if (message.getSubject() != null) {
            pairs.add(new BasicNameValuePair("subject", message.getSubject()));
        }
        pairs.add(new BasicNameValuePair("size", Integer.toString(message.getSize())));
    }

    pairs.add(new BasicNameValuePair(parameterKey, parameterValue));

    return pairs;
}

From source file:com.adaptris.core.mail.MailConsumerImp.java

@Override
protected int processMessages() {
    int count = 0;

    try (Closeable c = mbox) {
        mbox.connect();/*from   w w  w .ja va 2 s.com*/
        // log.trace("there are {} messages to process", mbox.getMessages().size());
        for (MimeMessage msg : mbox) {
            try {
                mbox.setMessageRead(msg);
                List<AdaptrisMessage> msgs = createMessages(msg);
                for (AdaptrisMessage m : msgs) {
                    retrieveAdaptrisMessageListener().onAdaptrisMessage(m);
                }
            } catch (MailException e) {
                mbox.resetMessage(msg);
                log.debug("Error processing {}", (msg != null ? msg.getMessageID() : "<null>"), e);
            }
            if (!continueProcessingMessages(++count)) {
                break;
            }
        }
    } catch (Exception e) {
        log.warn("Error reading mailbox; will retry on next poll", e);
    }
    return count;
}

From source file:org.zilverline.core.IMAPCollection.java

/**
 * Index a MIME message, which seems to be all of them.
 *//*from   w  w w. j  a v  a  2 s .  c o m*/
private void indexMimeMessage(final Document doc, final MimeMessage mm) throws MessagingException, IOException {
    // o.println( "\n\n[index mm]: " + mm.getSubject());

    long size = mm.getSize();
    int lines = mm.getLineCount();
    doc.add(Field.Keyword("hash", mm.getMessageID()));

    if (size > 0) {
        doc.add(Field.UnIndexed(F_SIZE, "" + size));
    } else {
        doc.add(Field.UnIndexed(F_SIZE, "" + 0));
    }
    indexPart(doc, mm);
}

From source file:com.duroty.service.Mailet.java

/**
 * DOCUMENT ME!// w w w  . j av  a  2 s .  co  m
 *
 * @param id DOCUMENT ME!
 * @param message DOCUMENT ME!
 */
private void saveInReplyTo(String id, MimeMessage message) {
    try {
        String[] inReplyTos = message.getHeader(RFC2822Headers.IN_REPLY_TO);

        String messageId = message.getMessageID();

        if ((inReplyTos == null) || (inReplyTos.length <= 0)) {
            if (messageId != null) {
                message.addHeader(RFC2822Headers.IN_REPLY_TO, messageId);
                message.saveChanges();
            }
        }

        String[] references = message.getHeader(RFC2822Headers.REFERENCES);

        if ((references == null) || (references.length <= 0)) {
            message.addHeader(RFC2822Headers.REFERENCES, messageId);
            message.saveChanges();
        }
    } catch (Exception e) {
    }
}

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

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

    SMIMEEncrypt mailet = new SMIMEEncrypt();

    mailet.init(mailetConfig);/*  www.  java2 s.  c o m*/

    Mail mail = new MockMail();

    MimeMessage message = MailUtils.loadMessage(new File(testBase, "mail/simple-text-message-with-id.eml"));

    assertEquals("<123456>", message.getMessageID());

    mail.setMessage(message);

    Set<MailAddress> recipients = new HashSet<MailAddress>();

    recipients.add(new MailAddress("test@example.com"));

    mail.setRecipients(recipients);

    DjigzoMailAttributes mailAttributes = new DjigzoMailAttributesImpl(mail);

    mailAttributes.setCertificates(certificates);

    mailet.service(mail);

    MailUtils.validateMessage(mail.getMessage());

    assertEquals(SMIMEHeader.ENCRYPTED_CONTENT_TYPE, mail.getMessage().getContentType());

    SMIMEInspector inspector = new SMIMEInspectorImpl(mail.getMessage(), null, "BC");

    assertEquals(SMIMEType.ENCRYPTED, inspector.getSMIMEType());
    assertEquals(SMIMEEncryptionAlgorithm.DES_EDE3_CBC.getOID().toString(),
            inspector.getEnvelopedInspector().getEncryptionAlgorithmOID());
    assertEquals(20, inspector.getEnvelopedInspector().getRecipients().size());
    assertEquals("<123456>", mail.getMessage().getMessageID());
}

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

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

    SMIMEEncrypt mailet = new SMIMEEncrypt();

    mailetConfig.setInitParameter("retainMessageID", "false");

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

    Mail mail = new MockMail();

    MimeMessage message = MailUtils.loadMessage(new File(testBase, "mail/simple-text-message-with-id.eml"));

    assertEquals("<123456>", message.getMessageID());

    mail.setMessage(message);

    Set<MailAddress> recipients = new HashSet<MailAddress>();

    recipients.add(new MailAddress("test@example.com"));

    mail.setRecipients(recipients);

    DjigzoMailAttributes mailAttributes = new DjigzoMailAttributesImpl(mail);

    mailAttributes.setCertificates(certificates);

    mailet.service(mail);

    MailUtils.validateMessage(mail.getMessage());

    assertEquals(SMIMEHeader.ENCRYPTED_CONTENT_TYPE, mail.getMessage().getContentType());

    SMIMEInspector inspector = new SMIMEInspectorImpl(mail.getMessage(), null, "BC");

    assertEquals(SMIMEType.ENCRYPTED, inspector.getSMIMEType());
    assertEquals(SMIMEEncryptionAlgorithm.DES_EDE3_CBC.getOID().toString(),
            inspector.getEnvelopedInspector().getEncryptionAlgorithmOID());
    assertEquals(20, inspector.getEnvelopedInspector().getRecipients().size());

    assertFalse("<123456>".equals(mail.getMessage().getMessageID()));
    assertTrue(mail.getMessage().getMessageID().contains("JavaMail"));
}

From source file:org.alfresco.repo.imap.AlfrescoImapFolder.java

/**
 * Extract a <code>NodeRef</code> from the message id.
 * <br>Typical message id is "<74bad8aa-75a5-4063-8e46-9d1b5737f43b@alfresco.org>"
 * <br>See {@link AbstractMimeMessage#updateMessageID()}
 * // w  w w .  j a  v a 2  s .c o  m
 * @param message MimeMessage
 * @return null if nothing is found
 */
private NodeRef extractNodeRef(MimeMessage message) {
    String uuid = null;
    String messageId = null;
    NodeRef result = null;
    NodeService nodeService = serviceRegistry.getNodeService();
    try {
        messageId = message.getMessageID();
    } catch (MessagingException me) {
        // we cannot use message id to extract nodeRef
    }

    if (messageId != null) {
        if (messageId.startsWith("<")) {
            messageId = messageId.substring(1);
        }
        if (messageId.indexOf("@") != -1) {
            uuid = messageId.substring(0, messageId.indexOf("@"));
        } else {
            uuid = messageId;
        }
        result = new NodeRef(StoreRef.PROTOCOL_WORKSPACE, "SpacesStore", uuid);
        if (nodeService.exists(result) == false) {
            result = null;
        }
    }

    if (result == null) {
        //check X-Alfresco-NodeRef-ID header
        try {
            if (message.getHeader(AlfrescoImapConst.X_ALF_NODEREF_ID) != null) {
                uuid = message.getHeader(AlfrescoImapConst.X_ALF_NODEREF_ID)[0];
                result = new NodeRef(StoreRef.PROTOCOL_WORKSPACE, "SpacesStore", uuid);
                if (nodeService.exists(result) == false) {
                    result = null;
                }
            }
        } catch (MessagingException e) {
            //Do nothing
        }
    }
    return result;
}

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

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

    SMIMEEncrypt mailet = new SMIMEEncrypt();

    mailetConfig.setInitParameter("retainMessageID", "false");
    mailetConfig.setInitParameter("protectedHeader", "from, message-id");

    mailet.init(mailetConfig);/*from w  ww  . j a v  a  2 s. co m*/

    Mail mail = new MockMail();

    MimeMessage message = MailUtils.loadMessage(new File(testBase, "mail/simple-text-message-with-id.eml"));

    assertEquals("<123456>", message.getMessageID());

    mail.setMessage(message);

    Set<MailAddress> recipients = new HashSet<MailAddress>();

    recipients.add(new MailAddress("test@example.com"));

    mail.setRecipients(recipients);

    DjigzoMailAttributes mailAttributes = new DjigzoMailAttributesImpl(mail);

    mailAttributes.setCertificates(certificates);

    mailet.service(mail);

    MimeMessage encrypted = mail.getMessage();

    /*
     * Change from to see whether it was protected
     */
    encrypted.setFrom(new InternetAddress("changed@changed.example.com"));

    MailUtils.validateMessage(encrypted);

    assertFalse("<123456>".equals(encrypted.getMessageID()));

    assertEquals(SMIMEHeader.ENCRYPTED_CONTENT_TYPE, encrypted.getContentType());

    KeyStoreKeyProvider keyStore = new KeyStoreKeyProvider(
            loadKeyStore(new File("test/resources/testdata/keys/testCertificates.p12"), "test"), "test");

    SMIMEInspector inspector = new SMIMEInspectorImpl(encrypted, keyStore, "BC");

    assertEquals(SMIMEType.ENCRYPTED, inspector.getSMIMEType());

    /*
     * now decrypt and check whether message-id and from was protected
     */
    MimeMessage decrypted = inspector.getContentAsMimeMessage();

    inspector = new SMIMEInspectorImpl(decrypted, keyStore, "BC");

    assertEquals(SMIMEType.NONE, inspector.getSMIMEType());

    assertTrue("<123456>".equals(decrypted.getMessageID()));
    assertEquals("test@example.com", StringUtils.join(decrypted.getFrom()));
}