Java Code Examples for javax.mail.internet.MimeMessage#getContent()
The following examples show how to use
javax.mail.internet.MimeMessage#getContent() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MailServiceIntegrationTest.java From tutorials with MIT License | 6 votes |
@Test public void testSendMultipartEmail() throws Exception { mailService.sendEmail("[email protected]", "testSubject", "testContent", true, false); verify(javaMailSender).send(messageCaptor.capture()); MimeMessage message = messageCaptor.getValue(); MimeMultipart mp = (MimeMultipart) message.getContent(); MimeBodyPart part = (MimeBodyPart) ((MimeMultipart) mp.getBodyPart(0).getContent()).getBodyPart(0); ByteArrayOutputStream aos = new ByteArrayOutputStream(); part.writeTo(aos); assertThat(message.getSubject()).isEqualTo("testSubject"); assertThat(message.getAllRecipients()[0].toString()).isEqualTo("[email protected]"); assertThat(message.getFrom()[0].toString()).isEqualTo("test@localhost"); assertThat(message.getContent()).isInstanceOf(Multipart.class); assertThat(aos.toString()).isEqualTo("\r\ntestContent"); assertThat(part.getDataHandler().getContentType()).isEqualTo("text/plain; charset=UTF-8"); }
Example 2
Source File: MailServiceIntTest.java From cubeai with Apache License 2.0 | 6 votes |
@Test public void testSendMultipartEmail() throws Exception { mailService.sendEmail("[email protected]", "testSubject", "testContent", true, false); verify(javaMailSender).send((MimeMessage) messageCaptor.capture()); MimeMessage message = (MimeMessage) messageCaptor.getValue(); MimeMultipart mp = (MimeMultipart) message.getContent(); MimeBodyPart part = (MimeBodyPart) ((MimeMultipart) mp.getBodyPart(0).getContent()).getBodyPart(0); ByteArrayOutputStream aos = new ByteArrayOutputStream(); part.writeTo(aos); assertThat(message.getSubject()).isEqualTo("testSubject"); assertThat(message.getAllRecipients()[0].toString()).isEqualTo("[email protected]"); assertThat(message.getFrom()[0].toString()).isEqualTo("test@localhost"); assertThat(message.getContent()).isInstanceOf(Multipart.class); assertThat(aos.toString()).isEqualTo("\r\ntestContent"); assertThat(part.getDataHandler().getContentType()).isEqualTo("text/plain; charset=UTF-8"); }
Example 3
Source File: MailServiceIntegrationTest.java From tutorials with MIT License | 6 votes |
@Test public void testSendMultipartHtmlEmail() throws Exception { mailService.sendEmail("[email protected]", "testSubject", "testContent", true, true); verify(javaMailSender).send(messageCaptor.capture()); MimeMessage message = messageCaptor.getValue(); MimeMultipart mp = (MimeMultipart) message.getContent(); MimeBodyPart part = (MimeBodyPart) ((MimeMultipart) mp.getBodyPart(0).getContent()).getBodyPart(0); ByteArrayOutputStream aos = new ByteArrayOutputStream(); part.writeTo(aos); assertThat(message.getSubject()).isEqualTo("testSubject"); assertThat(message.getAllRecipients()[0].toString()).isEqualTo("[email protected]"); assertThat(message.getFrom()[0].toString()).isEqualTo("test@localhost"); assertThat(message.getContent()).isInstanceOf(Multipart.class); assertThat(aos.toString()).isEqualTo("\r\ntestContent"); assertThat(part.getDataHandler().getContentType()).isEqualTo("text/html;charset=UTF-8"); }
Example 4
Source File: MailServiceIntTest.java From TeamDojo with Apache License 2.0 | 6 votes |
@Test public void testSendMultipartEmail() throws Exception { mailService.sendEmail("[email protected]", "testSubject", "testContent", true, false); verify(javaMailSender).send((MimeMessage) messageCaptor.capture()); MimeMessage message = (MimeMessage) messageCaptor.getValue(); MimeMultipart mp = (MimeMultipart) message.getContent(); MimeBodyPart part = (MimeBodyPart) ((MimeMultipart) mp.getBodyPart(0).getContent()).getBodyPart(0); ByteArrayOutputStream aos = new ByteArrayOutputStream(); part.writeTo(aos); assertThat(message.getSubject()).isEqualTo("testSubject"); assertThat(message.getAllRecipients()[0].toString()).isEqualTo("[email protected]"); assertThat(message.getFrom()[0].toString()).isEqualTo("test@localhost"); assertThat(message.getContent()).isInstanceOf(Multipart.class); assertThat(aos.toString()).isEqualTo("\r\ntestContent"); assertThat(part.getDataHandler().getContentType()).isEqualTo("text/plain; charset=UTF-8"); }
Example 5
Source File: MailServiceIntTest.java From TeamDojo with Apache License 2.0 | 6 votes |
@Test public void testSendMultipartHtmlEmail() throws Exception { mailService.sendEmail("[email protected]", "testSubject", "testContent", true, true); verify(javaMailSender).send((MimeMessage) messageCaptor.capture()); MimeMessage message = (MimeMessage) messageCaptor.getValue(); MimeMultipart mp = (MimeMultipart) message.getContent(); MimeBodyPart part = (MimeBodyPart) ((MimeMultipart) mp.getBodyPart(0).getContent()).getBodyPart(0); ByteArrayOutputStream aos = new ByteArrayOutputStream(); part.writeTo(aos); assertThat(message.getSubject()).isEqualTo("testSubject"); assertThat(message.getAllRecipients()[0].toString()).isEqualTo("[email protected]"); assertThat(message.getFrom()[0].toString()).isEqualTo("test@localhost"); assertThat(message.getContent()).isInstanceOf(Multipart.class); assertThat(aos.toString()).isEqualTo("\r\ntestContent"); assertThat(part.getDataHandler().getContentType()).isEqualTo("text/html;charset=UTF-8"); }
Example 6
Source File: Converter7BitTest.java From james-project with Apache License 2.0 | 6 votes |
@Test void convertTo7BitShouldKeepAttachmentPartUnTouchedWhenBase64Encoding() throws Exception { MimeMessage mimeMessage = MimeMessageUtil.mimeMessageFromString( fileContent("eml/multipart-8bit.eml")); testee.convertTo7Bit(mimeMessage); MimeMultipart multipart = (MimeMultipart) mimeMessage.getContent(); String messageAsString = MimeMessageUtil.asString(mimeMessage); SoftAssertions.assertSoftly(Throwing.consumer(softly -> { softly.assertThat(multipart.getBodyPart(1).getHeader(CONTENT_TRANSFER_ENCODING)) .containsOnly(BASE64); softly.assertThat(messageAsString) .contains(fileContent("eml/multipart-8bit-attachment-content.txt")); })); }
Example 7
Source File: Converter7BitTest.java From james-project with Apache License 2.0 | 6 votes |
@Test void convertTo7BitShouldKeepMessageAttachmentsContentUnTouched() throws Exception { MimeMessage mimeMessage = MimeMessageUtil.mimeMessageFromString( fileContent("eml/multipart-7bit.eml")); testee.convertTo7Bit(mimeMessage); MimeMultipart multipart = (MimeMultipart) mimeMessage.getContent(); String messageAsString = MimeMessageUtil.asString(mimeMessage); SoftAssertions.assertSoftly(Throwing.consumer(softly -> { softly.assertThat(multipart.getBodyPart(1).getHeader(CONTENT_TRANSFER_ENCODING)) .containsOnly(BASE64); softly.assertThat(messageAsString) .contains(fileContent("eml/multipart-7bit-attachment-content.txt")); })); }
Example 8
Source File: SendMailConnectorTest.java From camunda-bpm-mail with Apache License 2.0 | 6 votes |
@Test public void htmlMessage() throws Exception { MailConnectors.sendMail() .createRequest() .from("test") .to("[email protected]") .subject("subject") .text("test") .html("<b>test</b>") .execute(); MimeMessage[] mails = greenMail.getReceivedMessages(); MimeMessage mail = mails[0]; assertThat(mail.getContent()).isInstanceOf(MimeMultipart.class); MimeMultipart multiPart = (MimeMultipart) mail.getContent(); assertThat(multiPart.getCount()).isEqualTo(2); assertThat(GreenMailUtil.getBody(multiPart.getBodyPart(0))).isEqualTo("test"); assertThat(GreenMailUtil.getBody(multiPart.getBodyPart(1))).isEqualTo("<b>test</b>"); }
Example 9
Source File: MailServiceIntTest.java From e-commerce-microservice with Apache License 2.0 | 6 votes |
@Test public void testSendMultipartHtmlEmail() throws Exception { mailService.sendEmail("[email protected]", "testSubject", "testContent", true, true); verify(javaMailSender).send(messageCaptor.capture()); MimeMessage message = messageCaptor.getValue(); MimeMultipart mp = (MimeMultipart) message.getContent(); MimeBodyPart part = (MimeBodyPart) ((MimeMultipart) mp.getBodyPart(0).getContent()).getBodyPart(0); ByteArrayOutputStream aos = new ByteArrayOutputStream(); part.writeTo(aos); assertThat(message.getSubject()).isEqualTo("testSubject"); assertThat(message.getAllRecipients()[0].toString()).isEqualTo("[email protected]"); assertThat(message.getFrom()[0].toString()).isEqualTo("test@localhost"); assertThat(message.getContent()).isInstanceOf(Multipart.class); assertThat(aos.toString()).isEqualTo("\r\ntestContent"); assertThat(part.getDataHandler().getContentType()).isEqualTo("text/html;charset=UTF-8"); }
Example 10
Source File: TextCalendarBodyToAttachmentTest.java From james-project with Apache License 2.0 | 6 votes |
@Test public void serviceShouldConvertTextBodyOfMessageToAttachmentWhenTextCalendar() throws Exception { String messageContent = "Content-type: text/calendar; method=REPLY; charset=UTF-8\n" + "Content-transfer-encoding: 8BIT\n" + "\n" + "BEGIN:VCALENDAR\n" + "END:VEVENT\n" + "END:VCALENDAR"; MimeMessage message = MimeMessageUtil.mimeMessageFromString(messageContent); Mail mail = FakeMail.builder() .name("name") .mimeMessage(message) .build(); mailet.service(mail); MimeMessage actual = mail.getMessage(); Multipart multipart = (Multipart)actual.getContent(); assertThat(multipart.getCount()).isEqualTo(1); assertThat(multipart.getBodyPart(0).getDisposition()).isEqualTo("attachment"); }
Example 11
Source File: SendMailConnectorProcessTest.java From camunda-bpm-mail with Apache License 2.0 | 6 votes |
@Test @Deployment(resources = "processes/mail-send-file.bpmn") public void sendMailWithFileName() throws Exception { File attachment = new File(getClass().getResource("/attachment.txt").toURI()); assertThat(attachment.exists()).isTrue(); engineRule.getRuntimeService().startProcessInstanceByKey("send-mail", Variables.createVariables().putValue("file", attachment.getPath())); MimeMessage[] mails = greenMail.getReceivedMessages(); assertThat(mails).hasSize(1); MimeMessage mail = mails[0]; assertThat(mail.getContent()).isInstanceOf(MimeMultipart.class); MimeMultipart multiPart = (MimeMultipart) mail.getContent(); assertThat(multiPart.getCount()).isEqualTo(1); assertThat(GreenMailUtil.getBody(multiPart.getBodyPart(0))).isEqualTo("plain text"); }
Example 12
Source File: ImapConnection.java From davmail with GNU General Public License v2.0 | 6 votes |
protected void appendBodyStructure(StringBuilder buffer, MessageWrapper message) throws IOException, InterruptedException { buffer.append(" BODYSTRUCTURE "); try { MimeMessage mimeMessage = message.getMimeMessage(); Object mimeBody = mimeMessage.getContent(); if (mimeBody instanceof MimeMultipart) { appendBodyStructure(buffer, (MimeMultipart) mimeBody); } else { // no multipart, single body appendBodyStructure(buffer, mimeMessage); } } catch (UnsupportedEncodingException | MessagingException e) { DavGatewayTray.warn(e); // failover: send default bodystructure buffer.append("(\"TEXT\" \"PLAIN\" (\"CHARSET\" \"US-ASCII\") NIL NIL NIL NIL NIL)"); } }
Example 13
Source File: MailServiceIntTest.java From ehcache3-samples with Apache License 2.0 | 6 votes |
@Test public void testSendMultipartHtmlEmail() throws Exception { mailService.sendEmail("[email protected]", "testSubject", "testContent", true, true); verify(javaMailSender).send(messageCaptor.capture()); MimeMessage message = messageCaptor.getValue(); MimeMultipart mp = (MimeMultipart) message.getContent(); MimeBodyPart part = (MimeBodyPart) ((MimeMultipart) mp.getBodyPart(0).getContent()).getBodyPart(0); ByteArrayOutputStream aos = new ByteArrayOutputStream(); part.writeTo(aos); assertThat(message.getSubject()).isEqualTo("testSubject"); assertThat(message.getAllRecipients()[0].toString()).isEqualTo("[email protected]"); assertThat(message.getFrom()[0].toString()).isEqualTo("test@localhost"); assertThat(message.getContent()).isInstanceOf(Multipart.class); assertThat(aos.toString()).isEqualTo("\r\ntestContent"); assertThat(part.getDataHandler().getContentType()).isEqualTo("text/html;charset=UTF-8"); }
Example 14
Source File: DSNBounceTest.java From james-project with Apache License 2.0 | 5 votes |
@Test public void serviceShouldAttachTheOriginalMailWhenAttachmentIsEqualToAll() throws Exception { FakeMailetConfig mailetConfig = FakeMailetConfig.builder() .mailetName(MAILET_NAME) .mailetContext(fakeMailContext) .setProperty("attachment", "all") .build(); dsnBounce.init(mailetConfig); MailAddress senderMailAddress = new MailAddress("[email protected]"); MimeMessage mimeMessage = MimeMessageBuilder.mimeMessageBuilder() .setText("My content") .build(); FakeMail mail = FakeMail.builder() .name(MAILET_NAME) .sender(senderMailAddress) .recipient("[email protected]") .lastUpdated(Date.from(Instant.parse("2016-09-08T14:25:52.000Z"))) .mimeMessage(mimeMessage) .build(); MimeMessage mimeMessageCopy = new MimeMessage(mimeMessage); dsnBounce.service(mail); List<SentMail> sentMails = fakeMailContext.getSentMails(); assertThat(sentMails).hasSize(1); SentMail sentMail = sentMails.get(0); assertThat(sentMail.getSender()).isNull(); assertThat(sentMail.getRecipients()).containsOnly(senderMailAddress); MimeMessage sentMessage = sentMail.getMsg(); MimeMultipart content = (MimeMultipart) sentMessage.getContent(); assertThat(sentMail.getMsg().getContentType()).startsWith("multipart/report;"); assertThat(MimeMessageUtil.asString((MimeMessage) content.getBodyPart(2).getContent())) .isEqualTo(MimeMessageUtil.asString(mimeMessageCopy)); }
Example 15
Source File: DataSourceProviderTest.java From cxf with Apache License 2.0 | 5 votes |
public static MimeMultipart readAttachmentParts(String contentType, InputStream bais) throws MessagingException, IOException { DataSource source = new ByteArrayDataSource(bais, contentType); MimeMultipart mpart = new MimeMultipart(source); Session session = Session.getDefaultInstance(new Properties()); MimeMessage mm = new MimeMessage(session); mm.setContent(mpart); mm.addHeaderLine("Content-Type:" + contentType); return (MimeMultipart) mm.getContent(); }
Example 16
Source File: ZhilianEmailResumeParser.java From job with MIT License | 5 votes |
protected Document parse2Html(File file) throws Exception { InputStream in = new FileInputStream(file); Session mailSession = Session.getDefaultInstance(System.getProperties(), null); MimeMessage msg = new MimeMessage(mailSession, in); Multipart part = (Multipart) msg.getContent(); String html = ""; for(int i = 0; i < part.getCount(); i++) { BodyPart body = part.getBodyPart(i); String type = body.getContentType(); if(type.startsWith("text/html")) { html = body.getContent().toString(); break; } } in.close(); if(html == null || html.length() == 0) { String content = FileUtils.readFileToString(file); final String endFlag = "</html>"; int start = content.indexOf("<html"); int end = content.indexOf(endFlag); content = content.substring(start, end + endFlag.length()); html = QuotedPrintableUtils.decode(content.getBytes(), "gb2312"); System.err.println(html); } return Jsoup.parse(html); }
Example 17
Source File: DSNBounceTest.java From james-project with Apache License 2.0 | 5 votes |
@Test public void serviceShouldAttachTheOriginalMailHeadersOnlyWhenAttachmentIsEqualToHeads() throws Exception { FakeMailetConfig mailetConfig = FakeMailetConfig.builder() .mailetName(MAILET_NAME) .mailetContext(fakeMailContext) .setProperty("attachment", "heads") .build(); dsnBounce.init(mailetConfig); MailAddress senderMailAddress = new MailAddress("[email protected]"); FakeMail mail = FakeMail.builder() .name(MAILET_NAME) .sender(senderMailAddress) .mimeMessage(MimeMessageBuilder.mimeMessageBuilder() .setText("My content") .addHeader("myHeader", "myValue") .setSubject("mySubject")) .recipient("[email protected]") .lastUpdated(Date.from(Instant.parse("2016-09-08T14:25:52.000Z"))) .build(); dsnBounce.service(mail); List<SentMail> sentMails = fakeMailContext.getSentMails(); assertThat(sentMails).hasSize(1); SentMail sentMail = sentMails.get(0); assertThat(sentMail.getSender()).isNull(); assertThat(sentMail.getRecipients()).containsOnly(senderMailAddress); MimeMessage sentMessage = sentMail.getMsg(); MimeMultipart content = (MimeMultipart) sentMessage.getContent(); BodyPart bodyPart = content.getBodyPart(2); SharedByteArrayInputStream actualContent = (SharedByteArrayInputStream) bodyPart.getContent(); assertThat(IOUtils.toString(actualContent, StandardCharsets.UTF_8)) .contains("Subject: mySubject") .contains("myHeader: myValue"); assertThat(bodyPart.getContentType()).isEqualTo("text/rfc822-headers; name=mySubject"); }
Example 18
Source File: DSNBounceTest.java From james-project with Apache License 2.0 | 5 votes |
@Test public void serviceShouldSendMultipartMailContainingTextPartWhenCustomMessageIsConfigured() throws Exception { FakeMailetConfig mailetConfig = FakeMailetConfig.builder() .mailetName(MAILET_NAME) .mailetContext(fakeMailContext) .setProperty("messageString", "My custom message\n") .build(); dsnBounce.init(mailetConfig); MailAddress senderMailAddress = new MailAddress("[email protected]"); FakeMail mail = FakeMail.builder() .name(MAILET_NAME) .sender(senderMailAddress) .attribute(DELIVERY_ERROR_ATTRIBUTE) .mimeMessage(MimeMessageBuilder.mimeMessageBuilder() .setText("My content")) .recipient("[email protected]") .lastUpdated(Date.from(Instant.parse("2016-09-08T14:25:52.000Z"))) .build(); dsnBounce.service(mail); String expectedContent = "My custom message\n\n" + "Failed recipient(s):\n" + "[email protected]\n" + "\n" + "Error message:\n" + "Delivery error\n" + "\n"; List<SentMail> sentMails = fakeMailContext.getSentMails(); assertThat(sentMails).hasSize(1); SentMail sentMail = sentMails.get(0); MimeMessage sentMessage = sentMail.getMsg(); MimeMultipart content = (MimeMultipart) sentMessage.getContent(); BodyPart bodyPart = content.getBodyPart(0); assertThat(bodyPart.getContentType()).isEqualTo("text/plain; charset=us-ascii"); assertThat(bodyPart.getContent()).isEqualTo(expectedContent); }
Example 19
Source File: AttachmentSerializerTest.java From cxf with Apache License 2.0 | 4 votes |
@Test public void testMessageMTOM() throws Exception { MessageImpl msg = new MessageImpl(); Collection<Attachment> atts = new ArrayList<>(); AttachmentImpl a = new AttachmentImpl("test.xml"); InputStream is = getClass().getResourceAsStream("my.wav"); ByteArrayDataSource ds = new ByteArrayDataSource(is, "application/octet-stream"); a.setDataHandler(new DataHandler(ds)); atts.add(a); msg.setAttachments(atts); // Set the SOAP content type msg.put(Message.CONTENT_TYPE, "application/soap+xml"); ByteArrayOutputStream out = new ByteArrayOutputStream(); msg.setContent(OutputStream.class, out); AttachmentSerializer serializer = new AttachmentSerializer(msg); serializer.writeProlog(); String ct = (String) msg.get(Message.CONTENT_TYPE); assertTrue(ct.indexOf("multipart/related;") == 0); assertTrue(ct.indexOf("start=\"<[email protected]>\"") > -1); assertTrue(ct.indexOf("start-info=\"application/soap+xml\"") > -1); out.write("<soap:Body/>".getBytes()); serializer.writeAttachments(); out.flush(); DataSource source = new ByteArrayDataSource(new ByteArrayInputStream(out.toByteArray()), ct); MimeMultipart mpart = new MimeMultipart(source); Session session = Session.getDefaultInstance(new Properties()); MimeMessage inMsg = new MimeMessage(session); inMsg.setContent(mpart); inMsg.addHeaderLine("Content-Type: " + ct); MimeMultipart multipart = (MimeMultipart) inMsg.getContent(); MimeBodyPart part = (MimeBodyPart) multipart.getBodyPart(0); assertEquals("application/xop+xml; charset=UTF-8; type=\"application/soap+xml\"", part.getHeader("Content-Type")[0]); assertEquals("binary", part.getHeader("Content-Transfer-Encoding")[0]); assertEquals("<[email protected]>", part.getHeader("Content-ID")[0]); InputStream in = part.getDataHandler().getInputStream(); ByteArrayOutputStream bodyOut = new ByteArrayOutputStream(); IOUtils.copy(in, bodyOut); out.close(); in.close(); assertEquals("<soap:Body/>", bodyOut.toString()); MimeBodyPart part2 = (MimeBodyPart) multipart.getBodyPart(1); assertEquals("application/octet-stream", part2.getHeader("Content-Type")[0]); assertEquals("binary", part2.getHeader("Content-Transfer-Encoding")[0]); assertEquals("<test.xml>", part2.getHeader("Content-ID")[0]); }
Example 20
Source File: BasicEmailService.java From sakai with Educational Community License v2.0 | 4 votes |
protected void sendMessageAndLog(InternetAddress[] to, long start, MimeMessage msg, Session session) throws MessagingException { long preSend = 0; if (log.isDebugEnabled()) preSend = System.currentTimeMillis(); if (allowTransport) { msg.saveChanges(); transportMessage(session, getMessageSets(new ArrayList<>(Arrays.asList(to))), new ArrayList<>(), msg); } long end = 0; if (log.isDebugEnabled()) end = System.currentTimeMillis(); if (log.isInfoEnabled()) { StringBuilder buf = new StringBuilder(); buf.append("Email.sendMail:"); appendAddresses(buf, msg.getFrom(), " from:"); buf.append("subject: "); buf.append(msg.getSubject()); appendAddresses(buf, to, " to:"); appendAddresses(buf, msg.getRecipients(Message.RecipientType.TO), " headerTo{to}:"); appendAddresses(buf, msg.getRecipients(Message.RecipientType.CC), " headerTo{cc}:"); appendAddresses(buf, msg.getRecipients(Message.RecipientType.BCC), " headerTo{bcc}:"); appendAddresses(buf, msg.getReplyTo(), " replyTo:"); try { if (msg.getContent() instanceof Multipart) { Multipart parts = (Multipart) msg.getContent(); buf.append(" with ").append(parts.getCount() - 1).append(" attachments"); } } catch (IOException ioe) {} if (log.isDebugEnabled()) { buf.append(" time: "); buf.append("" + (end - start)); buf.append(" in send: "); buf.append("" + (end - preSend)); } log.info(buf.toString()); } }