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, String delimiter) throws MessagingException 

Source Link

Document

Get all the headers for this header name, returned as a single String, with headers separated by the delimiter.

Usage

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

@Test
public void testCompressDeprecatedHeaders() throws Exception {
    MimeMessage message = loadMessage("simple-text-message.eml");

    SMIMEBuilder builder = new SMIMEBuilderImpl(message);

    builder.setUseDeprecatedContentTypes(true);

    builder.compress();//from  w w w  .  j av  a 2s . com

    MimeMessage newMessage = builder.buildMessage();

    File file = new File(tempDir, "testCompressDeprecatedHeaders.eml");

    FileOutputStream output = new FileOutputStream(file);

    MailUtils.writeMessage(newMessage, output);

    newMessage = MailUtils.loadMessage(file);

    assertEquals(SMIMEHeader.DEPRECATED_COMPRESSED_CONTENT_TYPE, newMessage.getContentType());

    assertEquals(SMIMEHeader.Type.COMPRESSED, SMIMEHeader.getSMIMEContentType(newMessage));

    assertEquals("test@example.com", newMessage.getHeader("from", ","));
    assertEquals("test@example.com", newMessage.getHeader("to", ","));
    assertEquals("test simple message", newMessage.getHeader("subject", ","));
}

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

@Test
public void testSimpleMessageEncrypt() throws Exception {
    MimeMessage message = loadMessage("simple-text-message.eml");

    SMIMEBuilder builder = new SMIMEBuilderImpl(message, "to", "subject", "from");

    builder.addRecipient(encryptionCertificate, SMIMERecipientMode.ISSUER_SERIAL);

    builder.encrypt(SMIMEEncryptionAlgorithm.DES_EDE3_CBC);

    MimeMessage newMessage = builder.buildMessage();

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

    FileOutputStream output = new FileOutputStream(file);

    MailUtils.writeMessage(newMessage, output);

    newMessage = MailUtils.loadMessage(file);

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

    assertEquals(SMIMEHeader.Type.ENCRYPTED, SMIMEHeader.getSMIMEContentType(newMessage));

    assertEquals("test@example.com", newMessage.getHeader("from", ","));
    assertEquals("test@example.com", newMessage.getHeader("to", ","));
    assertEquals("test simple message", newMessage.getHeader("subject", ","));

    File opensslOutputFileSigned = new File(tempDir, "simple-text-message-encrypted-openssl.eml");

    decryptMessage(file, privateKeyEntry.getPrivateKey(), opensslOutputFileSigned);

    newMessage = MailUtils.loadMessage(opensslOutputFileSigned);

    assertEquals(SMIMEHeader.Type.NO_SMIME, SMIMEHeader.getSMIMEContentType(newMessage));

    assertTrue(newMessage.isMimeType("text/plain"));

    assertEquals("test@example.com", newMessage.getHeader("from", ","));
    assertEquals("test@example.com", newMessage.getHeader("to", ","));
    assertEquals("test simple message", newMessage.getHeader("subject", ","));

    assertEquals(SMIMEHeader.Type.NO_SMIME, SMIMEHeader.getSMIMEContentType(newMessage));

    String content = (String) newMessage.getContent();

    assertEquals("test", content.trim());
}

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

@Test
public void testEncryptDeprecatedHeaders() throws Exception {
    MimeMessage message = loadMessage("simple-text-message.eml");

    SMIMEBuilder builder = new SMIMEBuilderImpl(message, "to", "subject", "from");

    builder.setUseDeprecatedContentTypes(true);

    builder.addRecipient(encryptionCertificate, SMIMERecipientMode.ISSUER_SERIAL);

    builder.encrypt(SMIMEEncryptionAlgorithm.DES_EDE3_CBC);

    MimeMessage newMessage = builder.buildMessage();

    File file = new File(tempDir, "testEncryptDeprecatedHeaders.eml");

    FileOutputStream output = new FileOutputStream(file);

    MailUtils.writeMessage(newMessage, output);

    newMessage = MailUtils.loadMessage(file);

    assertEquals(SMIMEHeader.DEPRECATED_ENCRYPTED_CONTENT_TYPE, newMessage.getContentType());

    assertEquals(SMIMEHeader.Type.ENCRYPTED, SMIMEHeader.getSMIMEContentType(newMessage));

    assertEquals("test@example.com", newMessage.getHeader("from", ","));
    assertEquals("test@example.com", newMessage.getHeader("to", ","));
    assertEquals("test simple message", newMessage.getHeader("subject", ","));

    File opensslOutputFileSigned = new File(tempDir, "testEncryptDeprecatedHeaders-openssl.eml");

    decryptMessage(file, privateKeyEntry.getPrivateKey(), opensslOutputFileSigned);

    newMessage = MailUtils.loadMessage(opensslOutputFileSigned);

    assertEquals(SMIMEHeader.Type.NO_SMIME, SMIMEHeader.getSMIMEContentType(newMessage));

    assertTrue(newMessage.isMimeType("text/plain"));

    assertEquals("test@example.com", newMessage.getHeader("from", ","));
    assertEquals("test@example.com", newMessage.getHeader("to", ","));
    assertEquals("test simple message", newMessage.getHeader("subject", ","));

    assertEquals(SMIMEHeader.Type.NO_SMIME, SMIMEHeader.getSMIMEContentType(newMessage));

    String content = (String) newMessage.getContent();

    assertEquals("test", content.trim());
}

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

@Test
public void test3LayersStrictBlacklistedCert() throws Exception {
    SMIMEHandler mailet = new SMIMEHandler();

    mailetConfig.setInitParameter("handledProcessor", "handled");
    mailetConfig.setInitParameter("threshold", "10");
    mailetConfig.setInitParameter("strict", "true");

    mailet.init(mailetConfig);/*from  ww w.j av  a2s.c o  m*/

    MockMail mail = new MockMail();

    mail.setState(Mail.TRANSPORT);

    /*
     * Blacklist the ValidCertificate
     */
    proxy.addToCTL((X509Certificate) keyStore.getCertificate("ValidCertificate"), CTLEntryStatus.BLACKLISTED,
            false);

    /*
     * message with 3 layers of encryption
     */
    MimeMessage message = MailUtils.loadMessage(new File(testBase, "mail/3-layer-encrypted.eml"));

    mail.setMessage(message);

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

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

    mail.setRecipients(recipients);

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

    mailet.service(mail);

    assertEquals(2, sendMailEventListener.getMails().size());

    /* message 1 */

    Mail newMail = sendMailEventListener.getMails().get(0);

    MimeMessage newMessage = newMail.getMessage();

    SMIMEInspector inspector = new SMIMEInspectorImpl(newMessage, null, "BC");

    /*
     * the next layer could not be decrypted by test@example.com because valid certificate is blacklisted
     */
    assertEquals(SMIMEType.ENCRYPTED, inspector.getSMIMEType());

    /*
     * test@example.com, test2@example.com and test3@example.com can decrypt the first two layers
     */
    assertEquals(3, newMail.getRecipients().size());
    assertTrue(newMail.getRecipients().contains(new MailAddress("test@example.com")));
    assertTrue(newMail.getRecipients().contains(new MailAddress("test2@example.com")));
    assertTrue(newMail.getRecipients().contains(new MailAddress("test3@example.com")));
    assertEquals("handled", newMail.getState());

    assertEquals("RC2, Key size: 128", newMessage.getHeader("X-Djigzo-Info-Encryption-Algorithm-0", ","));
    assertEquals("3DES, Key size: 168", newMessage.getHeader("X-Djigzo-Info-Encryption-Algorithm-1", ","));
    assertNull(newMessage.getHeader("X-Djigzo-Info-Encryption-Algorithm-2"));

    newMail = sendMailEventListener.getMails().get(1);

    newMessage = newMail.getMessage();

    inspector = new SMIMEInspectorImpl(newMessage, null, "BC");

    /*
     * not decrypted by test4@example.com because of strict mode
     */
    assertEquals(SMIMEType.ENCRYPTED, inspector.getSMIMEType());

    assertEquals(1, newMail.getRecipients().size());
    assertTrue(newMail.getRecipients().contains(new MailAddress("test4@example.com")));

    assertEquals("RC2, Key size: 128", newMessage.getHeader("X-Djigzo-Info-Encryption-Algorithm-0", ","));

    /*
     * test4@example.com is not allowed to decrypt the message
     */
    assertEquals(Mail.GHOST, mail.getState());
}

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

@Test
public void testClearSignSimpleTextMessageLF() throws Exception {
    File mail = new File(testDir, "simple-text-message-LF.eml");

    /*/*from   w  w  w .  j  av a 2 s  . co m*/
     * check to make sure the file does not contain CR
     */
    String body = IOUtils.toString(new FileInputStream(mail));

    assertFalse(body.contains("\r"));

    MimeMessage message = MailUtils.loadMessage(mail);

    SMIMEBuilder builder = new SMIMEBuilderImpl(message, "to", "subject", "from");

    builder.addSigner(privateKeyEntry.getPrivateKey(), (X509Certificate) privateKeyEntry.getCertificate(),
            SMIMESigningAlgorithm.SHA1WITHRSA);

    builder.addCertificates(CertificateUtils.getX509Certificates(privateKeyEntry.getCertificateChain()));

    builder.sign(SMIMESignMode.CLEAR);

    MimeMessage newMessage = builder.buildMessage();

    File file = new File(tempDir, "test-simple-text-message-signed-lf.eml");

    FileOutputStream output = new FileOutputStream(file);

    MailUtils.writeMessage(newMessage, output);

    newMessage = MailUtils.loadMessage(file);

    assertEquals(SMIMEHeader.Type.CLEAR_SIGNED, SMIMEHeader.getSMIMEContentType(newMessage));

    File opensslOutputFile = new File(tempDir, "test-simple-text-message-lf-openssl.eml");

    verifyMessage(file, rootCertificate, opensslOutputFile);

    newMessage = MailUtils.loadMessage(opensslOutputFile);

    assertTrue(newMessage.isMimeType("text/plain"));

    assertEquals("test@example.com", newMessage.getHeader("from", ","));
    assertEquals("test@example.com", newMessage.getHeader("to", ","));
    assertEquals("test simple message", newMessage.getHeader("subject", ","));

    assertEquals(SMIMEHeader.Type.NO_SMIME, SMIMEHeader.getSMIMEContentType(newMessage));

    String content = (String) newMessage.getContent();

    assertEquals("test", content.trim());
}

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

@Test
public void testClearSignAddEncryptionKeyPreference() throws Exception {
    MimeMessage message = loadMessage("simple-text-message.eml");

    SMIMEBuilder builder = new SMIMEBuilderImpl(message, "to", "subject", "from");

    builder.addSigner(privateKeyEntry.getPrivateKey(), (X509Certificate) privateKeyEntry.getCertificate(),
            SMIMESigningAlgorithm.SHA1WITHRSA, (X509Certificate) privateKeyEntry.getCertificate());

    builder.addCertificates(CertificateUtils.getX509Certificates(privateKeyEntry.getCertificateChain()));

    builder.sign(SMIMESignMode.CLEAR);/*from w  w w.  java2 s  .co m*/

    MimeMessage newMessage = builder.buildMessage();

    File file = new File(tempDir, "test-signed-encryptionkeypreference-signed.eml");

    FileOutputStream output = new FileOutputStream(file);

    MailUtils.writeMessage(newMessage, output);

    newMessage = MailUtils.loadMessage(file);

    assertEquals(SMIMEHeader.Type.CLEAR_SIGNED, SMIMEHeader.getSMIMEContentType(newMessage));

    File opensslOutputFile = new File(tempDir, "test-signed-encryptionkeypreference-signed-openssl.eml");

    verifyMessage(file, rootCertificate, opensslOutputFile);

    newMessage = MailUtils.loadMessage(opensslOutputFile);

    assertTrue(newMessage.isMimeType("text/plain"));

    assertEquals("test@example.com", newMessage.getHeader("from", ","));
    assertEquals("test@example.com", newMessage.getHeader("to", ","));
    assertEquals("test simple message", newMessage.getHeader("subject", ","));

    assertEquals(SMIMEHeader.Type.NO_SMIME, SMIMEHeader.getSMIMEContentType(newMessage));

    String content = (String) newMessage.getContent();

    assertEquals("test", content.trim());
}

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

@Test
public void testOpaqueSignSimpleTextMessage() throws Exception {
    MimeMessage message = loadMessage("simple-text-message.eml");

    SMIMEBuilder builder = new SMIMEBuilderImpl(message, "to", "subject", "from");

    builder.addSigner(privateKeyEntry.getPrivateKey(), (X509Certificate) privateKeyEntry.getCertificate(),
            SMIMESigningAlgorithm.SHA1WITHRSA);

    builder.addCertificates(CertificateUtils.getX509Certificates(privateKeyEntry.getCertificateChain()));

    builder.sign(SMIMESignMode.OPAQUE);/*from ww  w  .  j a va 2s .c  o m*/

    MimeMessage newMessage = builder.buildMessage();

    File file = new File(tempDir, "test-simple-text-message-opaque-signed.eml");

    FileOutputStream output = new FileOutputStream(file);

    MailUtils.writeMessage(newMessage, output);

    newMessage = MailUtils.loadMessage(file);

    assertEquals(SMIMEHeader.ENCAPSULATED_SIGNED_CONTENT_TYPE, newMessage.getContentType());

    assertEquals(SMIMEHeader.Type.OPAQUE_SIGNED, SMIMEHeader.getSMIMEContentType(newMessage));

    File opensslOutputFile = new File(tempDir, "test-simple-text-message-opaque-openssl.eml");

    verifyMessage(file, rootCertificate, opensslOutputFile);

    newMessage = MailUtils.loadMessage(opensslOutputFile);

    assertTrue(newMessage.isMimeType("text/plain"));

    assertEquals("test@example.com", newMessage.getHeader("from", ","));
    assertEquals("test@example.com", newMessage.getHeader("to", ","));
    assertEquals("test simple message", newMessage.getHeader("subject", ","));

    assertEquals(SMIMEHeader.Type.NO_SMIME, SMIMEHeader.getSMIMEContentType(newMessage));

    String content = (String) newMessage.getContent();

    assertEquals("test", content.trim());
}

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

@Test
public void testOpaqueSignDeprecatedHeaders() throws Exception {
    MimeMessage message = loadMessage("simple-text-message.eml");

    SMIMEBuilder builder = new SMIMEBuilderImpl(message, "to", "subject", "from");

    builder.setUseDeprecatedContentTypes(true);

    builder.addSigner(privateKeyEntry.getPrivateKey(), (X509Certificate) privateKeyEntry.getCertificate(),
            SMIMESigningAlgorithm.SHA1WITHRSA);

    builder.addCertificates(CertificateUtils.getX509Certificates(privateKeyEntry.getCertificateChain()));

    builder.sign(SMIMESignMode.OPAQUE);//from  ww  w. ja va 2 s  .c  om

    MimeMessage newMessage = builder.buildMessage();

    File file = new File(tempDir, "testOpaqueSignDeprecatedHeaders.eml");

    FileOutputStream output = new FileOutputStream(file);

    MailUtils.writeMessage(newMessage, output);

    newMessage = MailUtils.loadMessage(file);

    assertEquals(SMIMEHeader.DEPRECATED_ENCAPSULATED_SIGNED_CONTENT_TYPE, newMessage.getContentType());

    assertEquals(SMIMEHeader.Type.OPAQUE_SIGNED, SMIMEHeader.getSMIMEContentType(newMessage));

    File opensslOutputFile = new File(tempDir, "testOpaqueSignDeprecatedHeaders-openssl.eml");

    verifyMessage(file, rootCertificate, opensslOutputFile);

    newMessage = MailUtils.loadMessage(opensslOutputFile);

    assertTrue(newMessage.isMimeType("text/plain"));

    assertEquals("test@example.com", newMessage.getHeader("from", ","));
    assertEquals("test@example.com", newMessage.getHeader("to", ","));
    assertEquals("test simple message", newMessage.getHeader("subject", ","));

    assertEquals(SMIMEHeader.Type.NO_SMIME, SMIMEHeader.getSMIMEContentType(newMessage));

    String content = (String) newMessage.getContent();

    assertEquals("test", content.trim());
}

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

@Test
public void testClearSignNoProtectedHeader() throws Exception {
    MimeMessage message = loadMessage("normal-message-with-attach.eml");

    SMIMEBuilder builder = new SMIMEBuilderImpl(message, "");

    builder.addSigner(privateKeyEntry.getPrivateKey(), (X509Certificate) privateKeyEntry.getCertificate(),
            SMIMESigningAlgorithm.SHA1WITHRSA);

    builder.addCertificates(CertificateUtils.getX509Certificates(privateKeyEntry.getCertificateChain()));

    builder.sign(SMIMESignMode.CLEAR);/*from  www .j av a  2 s. c o  m*/

    MimeMessage newMessage = builder.buildMessage();

    File file = new File(tempDir, "testClearSignNoProtectedHeader.eml");

    FileOutputStream output = new FileOutputStream(file);

    MailUtils.writeMessage(newMessage, output);

    newMessage = MailUtils.loadMessage(file);

    assertEquals(SMIMEHeader.Type.CLEAR_SIGNED, SMIMEHeader.getSMIMEContentType(newMessage));

    checkForSourceHeaders(newMessage);

    File opensslOutputFile = new File(tempDir, "testClearSignNoProtectedHeader-openssl.eml");

    verifyMessage(file, rootCertificate, opensslOutputFile);

    newMessage = MailUtils.loadMessage(opensslOutputFile);

    assertTrue(newMessage.isMimeType("multipart/mixed"));

    assertEquals(SMIMEHeader.Type.NO_SMIME, SMIMEHeader.getSMIMEContentType(newMessage));

    assertNull(newMessage.getHeader("subject", ","));
    assertEquals("<test@example.com>", message.getHeader("to", ","));
    assertNull(newMessage.getHeader("from"));
}

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

@Test
public void testClearSignSimpleTextMessage() throws Exception {
    MimeMessage message = loadMessage("simple-text-message.eml");

    SMIMEBuilder builder = new SMIMEBuilderImpl(message, "to", "subject", "from");

    builder.addSigner(privateKeyEntry.getPrivateKey(), (X509Certificate) privateKeyEntry.getCertificate(),
            SMIMESigningAlgorithm.SHA1WITHRSA);

    builder.addCertificates(CertificateUtils.getX509Certificates(privateKeyEntry.getCertificateChain()));

    builder.sign(SMIMESignMode.CLEAR);/*from w  w  w  .  ja va2 s  .  c o  m*/

    MimeMessage newMessage = builder.buildMessage();

    File file = new File(tempDir, "test-simple-text-message-signed.eml");

    FileOutputStream output = new FileOutputStream(file);

    MailUtils.writeMessage(newMessage, output);

    newMessage = MailUtils.loadMessage(file);

    assertEquals(SMIMEHeader.DETACHED_SIGNATURE_TYPE,
            SMIMEUtils.dissectSigned((Multipart) newMessage.getContent())[1].getContentType());

    assertEquals(SMIMEHeader.Type.CLEAR_SIGNED, SMIMEHeader.getSMIMEContentType(newMessage));

    File opensslOutputFile = new File(tempDir, "test-simple-text-message-openssl.eml");

    verifyMessage(file, rootCertificate, opensslOutputFile);

    newMessage = MailUtils.loadMessage(opensslOutputFile);

    assertTrue(newMessage.isMimeType("text/plain"));

    assertEquals("test@example.com", newMessage.getHeader("from", ","));
    assertEquals("test@example.com", newMessage.getHeader("to", ","));
    assertEquals("test simple message", newMessage.getHeader("subject", ","));

    assertEquals(SMIMEHeader.Type.NO_SMIME, SMIMEHeader.getSMIMEContentType(newMessage));

    String content = (String) newMessage.getContent();

    assertEquals("test", content.trim());
}