@Test void testHtmlAndOneAttachment() throws MessagingException, IOException { Email email = Email.create() .from("inf0@jodd.org") .to("ig0r@gmail.com") .subject("test6") .textMessage("Hello!") .attachment(EmailAttachment.with().content(BYTES_11_15, APPLICATION_ZIP)); Message message = createMessage(email); // wrapper final MimeMultipart multipart = (MimeMultipart) message.getContent(); assertEquals(2, multipart.getCount()); // inner content #1 MimeBodyPart mimeBodyPart = (MimeBodyPart) multipart.getBodyPart(0); final MimeMultipart mimeMultipart = (MimeMultipart) mimeBodyPart.getContent(); assertEquals(1, mimeMultipart.getCount()); MimeBodyPart bodyPart = (MimeBodyPart) mimeMultipart.getBodyPart(0); assertEquals("Hello!", bodyPart.getContent()); }
SMIMEEnvelopedGenerator gen = new SMIMEEnvelopedGenerator(); JceKeyAgreeRecipientInfoGenerator rig = new JceKeyAgreeRecipientInfoGenerator(CMSAlgorithm.ECDH_SHA1KDF, senderPrivateKey, senderPublicKey, CMSAlgorithm.AES128_WRAP); rig.setProvider("BC"); gen.addRecipientInfoGenerator(rig); MimeBodyPart msg = new MimeBodyPart(); msg.setText("This is a secret message"); MimeBodyPart mp = gen.generate(msg, new JceCMSContentEncryptorBuilder(CMSAlgorithm.AES128_CBC).setProvider("BC").build()); Properties props = System.getProperties(); Session session = Session.getDefaultInstance(props, null); String to = "bob@example.com"; Address fromUser = new InternetAddress("alice@example.com"); Address toUser = new InternetAddress(to); MimeMessage body = new MimeMessage(session); body.setFrom(fromUser); body.setRecipient(Message.RecipientType.TO, toUser); body.setSubject("example encrypted message"); body.setContent(mp.getContent(), mp.getContentType()); body.saveChanges(); body.writeTo(new FileOutputStream("/tmp/encrypted.msg"));
String type[] = bp.getContentType().split("/"); if (type[0].equals("text") && type[1].equals("plain")) { map.put(name, bp.getContent()); } else {
@Test void testTextHtml() throws MessagingException, IOException { final Email email = Email.create() .from(FROM_EXAMPLE_COM) .to(TO_EXAMPLE_COM) .subject(SUB) .textMessage(HELLO) .htmlMessage("<html><body><h1>Hey!</h1></body></html>"); final Message message = createMessage(email); assertEquals(1, message.getFrom().length); assertEquals(FROM_EXAMPLE_COM, message.getFrom()[0].toString()); assertEquals(1, message.getRecipients(RecipientType.TO).length); assertEquals(TO_EXAMPLE_COM, message.getRecipients(RecipientType.TO)[0].toString()); assertEquals(SUB, message.getSubject()); // wrapper final MimeMultipart multipart = (MimeMultipart) message.getContent(); assertEquals(1, multipart.getCount()); assertTrue(multipart.getContentType().contains("multipart/mixed")); // inner content final MimeBodyPart mimeBodyPart = (MimeBodyPart) multipart.getBodyPart(0); final MimeMultipart mimeMultipart = (MimeMultipart) mimeBodyPart.getContent(); assertEquals(2, mimeMultipart.getCount()); assertTrue(mimeMultipart.getContentType().contains("multipart/alternative")); MimeBodyPart bodyPart = (MimeBodyPart) mimeMultipart.getBodyPart(0); assertEquals(HELLO, bodyPart.getContent()); assertTrue(bodyPart.getDataHandler().getContentType().contains(MimeTypes.MIME_TEXT_PLAIN)); bodyPart = (MimeBodyPart) mimeMultipart.getBodyPart(1); assertEquals("<html><body><h1>Hey!</h1></body></html>", bodyPart.getContent()); assertTrue(bodyPart.getDataHandler().getContentType().contains(MimeTypes.MIME_TEXT_HTML)); }
String type[] = bp.getContentType().split("/"); if (type[0].equals("text") && type[1].equals("plain")) { map.put(name, bp.getContent()); } else {
final MimeMultipart mimeMultipart = (MimeMultipart) mimeBodyPart.getContent(); assertEquals(2, mimeMultipart.getCount()); assertEquals(HELLO, bodyPart.getContent()); final MimeMultipart htmlMessage = (MimeMultipart) bodyPart.getContent(); assertTrue(htmlMessage.getContentType().contains("multipart/related")); assertEquals(2, htmlMessage.getCount()); assertEquals("<html><body><h1>Hey!</h1><img src='cid:c.png'></body></html>", htmlMimeBodyPart.getContent()); assertTrue(htmlMimeBodyPart.getDataHandler().getContentType().contains(MimeTypes.MIME_TEXT_HTML));
public static MimeMessage encryptMessage(MimeMessage message) throws Exception { Security.addProvider(new BouncyCastleProvider()); // create the generator for creating an smime/encrypted message SMIMEEnvelopedGenerator gen = new SMIMEEnvelopedGenerator(); X509Certificate recipientCert = getRecipientPublicCertificate(message); gen.addRecipientInfoGenerator(new JceKeyTransRecipientInfoGenerator(recipientCert).setProvider("BC")); MimeBodyPart msg = new MimeBodyPart(); msg.setContent(message.getContent(), message.getContentType()); MimeBodyPart mp = gen.generate(msg, new JceCMSContentEncryptorBuilder(CMSAlgorithm.RC2_CBC).setProvider("BC").build()); message.setContent(mp.getContent(), mp.getContentType()); message.saveChanges(); return message; }
MimeBodyPart mbp = (MimeBodyPart)part; if (mbp.isMimeType("text/plain")) { body += mbp.getContent().toString(); } else if (mbp.isMimeType("TEXT/HTML")) { body += mbp.getContent().toString(); }else if(mbp.isMimeType("text/xml")) { body += mbp.getContent().toString(); }
String type[] = bp.getContentType().split("/"); if (type[0].equals("text") && type[1].equals("plain")) { map.put(name, bp.getContent()); } else {
public InputStream getContentInputStream(int bodyPart) throws java.io.IOException,javax.mail.MessagingException{ if (!isMultipart) { return in; } MimeBodyPart bp = (MimeBodyPart) mp.getBodyPart(bodyPart); Object o = bp.getContent(); if (o instanceof java.io.InputStream) { return (InputStream) o; } else { throw new java.io.IOException("getContentInputStreamCannot handle data type " + o.getClass().getName() + " in SOAPLite"); } }
lines.add("file: " + fileName); } else { lines.add(S.string(bp.getContent()));
public String getContent(int bodyPart) throws java.io.IOException,javax.mail.MessagingException{ if (!isMultipart) { return body; } MimeBodyPart bp = (MimeBodyPart) mp.getBodyPart(bodyPart); Object o = bp.getContent(); if (o.getClass().getName().equals("java.lang.String")) { return (String) o; } else if (o.getClass().getName().equals("java.io.ByteArrayInputStream")) { return translate( (ByteArrayInputStream) o); } else { throw new java.io.IOException("Cannot handle data type " + o.getClass().getName() + " in SOAPLite"); } }
lines.add("file: " + fileName); } else { lines.add(S.string(bp.getContent()));
if(part.getContent() instanceof Multipart) collectParts((Multipart)part.getContent(), contents, attachments); contents.add((String)part.getContent()); attachments.add(part.getContent());
throw new GeneralSecurityException ("Content-Type indicates data isn't signed: " + aPart.getContentType ()); final MimeMultipart aMainPart = (MimeMultipart) aPart.getContent ();
private static void parseMultipart(final StrBuilder sb, final Multipart multipart) throws MessagingException, IOException { for (int i = 0; i < multipart.getCount(); i++) { BodyPart bodyPart = multipart.getBodyPart(i); String disposition = bodyPart.getDisposition(); if (disposition == null && bodyPart instanceof MimeBodyPart) { // not an attachment MimeBodyPart mimeBodyPart = (MimeBodyPart) bodyPart; if (mimeBodyPart.getContent() instanceof Multipart) { parseMultipart(sb, (Multipart) mimeBodyPart.getContent()); } else { String body = (String) mimeBodyPart.getContent(); sb.appendln(body); sb.appendln(""); } } } } }
new JceCMSContentEncryptorBuilder(encryption).setProvider(BouncyCastleProvider.PROVIDER_NAME) .build()); encryptedMessage.setContent(mp.getContent(), mp.getContentType()); Enumeration<?> mpEnum = mp.getAllHeaders(); while (mpEnum.hasMoreElements()) {
Object content = part.getContent(); if (content instanceof String) { return (String) part.getContent(); InputStream is = (InputStream) part.getContent(); Scanner scanner = null; try {
MimeBodyPart bodyPart = (MimeBodyPart) multiPart.getBodyPart(i); try { Object mimeBody = bodyPart.getContent(); if (mimeBody instanceof MimeMultipart) { appendBodyStructure(buffer, (MimeMultipart) mimeBody);
String contentType = message.getContentType(); String messageContent = ""; if (contentType.contains("multipart")) { // content may contain attachments Multipart multiPart = (Multipart) message.getContent(); int numberOfParts = multiPart.getCount(); for (int partCount = 0; partCount < numberOfParts; partCount++) { MimeBodyPart part = (MimeBodyPart) multiPart.getBodyPart(partCount); if (Part.ATTACHMENT.equalsIgnoreCase(part.getDisposition())) { // this part is attachment String fileName = part.getFileName(); attachFiles += fileName + ", "; part.saveFile(saveDirectory + File.separator + fileName); } else { // this part may be the message content messageContent = part.getContent().toString(); } } if (attachFiles.length() > 1) { attachFiles = attachFiles.substring(0, attachFiles.length() - 2); } } else if (contentType.contains("text/plain") || contentType.contains("text/html")) { Object content = message.getContent(); if (content != null) { messageContent = content.toString(); } }