Example usage for javax.mail.internet MimeMessage isMimeType

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

Introduction

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

Prototype

@Override
public boolean isMimeType(String mimeType) throws MessagingException 

Source Link

Document

Is this Part of the specified MIME type?

Usage

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

@Test
public void testClearSignExtraCRLFPreamble() throws Exception {
    MimeMessage message = loadMessage("extra-cr-lf-preamble.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 .  j a va2s .c  om

    MimeMessage newMessage = builder.buildMessage();

    File file = new File(tempDir, "extra-cr-lf-preamble-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, "extra-cr-lf-preamble-openssl.eml");

    verifyMessage(file, rootCertificate, opensslOutputFile);

    newMessage = MailUtils.loadMessage(opensslOutputFile);

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

    assertEquals("Martijn Brinkers <martijn@djigzo.com>", newMessage.getHeader("from", ","));
    assertEquals("Martijn Brinkers <martijn@djigzo.com>", newMessage.getHeader("to", ","));
    assertEquals("test multiple attachments extra CR/LF preamble", newMessage.getHeader("subject", ","));

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

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 ww w  .  j  a va  2 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());
}

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

@Test
public void testEncryptSignedQuotedPrintableSoftBreaksDirectBC() throws Exception {
    MimeMessage message = loadMessage("qp-soft-breaks-signed.eml");

    SMIMEEnvelopedGenerator envelopedGenerator = new SMIMEEnvelopedGenerator();

    JceKeyTransRecipientInfoGenerator infoGenerator = new JceKeyTransRecipientInfoGenerator(
            encryptionCertificate);//from   w  ww  .  ja  v  a  2 s. c om

    envelopedGenerator.addRecipientInfoGenerator(infoGenerator);

    JceCMSContentEncryptorBuilder encryptorBuilder = new JceCMSContentEncryptorBuilder(
            new ASN1ObjectIdentifier("1.2.840.113549.3.7"), 0).setProvider("BC");

    MimeBodyPart bodyPart = envelopedGenerator.generate(message, encryptorBuilder.build());

    MimeMessage newMessage = new MimeMessage(MailSession.getDefaultSession());

    newMessage.setContent(bodyPart.getContent(), bodyPart.getContentType());

    newMessage.saveChanges();

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

    FileOutputStream output = new FileOutputStream(file);

    MailUtils.writeMessage(newMessage, output);

    newMessage = MailUtils.loadMessage(file);

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

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

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

    newMessage = MailUtils.loadMessage(opensslOutputFileSigned);

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

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

    verifyMessage(opensslOutputFileSigned, rootCertificate, opensslOutputFile);

    newMessage = MailUtils.loadMessage(opensslOutputFile);

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

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

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

@Test
public void testClearSignDeprecatedHeaders() 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.CLEAR);/*  ww  w .  ja  v a  2  s. c o  m*/

    MimeMessage newMessage = builder.buildMessage();

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

    FileOutputStream output = new FileOutputStream(file);

    MailUtils.writeMessage(newMessage, output);

    newMessage = MailUtils.loadMessage(file);

    SMIMEUtils.dissectSigned((Multipart) newMessage.getContent());

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

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

    File opensslOutputFile = new File(tempDir, "testClearSignDeprecatedHeaders-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 testSignEncrypt() throws Exception {
    MimeMessage message = loadMessage("normal-message-with-attach.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  ww  w .  j  a v  a2s .  com*/

    builder.addRecipient(encryptionCertificate, SMIMERecipientMode.ISSUER_SERIAL);

    builder.encrypt(SMIMEEncryptionAlgorithm.DES_EDE3_CBC);

    MimeMessage newMessage = builder.buildMessage();

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

    FileOutputStream output = new FileOutputStream(file);

    MailUtils.writeMessage(newMessage, output);

    newMessage = MailUtils.loadMessage(file);

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

    checkForSourceHeaders(newMessage);

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

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

    newMessage = MailUtils.loadMessage(opensslOutputFileSigned);

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

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

    verifyMessage(opensslOutputFileSigned, rootCertificate, opensslOutputFile);

    newMessage = MailUtils.loadMessage(opensslOutputFile);

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

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

    // the message should contain the signed from, to and subject
    checkForEmbeddedHeaders(newMessage);
}

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

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

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

    builder.addRecipient(encryptionCertificate, SMIMERecipientMode.ISSUER_SERIAL);

    builder.encrypt(SMIMEEncryptionAlgorithm.DES_EDE3_CBC);

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

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

    builder.sign(SMIMESignMode.CLEAR);//from   ww  w .  j  a  v  a  2s  . c  o m

    MimeMessage newMessage = builder.buildMessage();

    File file = new File(tempDir, "testEncryptSign.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 opensslOutputFileEncrypted = new File(tempDir, "testEncryptSign-openssl-encrypted.eml");

    verifyMessage(file, rootCertificate, opensslOutputFileEncrypted);

    newMessage = MailUtils.loadMessage(opensslOutputFileEncrypted);

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

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

    decryptMessage(opensslOutputFileEncrypted, privateKeyEntry.getPrivateKey(), opensslOutputFile);

    newMessage = MailUtils.loadMessage(opensslOutputFile);

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

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

    // the message should contain the signed from, to and subject
    checkForEmbeddedHeaders(newMessage);
}

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.common.security.smime.SMIMEBuilderImplTest.java

@Test
public void testSignEncryptSign() throws Exception {
    MimeMessage message = loadMessage("normal-message-with-attach.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);//  ww  w  .  j a va 2s  .co  m

    builder.addRecipient(encryptionCertificate, SMIMERecipientMode.ISSUER_SERIAL);

    builder.encrypt(SMIMEEncryptionAlgorithm.DES_EDE3_CBC);

    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, "testSignEncryptSign.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 opensslOutputFileEncrypted = new File(tempDir, "testSignEncryptSign-openssl-signed-encrypted.eml");

    verifyMessage(file, rootCertificate, opensslOutputFileEncrypted);

    newMessage = MailUtils.loadMessage(opensslOutputFileEncrypted);

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

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

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

    newMessage = MailUtils.loadMessage(opensslOutputFileSigned);

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

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

    verifyMessage(opensslOutputFileSigned, rootCertificate, opensslOutputFile);

    newMessage = MailUtils.loadMessage(opensslOutputFile);

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

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

    // the message should contain the signed from, to and subject
    checkForEmbeddedHeaders(newMessage);
}

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

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

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

    builder.addRecipient(encryptionCertificate, SMIMERecipientMode.ISSUER_SERIAL);

    builder.encrypt(SMIMEEncryptionAlgorithm.DES_EDE3_CBC);

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

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

    builder.sign(SMIMESignMode.CLEAR);/*  w w  w .ja v  a  2 s.  c  om*/

    builder.addRecipient(encryptionCertificate, SMIMERecipientMode.ISSUER_SERIAL);

    builder.encrypt(SMIMEEncryptionAlgorithm.DES_EDE3_CBC);

    MimeMessage newMessage = builder.buildMessage();

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

    FileOutputStream output = new FileOutputStream(file);

    MailUtils.writeMessage(newMessage, output);

    newMessage = MailUtils.loadMessage(file);

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

    checkForSourceHeaders(newMessage);

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

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

    newMessage = MailUtils.loadMessage(opensslOutputFileSigned);

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

    File opensslOutputFileEncrypted = new File(tempDir, "testEncryptSignEncrypt-openssl-encrypted.eml");

    verifyMessage(opensslOutputFileSigned, rootCertificate, opensslOutputFileEncrypted);

    newMessage = MailUtils.loadMessage(opensslOutputFileEncrypted);

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

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

    decryptMessage(opensslOutputFileEncrypted, privateKeyEntry.getPrivateKey(), opensslOutputFile);

    newMessage = MailUtils.loadMessage(opensslOutputFile);

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

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

    // the message should contain the signed from, to and subject
    checkForEmbeddedHeaders(newMessage);
}