List of usage examples for javax.mail.internet MimeMessage getContent
@Override public Object getContent() throws IOException, MessagingException
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 v a 2 s . c om 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.application.djigzo.james.mailets.SMIMEHandlerTest.java
@Test public void testDecryptOL2010WithMissingSKIInSubjectWorkaroundEnabled() throws Exception { boolean skiWorkaroundEnabled = SecurityConstants.isOutlook2010SKIWorkaroundEnabled(); try {//w w w . ja v a2 s .c o m SecurityConstants.setOutlook2010SKIWorkaroundEnabled(true); importKeyStore(keyAndCertificateWorkflow, new File("test/resources/testdata/keys/outlook2010_cert_missing_subjkeyid.p12"), ""); SMIMEHandler mailet = new SMIMEHandler(); mailetConfig.setInitParameter("handledProcessor", "handled"); mailet.init(mailetConfig); MockMail mail = new MockMail(); MimeMessage message = MailUtils .loadMessage(new File(testBase, "mail/outlook2010_cert_missing_subjkeyid.eml")); mail.setMessage(message); Set<MailAddress> recipients = new HashSet<MailAddress>(); recipients.add(new MailAddress("m.brinkers@pobox.com")); mail.setRecipients(recipients); mail.setSender(new MailAddress("test@example.com")); mailet.service(mail); assertEquals(1, sendMailEventListener.getMails().size()); Mail newMail = sendMailEventListener.getMails().get(0); MimeMessage decrypted = newMail.getMessage(); assertTrue(decrypted.isMimeType("text/plain")); assertEquals("A broken S/MIME encrypted message", decrypted.getSubject()); assertEquals("<000c01cadd1e$d8e3b700$8aab2500$@Domain>", decrypted.getMessageID()); assertEquals(message.getMessageID(), decrypted.getMessageID()); assertEquals("3DES, Key size: 168", decrypted.getHeader("X-Djigzo-Info-Encryption-Algorithm-0", ",")); assertEquals("//2219E504D5750B37D20CC930B14129E1A2E583B1/1.2.840.113549.1.1.1", decrypted.getHeader("X-Djigzo-Info-Encryption-Recipient-0-0", ",")); assertEquals("Created with Outlook 2010 Beta (14.0.4536.1000)", StringUtils.trim((String) decrypted.getContent())); assertEquals(Mail.GHOST, mail.getState()); } finally { SecurityConstants.setOutlook2010SKIWorkaroundEnabled(skiWorkaroundEnabled); } }
From source file:mitm.application.djigzo.james.mailets.SMIMEHandlerTest.java
@Test public void test3Layers() throws Exception { SMIMEHandler mailet = new SMIMEHandler(); mailetConfig.setInitParameter("handledProcessor", "handled"); mailetConfig.setInitParameter("threshold", "10"); mailet.init(mailetConfig);/*from ww w. j av a2s. c o m*/ MockMail mail = new MockMail(); mail.setState(Mail.TRANSPORT); 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(1, sendMailEventListener.getMails().size()); Mail newMail = sendMailEventListener.getMails().get(0); MimeMessage newMessage = newMail.getMessage(); SMIMEInspector inspector = new SMIMEInspectorImpl(newMessage, null, "BC"); assertEquals(SMIMEType.NONE, inspector.getSMIMEType()); assertEquals(4, 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"))); assertTrue(newMail.getRecipients().contains(new MailAddress("test4@example.com"))); assertEquals("handled", newMail.getState()); assertEquals(Mail.GHOST, mail.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", ",")); assertEquals("AES128, Key size: 128", newMessage.getHeader("X-Djigzo-Info-Encryption-Algorithm-2", ",")); assertEquals("test", ((String) newMessage.getContent()).trim()); }
From source file:com.flexoodb.common.FlexUtils.java
/** * method to obtain a FileContainer containing the file wrapped in a mimemessage. * * @param data the mimemessage in a byte array. * @return a FileContainer containing the file. *//*from w ww . j ava2 s.c om*/ static public FileContainer extractFileFromMimeMessage(byte[] data) throws Exception { FileContainer fc = null; MimeMessage message = new MimeMessage(null, new ByteArrayInputStream(data)); Object content = message.getContent(); if (content instanceof Multipart) { Multipart multipart = (Multipart) content; for (int i = 0, n = multipart.getCount(); i < n; i++) { Part part = multipart.getBodyPart(i); String disposition = part.getDisposition(); if ((disposition != null) && (disposition.equals(Part.ATTACHMENT) || (disposition.equals(Part.INLINE)))) { if (part.getFileName() != null) { fc = new FileContainer(part.getFileName(), part.getFileName(), getBytesFromInputStream(part.getInputStream())); break; } } } } return fc; }
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"); /*/*w w w . j a va2 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.j a v a 2 s. com 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 w w w . jav a2 s. 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);/* w ww. j av a 2s .c o m*/ 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.application.djigzo.james.mailets.SMIMEHandlerTest.java
@Test public void test3LayersStrictDomainCerts() throws Exception { SMIMEHandler mailet = new SMIMEHandler(); mailetConfig.setInitParameter("handledProcessor", "handled"); mailetConfig.setInitParameter("threshold", "10"); mailetConfig.setInitParameter("strict", "true"); mailet.init(mailetConfig);/*from w w w . j av a 2 s.com*/ MockMail mail = new MockMail(); mail.setState(Mail.TRANSPORT); /* * Make sure sub@subdomain.example.com can decrypt all */ proxy.addDomainCertificate("sub.example.com", (X509Certificate) keyStore.getCertificate("NoEmail")); proxy.addDomainCertificate("sub.example.com", (X509Certificate) keyStore.getCertificate("ValidCertificate")); proxy.addDomainCertificate("sub.example.com", (X509Certificate) keyStore.getCertificate("multipleEmail")); MimeMessage message = MailUtils.loadMessage(new File(testBase, "mail/3-layer-encrypted.eml")); mail.setMessage(message); Set<MailAddress> recipients = new LinkedHashSet<MailAddress>(); recipients.add(new MailAddress("test@sub.example.com")); recipients.add(new MailAddress("test2@sub.example.com")); recipients.add(new MailAddress("test3@sub.example.com")); recipients.add(new MailAddress("test4@sub.example.com")); recipients.add(new MailAddress("test5@example.com")); mail.setRecipients(recipients); mail.setSender(new MailAddress("sender@example.com")); mailet.service(mail); assertEquals(2, sendMailEventListener.getMails().size()); Mail newMail = sendMailEventListener.getMails().get(0); MimeMessage newMessage = newMail.getMessage(); SMIMEInspector inspector = new SMIMEInspectorImpl(newMessage, null, "BC"); assertEquals(SMIMEType.ENCRYPTED, inspector.getSMIMEType()); assertEquals("RC2, Key size: 128", newMessage.getHeader("X-Djigzo-Info-Encryption-Algorithm-0", ",")); assertEquals(1, newMail.getRecipients().size()); assertTrue(newMail.getRecipients().contains(new MailAddress("test5@example.com"))); newMail = sendMailEventListener.getMails().get(1); newMessage = newMail.getMessage(); inspector = new SMIMEInspectorImpl(newMessage, null, "BC"); assertEquals(SMIMEType.NONE, inspector.getSMIMEType()); assertEquals(4, newMail.getRecipients().size()); assertTrue(newMail.getRecipients().contains(new MailAddress("test@sub.example.com"))); assertTrue(newMail.getRecipients().contains(new MailAddress("test2@sub.example.com"))); assertTrue(newMail.getRecipients().contains(new MailAddress("test3@sub.example.com"))); assertTrue(newMail.getRecipients().contains(new MailAddress("test4@sub.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", ",")); assertEquals("AES128, Key size: 128", newMessage.getHeader("X-Djigzo-Info-Encryption-Algorithm-2", ",")); assertEquals("test", ((String) newMessage.getContent()).trim()); assertEquals(Mail.GHOST, mail.getState()); }
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()); }