List of usage examples for javax.mail.internet MimeMultipart addBodyPart
@Override public synchronized void addBodyPart(BodyPart part) throws MessagingException
From source file:org.apache.camel.component.mail.MailBinding.java
protected void addBodyToMultipart(MailConfiguration configuration, MimeMultipart activeMultipart, Exchange exchange) throws MessagingException, IOException { BodyPart bodyMessage = new MimeBodyPart(); populateContentOnBodyPart(bodyMessage, configuration, exchange); activeMultipart.addBodyPart(bodyMessage); }
From source file:org.apache.hupa.server.handler.AbstractSendMessageHandler.java
/** * Fill the body of a message already created. * The result message depends on the information given. * //ww w . jav a 2 s . c om * @param message * @param text * @param html * @param parts * @return The composed message * @throws MessagingException * @throws IOException */ @SuppressWarnings("rawtypes") public static Message composeMessage(Message message, String text, String html, List parts) throws MessagingException, IOException { MimeBodyPart txtPart = null; MimeBodyPart htmlPart = null; MimeMultipart mimeMultipart = null; if (text == null && html == null) { text = ""; } if (text != null) { txtPart = new MimeBodyPart(); txtPart.setContent(text, "text/plain"); } if (html != null) { htmlPart = new MimeBodyPart(); htmlPart.setContent(html, "text/html"); } if (html != null && text != null) { mimeMultipart = new MimeMultipart(); mimeMultipart.setSubType("alternative"); mimeMultipart.addBodyPart(txtPart); mimeMultipart.addBodyPart(htmlPart); } if (parts == null || parts.isEmpty()) { if (mimeMultipart != null) { message.setContent(mimeMultipart); } else if (html != null) { message.setText(html); message.setHeader("Content-type", "text/html"); } else if (text != null) { message.setText(text); } } else { MimeBodyPart bodyPart = new MimeBodyPart(); if (mimeMultipart != null) { bodyPart.setContent(mimeMultipart); } else if (html != null) { bodyPart.setText(html); bodyPart.setHeader("Content-type", "text/html"); } else if (text != null) { bodyPart.setText(text); } Multipart multipart = new MimeMultipart(); multipart.addBodyPart(bodyPart); for (Object attachment : parts) { if (attachment instanceof FileItem) { multipart.addBodyPart(MessageUtils.fileitemToBodypart((FileItem) attachment)); } else { multipart.addBodyPart((BodyPart) attachment); } } message.setContent(multipart); } message.saveChanges(); return message; }
From source file:org.apache.hupa.server.service.SendMessageBaseServiceImpl.java
/** * Fill the body of a message already created. * The result message depends on the information given. * * @param message//from www . j a v a 2 s. c o m * @param text * @param html * @param parts * @return The composed message * @throws MessagingException * @throws IOException */ @SuppressWarnings("rawtypes") public static Message composeMessage(Message message, String text, String html, List parts) throws MessagingException, IOException { MimeBodyPart txtPart = null; MimeBodyPart htmlPart = null; MimeMultipart mimeMultipart = null; if (text == null && html == null) { text = ""; } if (text != null) { txtPart = new MimeBodyPart(); txtPart.setContent(text, "text/plain; charset=UTF-8"); } if (html != null) { htmlPart = new MimeBodyPart(); htmlPart.setContent(html, "text/html; charset=UTF-8"); } if (html != null && text != null) { mimeMultipart = new MimeMultipart(); mimeMultipart.setSubType("alternative"); mimeMultipart.addBodyPart(txtPart); mimeMultipart.addBodyPart(htmlPart); } if (parts == null || parts.isEmpty()) { if (mimeMultipart != null) { message.setContent(mimeMultipart); } else if (html != null) { message.setText(html); message.setHeader("Content-type", "text/html"); } else if (text != null) { message.setText(text); } } else { MimeBodyPart bodyPart = new MimeBodyPart(); if (mimeMultipart != null) { bodyPart.setContent(mimeMultipart); } else if (html != null) { bodyPart.setText(html); bodyPart.setHeader("Content-type", "text/html"); } else if (text != null) { bodyPart.setText(text); } Multipart multipart = new MimeMultipart(); multipart.addBodyPart(bodyPart); for (Object attachment : parts) { if (attachment instanceof FileItem) { multipart.addBodyPart(MessageUtils.fileitemToBodypart((FileItem) attachment)); } else { multipart.addBodyPart((BodyPart) attachment); } } message.setContent(multipart); } message.saveChanges(); return message; }
From source file:org.apache.james.transport.mailets.managesieve.ManageSieveMailetTestCase.java
private MimeMessage prepareMessageWithAttachment(String scriptContent, String subject) throws MessagingException, IOException { MimeMessage message = prepareMimeMessage(subject); MimeMultipart multipart = new MimeMultipart(); MimeBodyPart scriptPart = new MimeBodyPart(); scriptPart.setDataHandler(// ww w. j a v a 2s .c o m new DataHandler(new ByteArrayDataSource(scriptContent, "application/sieve; charset=UTF-8"))); scriptPart.setDisposition(MimeBodyPart.ATTACHMENT); // setting a DataHandler with no mailcap definition is not // supported by the specs. Javamail activation still work, // but Geronimo activation translate it to text/plain. // Let's manually force the header. scriptPart.setHeader("Content-Type", "application/sieve; charset=UTF-8"); scriptPart.setFileName(SCRIPT_NAME); multipart.addBodyPart(scriptPart); message.setContent(multipart); message.saveChanges(); return message; }
From source file:org.apache.james.transport.mailets.StripAttachmentTest.java
@Test public void testSimpleAttachment() throws MessagingException, IOException { Mailet mailet = initMailet();/*from w ww . j a v a 2s. c om*/ MimeMessage message = new MimeMessage(Session.getDefaultInstance(new Properties())); MimeMultipart mm = new MimeMultipart(); MimeBodyPart mp = new MimeBodyPart(); mp.setText("simple text"); mm.addBodyPart(mp); String body = "\u0023\u00A4\u00E3\u00E0\u00E9"; MimeBodyPart mp2 = new MimeBodyPart(new ByteArrayInputStream( ("Content-Transfer-Encoding: 8bit\r\nContent-Type: application/octet-stream; charset=utf-8\r\n\r\n" + body).getBytes("UTF-8"))); mp2.setDisposition("attachment"); mp2.setFileName("10.tmp"); mm.addBodyPart(mp2); String body2 = "\u0014\u00A3\u00E1\u00E2\u00E4"; MimeBodyPart mp3 = new MimeBodyPart(new ByteArrayInputStream( ("Content-Transfer-Encoding: 8bit\r\nContent-Type: application/octet-stream; charset=utf-8\r\n\r\n" + body2).getBytes("UTF-8"))); mp3.setDisposition("attachment"); mp3.setFileName("temp.zip"); mm.addBodyPart(mp3); message.setSubject("test"); message.setContent(mm); message.saveChanges(); Mail mail = FakeMail.builder().mimeMessage(message).build(); mailet.service(mail); ByteArrayOutputStream rawMessage = new ByteArrayOutputStream(); mail.getMessage().writeTo(rawMessage, new String[] { "Bcc", "Content-Length", "Message-ID" }); @SuppressWarnings("unchecked") Collection<String> c = (Collection<String>) mail .getAttribute(StripAttachment.SAVED_ATTACHMENTS_ATTRIBUTE_KEY); Assert.assertNotNull(c); Assert.assertEquals(1, c.size()); String name = c.iterator().next(); File f = new File("./" + name); try { InputStream is = new FileInputStream(f); String savedFile = toString(is); is.close(); Assert.assertEquals(body, savedFile); } finally { FileUtils.deleteQuietly(f); } }
From source file:org.apache.james.transport.mailets.StripAttachmentTest.java
@Test public void testSimpleAttachment2() throws MessagingException, IOException { Mailet mailet = new StripAttachment(); FakeMailetConfig mci = new FakeMailetConfig("Test", FakeMailContext.defaultContext()); mci.setProperty("directory", "./"); mci.setProperty("remove", "all"); mci.setProperty("notpattern", "^(winmail\\.dat$)"); mailet.init(mci);/*from w w w . ja v a 2 s. co m*/ MimeMessage message = new MimeMessage(Session.getDefaultInstance(new Properties())); MimeMultipart mm = new MimeMultipart(); MimeBodyPart mp = new MimeBodyPart(); mp.setText("simple text"); mm.addBodyPart(mp); String body = "\u0023\u00A4\u00E3\u00E0\u00E9"; MimeBodyPart mp2 = new MimeBodyPart( new ByteArrayInputStream(("Content-Transfer-Encoding: 8bit\r\n\r\n" + body).getBytes("UTF-8"))); mp2.setDisposition("attachment"); mp2.setFileName("temp.tmp"); mm.addBodyPart(mp2); String body2 = "\u0014\u00A3\u00E1\u00E2\u00E4"; MimeBodyPart mp3 = new MimeBodyPart( new ByteArrayInputStream(("Content-Transfer-Encoding: 8bit\r\n\r\n" + body2).getBytes("UTF-8"))); mp3.setDisposition("attachment"); mp3.setFileName("winmail.dat"); mm.addBodyPart(mp3); message.setSubject("test"); message.setContent(mm); message.saveChanges(); Mail mail = FakeMail.builder().mimeMessage(message).build(); mailet.service(mail); ByteArrayOutputStream rawMessage = new ByteArrayOutputStream(); mail.getMessage().writeTo(rawMessage, new String[] { "Bcc", "Content-Length", "Message-ID" }); // String res = rawMessage.toString(); @SuppressWarnings("unchecked") Collection<String> c = (Collection<String>) mail .getAttribute(StripAttachment.SAVED_ATTACHMENTS_ATTRIBUTE_KEY); Assert.assertNotNull(c); Assert.assertEquals(1, c.size()); String name = c.iterator().next(); File f = new File("./" + name); try { InputStream is = new FileInputStream(f); String savedFile = toString(is); is.close(); Assert.assertEquals(body, savedFile); } finally { FileUtils.deleteQuietly(f); } }
From source file:org.apache.james.transport.mailets.StripAttachmentTest.java
@Test public void testSimpleAttachment3() throws MessagingException, IOException { Mailet mailet = initMailet();//ww w . j a v a 2s . co m // System.setProperty("mail.mime.decodefilename", "true"); MimeMessage message = new MimeMessage(Session.getDefaultInstance(new Properties())); MimeMultipart mm = new MimeMultipart(); MimeBodyPart mp = new MimeBodyPart(); mp.setText("simple text"); mm.addBodyPart(mp); String body = "\u0023\u00A4\u00E3\u00E0\u00E9"; MimeBodyPart mp2 = new MimeBodyPart( new ByteArrayInputStream(("Content-Transfer-Encoding: 8bit\r\n\r\n" + body).getBytes("UTF-8"))); mp2.setDisposition("attachment"); mp2.setFileName("=?iso-8859-15?Q?=E9_++++Pubblicit=E0_=E9_vietata____Milano9052.tmp?="); mm.addBodyPart(mp2); String body2 = "\u0014\u00A3\u00E1\u00E2\u00E4"; MimeBodyPart mp3 = new MimeBodyPart( new ByteArrayInputStream(("Content-Transfer-Encoding: 8bit\r\n\r\n" + body2).getBytes("UTF-8"))); mp3.setDisposition("attachment"); mp3.setFileName("temp.zip"); mm.addBodyPart(mp3); message.setSubject("test"); message.setContent(mm); message.saveChanges(); // message.writeTo(System.out); // System.out.println("--------------------------\n\n\n"); Mail mail = FakeMail.builder().mimeMessage(message).build(); mailet.service(mail); ByteArrayOutputStream rawMessage = new ByteArrayOutputStream(); mail.getMessage().writeTo(rawMessage, new String[] { "Bcc", "Content-Length", "Message-ID" }); // String res = rawMessage.toString(); @SuppressWarnings("unchecked") Collection<String> c = (Collection<String>) mail .getAttribute(StripAttachment.SAVED_ATTACHMENTS_ATTRIBUTE_KEY); Assert.assertNotNull(c); Assert.assertEquals(1, c.size()); String name = c.iterator().next(); // System.out.println("--------------------------\n\n\n"); // System.out.println(name); Assert.assertTrue(name.startsWith("e_Pubblicita_e_vietata_Milano9052")); File f = new File("./" + name); try { InputStream is = new FileInputStream(f); String savedFile = toString(is); is.close(); Assert.assertEquals(body, savedFile); } finally { FileUtils.deleteQuietly(f); } }
From source file:org.apache.james.transport.mailets.StripAttachmentTest.java
@Test public void testToAndFromAttributes() throws MessagingException, IOException { Mailet strip = new StripAttachment(); FakeMailetConfig mci = new FakeMailetConfig("Test", FakeMailContext.defaultContext()); mci.setProperty("attribute", "my.attribute"); mci.setProperty("remove", "all"); mci.setProperty("notpattern", ".*\\.tmp.*"); strip.init(mci);/*from w w w . j a v a2 s .c o m*/ Mailet recover = new RecoverAttachment(); FakeMailetConfig mci2 = new FakeMailetConfig("Test", FakeMailContext.defaultContext()); mci2.setProperty("attribute", "my.attribute"); recover.init(mci2); Mailet onlyText = new OnlyText(); onlyText.init(new FakeMailetConfig("Test", FakeMailContext.defaultContext())); MimeMessage message = new MimeMessage(Session.getDefaultInstance(new Properties())); MimeMultipart mm = new MimeMultipart(); MimeBodyPart mp = new MimeBodyPart(); mp.setText("simple text"); mm.addBodyPart(mp); String body = "\u0023\u00A4\u00E3\u00E0\u00E9"; MimeBodyPart mp2 = new MimeBodyPart(new ByteArrayInputStream( ("Content-Transfer-Encoding: 8bit\r\nContent-Type: application/octet-stream; charset=utf-8\r\n\r\n" + body).getBytes("UTF-8"))); mp2.setDisposition("attachment"); mp2.setFileName("=?iso-8859-15?Q?=E9_++++Pubblicit=E0_=E9_vietata____Milano9052.tmp?="); mm.addBodyPart(mp2); String body2 = "\u0014\u00A3\u00E1\u00E2\u00E4"; MimeBodyPart mp3 = new MimeBodyPart(new ByteArrayInputStream( ("Content-Transfer-Encoding: 8bit\r\nContent-Type: application/octet-stream; charset=utf-8\r\n\r\n" + body2).getBytes("UTF-8"))); mp3.setDisposition("attachment"); mp3.setFileName("temp.zip"); mm.addBodyPart(mp3); message.setSubject("test"); message.setContent(mm); message.saveChanges(); Mail mail = FakeMail.builder().mimeMessage(message).build(); Assert.assertTrue(mail.getMessage().getContent() instanceof MimeMultipart); Assert.assertEquals(3, ((MimeMultipart) mail.getMessage().getContent()).getCount()); strip.service(mail); Assert.assertTrue(mail.getMessage().getContent() instanceof MimeMultipart); Assert.assertEquals(1, ((MimeMultipart) mail.getMessage().getContent()).getCount()); onlyText.service(mail); Assert.assertFalse(mail.getMessage().getContent() instanceof MimeMultipart); Assert.assertEquals("simple text", mail.getMessage().getContent()); // prova per caricare il mime message da input stream che altrimenti // javamail si comporta differentemente. String mimeSource = "Message-ID: <26194423.21197328775426.JavaMail.bago@bagovista>\r\nSubject: test\r\nMIME-Version: 1.0\r\nContent-Type: text/plain; charset=us-ascii\r\nContent-Transfer-Encoding: 7bit\r\n\r\nsimple text"; MimeMessage mmNew = new MimeMessage(Session.getDefaultInstance(new Properties()), new ByteArrayInputStream(mimeSource.getBytes("UTF-8"))); mmNew.writeTo(System.out); mail.setMessage(mmNew); recover.service(mail); Assert.assertTrue(mail.getMessage().getContent() instanceof MimeMultipart); Assert.assertEquals(2, ((MimeMultipart) mail.getMessage().getContent()).getCount()); Object actual = ((MimeMultipart) mail.getMessage().getContent()).getBodyPart(1).getContent(); if (actual instanceof ByteArrayInputStream) { Assert.assertEquals(body2, toString((ByteArrayInputStream) actual)); } else { Assert.assertEquals(body2, actual); } }
From source file:org.apache.jsieve.mailet.SieveMailboxMailet.java
/** * Deliver the original mail as an attachment with the main part being an error report. * * @param recipient//from w w w .j a v a2 s .c o m * @param aMail * @param ex * @throws MessagingException * @throws IOException */ protected void handleFailure(MailAddress recipient, Mail aMail, Exception ex) throws MessagingException, IOException { String user = getUsername(recipient); MimeMessage originalMessage = aMail.getMessage(); MimeMessage message = new MimeMessage(originalMessage); MimeMultipart multipart = new MimeMultipart(); MimeBodyPart noticePart = new MimeBodyPart(); noticePart.setText(new StringBuilder().append( "An error was encountered while processing this mail with the active sieve script for user \"") .append(user).append("\". The error encountered was:\r\n").append(ex.getLocalizedMessage()) .append("\r\n").toString()); multipart.addBodyPart(noticePart); MimeBodyPart originalPart = new MimeBodyPart(); originalPart.setContent(originalMessage, "message/rfc822"); if ((originalMessage.getSubject() != null) && (!originalMessage.getSubject().trim().isEmpty())) { originalPart.setFileName(originalMessage.getSubject().trim()); } else { originalPart.setFileName("No Subject"); } originalPart.setDisposition(MimeBodyPart.INLINE); multipart.addBodyPart(originalPart); message.setContent(multipart); message.setSubject("[SIEVE ERROR] " + originalMessage.getSubject()); message.setHeader("X-Priority", "1"); message.saveChanges(); storeMessageInbox(user, message); }
From source file:org.apache.nifi.processors.standard.PutEmail.java
@Override public void onTrigger(final ProcessContext context, final ProcessSession session) { final FlowFile flowFile = session.get(); if (flowFile == null) { return;//from ww w.ja v a 2s. co m } final Properties properties = this.getMailPropertiesFromFlowFile(context, flowFile); final Session mailSession = this.createMailSession(properties); final Message message = new MimeMessage(mailSession); final ComponentLog logger = getLogger(); try { message.addFrom(toInetAddresses(context, flowFile, FROM)); message.setRecipients(RecipientType.TO, toInetAddresses(context, flowFile, TO)); message.setRecipients(RecipientType.CC, toInetAddresses(context, flowFile, CC)); message.setRecipients(RecipientType.BCC, toInetAddresses(context, flowFile, BCC)); message.setHeader("X-Mailer", context.getProperty(HEADER_XMAILER).evaluateAttributeExpressions(flowFile).getValue()); message.setSubject(context.getProperty(SUBJECT).evaluateAttributeExpressions(flowFile).getValue()); String messageText = context.getProperty(MESSAGE).evaluateAttributeExpressions(flowFile).getValue(); if (context.getProperty(INCLUDE_ALL_ATTRIBUTES).asBoolean()) { messageText = formatAttributes(flowFile, messageText); } String contentType = context.getProperty(CONTENT_TYPE).evaluateAttributeExpressions(flowFile) .getValue(); message.setContent(messageText, contentType); message.setSentDate(new Date()); if (context.getProperty(ATTACH_FILE).asBoolean()) { final MimeBodyPart mimeText = new PreencodedMimeBodyPart("base64"); mimeText.setDataHandler(new DataHandler(new ByteArrayDataSource( Base64.encodeBase64(messageText.getBytes("UTF-8")), contentType + "; charset=\"utf-8\""))); final MimeBodyPart mimeFile = new MimeBodyPart(); session.read(flowFile, new InputStreamCallback() { @Override public void process(final InputStream stream) throws IOException { try { mimeFile.setDataHandler( new DataHandler(new ByteArrayDataSource(stream, "application/octet-stream"))); } catch (final Exception e) { throw new IOException(e); } } }); mimeFile.setFileName(flowFile.getAttribute(CoreAttributes.FILENAME.key())); MimeMultipart multipart = new MimeMultipart(); multipart.addBodyPart(mimeText); multipart.addBodyPart(mimeFile); message.setContent(multipart); } send(message); session.getProvenanceReporter().send(flowFile, "mailto:" + message.getAllRecipients()[0].toString()); session.transfer(flowFile, REL_SUCCESS); logger.info("Sent email as a result of receiving {}", new Object[] { flowFile }); } catch (final ProcessException | MessagingException | IOException e) { context.yield(); logger.error("Failed to send email for {}: {}; routing to failure", new Object[] { flowFile, e.getMessage() }, e); session.transfer(flowFile, REL_FAILURE); } }