List of usage examples for javax.mail.internet MimeMessage getContent
@Override public Object getContent() throws IOException, MessagingException
From source file:org.craftercms.commons.mail.impl.EmailFactoryImplTest.java
@Test public void testGetEmailWithBodyTemplate() throws Exception { Map<String, Object> model = Collections.<String, Object>singletonMap("name", "John Doe"); String body = processTemplate(TEMPLATE_NAME, model); EmailImpl email = (EmailImpl) emailFactory.getEmail(FROM, TO, CC, BCC, REPLY_TO, SUBJECT, TEMPLATE_NAME, model, false);/*from w w w .j a v a 2s .com*/ assertNotNull(email); MimeMessage msg = email.message; assertArrayEquals(InternetAddress.parse(FROM), msg.getFrom()); assertArrayEquals(InternetAddress.parse(StringUtils.join(TO)), msg.getRecipients(Message.RecipientType.TO)); assertArrayEquals(InternetAddress.parse(StringUtils.join(CC)), msg.getRecipients(Message.RecipientType.CC)); assertArrayEquals(InternetAddress.parse(StringUtils.join(BCC)), msg.getRecipients(Message.RecipientType.BCC)); assertArrayEquals(InternetAddress.parse(REPLY_TO), msg.getReplyTo()); assertEquals(SUBJECT, msg.getSubject()); assertEquals(body, msg.getContent()); }
From source file:org.devtoolbox.errorhandling.RouteFailureTest.java
public @Test void testRoute() throws Exception { Wiser wiser = new Wiser(); wiser.start();/*from w w w . j a v a 2s. co m*/ Thread.sleep(10000); //wait for route to execute Assert.assertFalse(new File(rootFolderPath + "/" + processingFolderPath + "/" + testFileName).exists()); Assert.assertTrue(new File(rootFolderPath + "/" + errorFolderPath + "/" + testFileName).exists()); List<WiserMessage> wiserMessageList = wiser.getMessages(); Assert.assertTrue(wiserMessageList.size() > 0); for (WiserMessage message : wiserMessageList) { String envelopeSender = message.getEnvelopeSender(); Assert.assertNotNull(envelopeSender); Assert.assertEquals("some@some.com", envelopeSender); String envelopeReceiver = message.getEnvelopeReceiver(); Assert.assertNotNull(envelopeReceiver); Assert.assertEquals("some@some.com", envelopeReceiver); MimeMessage mimeMessage = message.getMimeMessage(); Assert.assertNotNull(mimeMessage); Object content = mimeMessage.getContent(); Assert.assertEquals( "The processing of the file " + testFileName + " has failed. The file was moved to the " + rootFolderPath + "/" + errorFolderPath + " folder.", content.toString()); } wiser.stop(); }
From source file:org.drools.task.service.IcalBaseTest.java
public void testSendWithStartandEndDeadline() throws Exception { Map vars = new HashedMap(); vars.put("users", users); vars.put("groups", groups); vars.put("now", new Date()); String str = "(with (new Task()) { priority = 55, taskData = (with( new TaskData()) { createdOn = now, createdBy = users['tony'], activationTime = now}), "; str += "peopleAssignments = (with ( new PeopleAssignments() ) {potentialOwners = [users['steve' ], users['tony' ]]}), "; str += "names = [ new I18NText( 'en-UK', 'This is my task name')],"; str += "subjects = [ new I18NText( 'en-UK', 'This is my task subject')],"; str += "descriptions = [ new I18NText( 'en-UK', 'This is my task description')],"; str += "deadlines = (with (new Deadlines() ) {"; str += " startDeadlines = [ "; str += " (with (new Deadline()) {"; str += " date = now"; str += " } ) ],"; str += " endDeadlines = ["; str += " (with (new Deadline()) {"; str += " date = new Date( now.time + ( 1000 * 60 * 60 * 24 ) )"; // set to tomorrow str += " } ) ]"; str += "} ) })"; MockUserInfo userInfo = new MockUserInfo(); userInfo.getEmails().put(users.get("tony"), "tony@domain.com"); userInfo.getEmails().put(users.get("steve"), "steve@domain.com"); userInfo.getLanguages().put(users.get("tony"), "en-UK"); userInfo.getLanguages().put(users.get("steve"), "en-UK"); taskService.setUserinfo(userInfo);/*from ww w . j a va 2 s.co m*/ BlockingAddTaskResponseHandler addTaskResponseHandler = new BlockingAddTaskResponseHandler(); Task task = (Task) eval(new StringReader(str), vars); client.addTask(task, null, addTaskResponseHandler); long taskId = addTaskResponseHandler.getTaskId(); BlockingTaskOperationResponseHandler responseHandler = new BlockingTaskOperationResponseHandler(); client.claim(taskId, users.get("steve").getId(), responseHandler); responseHandler.waitTillDone(5000); assertEquals(2, getWiser().getMessages().size()); assertEquals("steve@domain.com", getWiser().getMessages().get(0).getEnvelopeReceiver()); assertEquals("steve@domain.com", getWiser().getMessages().get(1).getEnvelopeReceiver()); String subject = "Summary\n-------\n\nThis is my task subject\n\n"; String description = "Description\n-----------\n\nThis is my task description"; MimeMessage msg = ((WiserMessage) getWiser().getMessages().get(0)).getMimeMessage(); assertEqualsIgnoreWhitespace("multipart/alternative;boundary=\"----=_Part_", msg.getContentType(), 0, 47); assertEquals("tony@domain.com", ((InternetAddress) msg.getFrom()[0]).getAddress()); assertEquals("tony@domain.com", ((InternetAddress) msg.getReplyTo()[0]).getAddress()); assertEquals("steve@domain.com", ((InternetAddress) msg.getRecipients(RecipientType.TO)[0]).getAddress()); assertEquals("Task Assignment Start Event: This is my task name", msg.getSubject()); MimeMultipart multiPart = (MimeMultipart) msg.getContent(); BodyPart messageBodyPart = multiPart.getBodyPart(0); assertEquals("text/plain; charset=UTF8;", messageBodyPart.getDataHandler().getContentType()); String content = new String(getBytes(messageBodyPart.getDataHandler().getInputStream())); assertEqualsIgnoreWhitespace(subject + description, content); messageBodyPart = multiPart.getBodyPart(1); assertEquals("text/calendar; charset=UTF8; name=ical-Start-1.ics", messageBodyPart.getDataHandler().getContentType()); content = new String(getBytes(messageBodyPart.getDataHandler().getInputStream())); assertEqualsIgnoreWhitespace( "BEGIN:VCALENDARPRODID:-//iCal4j 1.0//ENCALSCALE:GREGORIANVERSION:2.0METHOD:REQUESTBEGIN:VEVENTDTSTART;TZID=UTC:", content.substring(0, 123)); assertEqualsIgnoreWhitespace( "SUMMARY:\"Task Start : This is my task subject\"DESCRIPTION:\"This is my task description\"PRIORITY:55END:VEVENTEND:VCALENDAR", content.substring(content.length() - 131, content.length())); msg = ((WiserMessage) getWiser().getMessages().get(1)).getMimeMessage(); assertEqualsIgnoreWhitespace("multipart/alternative;boundary=\"----=_Part_", msg.getContentType(), 0, 47); assertEquals("tony@domain.com", ((InternetAddress) msg.getFrom()[0]).getAddress()); assertEquals("tony@domain.com", ((InternetAddress) msg.getReplyTo()[0]).getAddress()); assertEquals("steve@domain.com", ((InternetAddress) msg.getRecipients(RecipientType.TO)[0]).getAddress()); assertEquals("Task Assignment End Event: This is my task name", msg.getSubject()); multiPart = (MimeMultipart) msg.getContent(); messageBodyPart = multiPart.getBodyPart(0); assertEquals("text/plain; charset=UTF8;", messageBodyPart.getDataHandler().getContentType()); content = new String(getBytes(messageBodyPart.getDataHandler().getInputStream())); assertEqualsIgnoreWhitespace(subject + description, content); messageBodyPart = multiPart.getBodyPart(1); assertEquals("text/calendar; charset=UTF8; name=ical-End-1.ics", messageBodyPart.getDataHandler().getContentType()); content = new String(getBytes(messageBodyPart.getDataHandler().getInputStream())); assertEqualsIgnoreWhitespace( "BEGIN:VCALENDARPRODID:-//iCal4j 1.0//ENCALSCALE:GREGORIANVERSION:2.0METHOD:REQUESTBEGIN:VEVENTDTSTART;TZID=UTC:", content.substring(0, 123)); assertEqualsIgnoreWhitespace( "SUMMARY:\"Task End : This is my task subject\"DESCRIPTION:\"This is my task description\"PRIORITY:55END:VEVENTEND:VCALENDAR", content.substring(content.length() - 131, content.length())); }
From source file:org.drools.task.service.IcalBaseTest.java
public void testSendWithStartDeadline() throws Exception { Map vars = new HashedMap(); vars.put("users", users); vars.put("groups", groups); vars.put("now", new Date()); String str = "(with (new Task()) { priority = 55, taskData = (with( new TaskData()) { createdOn = now, createdBy = users['tony'], activationTime = now}), "; str += "peopleAssignments = (with ( new PeopleAssignments() ) {potentialOwners = [users['steve' ], users['tony' ]]}), "; str += "names = [ new I18NText( 'en-UK', 'This is my task name')],"; str += "subjects = [ new I18NText( 'en-UK', 'This is my task subject')],"; str += "descriptions = [ new I18NText( 'en-UK', 'This is my task description')],"; str += "deadlines = (with (new Deadlines() ) {"; str += " startDeadlines = [ "; str += " (with (new Deadline()) {"; str += " date = now"; str += " } ) ]"; str += "} ) })"; MockUserInfo userInfo = new MockUserInfo(); userInfo.getEmails().put(users.get("tony"), "tony@domain.com"); userInfo.getEmails().put(users.get("steve"), "steve@domain.com"); userInfo.getLanguages().put(users.get("tony"), "en-UK"); userInfo.getLanguages().put(users.get("steve"), "en-UK"); taskService.setUserinfo(userInfo);/*from w w w .j a va 2 s .co m*/ BlockingAddTaskResponseHandler addTaskResponseHandler = new BlockingAddTaskResponseHandler(); Task task = (Task) eval(new StringReader(str), vars); client.addTask(task, null, addTaskResponseHandler); long taskId = addTaskResponseHandler.getTaskId(); BlockingTaskOperationResponseHandler responseHandler = new BlockingTaskOperationResponseHandler(); client.claim(taskId, users.get("steve").getId(), responseHandler); responseHandler.waitTillDone(5000); assertEquals(1, getWiser().getMessages().size()); assertEquals("steve@domain.com", getWiser().getMessages().get(0).getEnvelopeReceiver()); String subject = "Summary\n-------\n\nThis is my task subject\n\n"; String description = "Description\n-----------\n\nThis is my task description"; MimeMessage msg = ((WiserMessage) getWiser().getMessages().get(0)).getMimeMessage(); assertEqualsIgnoreWhitespace("multipart/alternative;boundary=\"----=_Part_", msg.getContentType(), 0, 47); assertEquals("tony@domain.com", ((InternetAddress) msg.getFrom()[0]).getAddress()); assertEquals("tony@domain.com", ((InternetAddress) msg.getReplyTo()[0]).getAddress()); assertEquals("steve@domain.com", ((InternetAddress) msg.getRecipients(RecipientType.TO)[0]).getAddress()); assertEquals("Task Assignment Start Event: This is my task name", msg.getSubject()); MimeMultipart multiPart = (MimeMultipart) msg.getContent(); BodyPart messageBodyPart = multiPart.getBodyPart(0); assertEquals("text/plain; charset=UTF8;", messageBodyPart.getDataHandler().getContentType()); String content = new String(getBytes(messageBodyPart.getDataHandler().getInputStream())); assertEqualsIgnoreWhitespace(subject + description, content); messageBodyPart = multiPart.getBodyPart(1); assertEquals("text/calendar; charset=UTF8; name=ical-Start-1.ics", messageBodyPart.getDataHandler().getContentType()); content = new String(getBytes(messageBodyPart.getDataHandler().getInputStream())); assertEqualsIgnoreWhitespace( "BEGIN:VCALENDARPRODID:-//iCal4j 1.0//ENCALSCALE:GREGORIANVERSION:2.0METHOD:REQUESTBEGIN:VEVENTDTSTART;TZID=UTC:", content.substring(0, 123)); assertEqualsIgnoreWhitespace( "SUMMARY:\"Task Start : This is my task subject\"DESCRIPTION:\"This is my task description\"PRIORITY:55END:VEVENTEND:VCALENDAR", content.substring(content.length() - 131, content.length())); }
From source file:org.drools.task.service.IcalBaseTest.java
public void testSendWithEndDeadline() throws Exception { Map vars = new HashedMap(); vars.put("users", users); vars.put("groups", groups); vars.put("now", new Date()); String str = "(with (new Task()) { priority = 55, taskData = (with( new TaskData()) { createdOn = now, createdBy = users['tony'], activationTime = now}), "; str += "peopleAssignments = (with ( new PeopleAssignments() ) {potentialOwners = [users['steve' ], users['tony' ]]}), "; str += "names = [ new I18NText( 'en-UK', 'This is my task name')],"; str += "subjects = [ new I18NText( 'en-UK', 'This is my task subject')],"; str += "descriptions = [ new I18NText( 'en-UK', 'This is my task description')],"; str += "deadlines = (with (new Deadlines() ) {"; str += " endDeadlines = ["; str += " (with (new Deadline()) {"; str += " date = new Date( now.time + ( 1000 * 60 * 60 * 24 ) )"; // set to tomorrow str += " } ) ]"; str += "} ) })"; MockUserInfo userInfo = new MockUserInfo(); userInfo.getEmails().put(users.get("tony"), "tony@domain.com"); userInfo.getEmails().put(users.get("steve"), "steve@domain.com"); userInfo.getLanguages().put(users.get("tony"), "en-UK"); userInfo.getLanguages().put(users.get("steve"), "en-UK"); taskService.setUserinfo(userInfo);//from w w w. j a v a 2s . co m BlockingAddTaskResponseHandler addTaskResponseHandler = new BlockingAddTaskResponseHandler(); Task task = (Task) eval(new StringReader(str), vars); client.addTask(task, null, addTaskResponseHandler); long taskId = addTaskResponseHandler.getTaskId(); BlockingTaskOperationResponseHandler responseHandler = new BlockingTaskOperationResponseHandler(); client.claim(taskId, users.get("steve").getId(), responseHandler); responseHandler.waitTillDone(5000); assertEquals(1, getWiser().getMessages().size()); assertEquals("steve@domain.com", getWiser().getMessages().get(0).getEnvelopeReceiver()); String subject = "Summary\n-------\n\nThis is my task subject\n\n"; String description = "Description\n-----------\n\nThis is my task description"; MimeMessage msg = ((WiserMessage) getWiser().getMessages().get(0)).getMimeMessage(); assertEqualsIgnoreWhitespace("multipart/alternative;boundary=\"----=_Part_", msg.getContentType(), 0, 47); assertEquals("tony@domain.com", ((InternetAddress) msg.getFrom()[0]).getAddress()); assertEquals("tony@domain.com", ((InternetAddress) msg.getReplyTo()[0]).getAddress()); assertEquals("steve@domain.com", ((InternetAddress) msg.getRecipients(RecipientType.TO)[0]).getAddress()); assertEquals("Task Assignment End Event: This is my task name", msg.getSubject()); MimeMultipart multiPart = (MimeMultipart) msg.getContent(); BodyPart messageBodyPart = multiPart.getBodyPart(0); assertEquals("text/plain; charset=UTF8;", messageBodyPart.getDataHandler().getContentType()); String content = new String(getBytes(messageBodyPart.getDataHandler().getInputStream())); assertEqualsIgnoreWhitespace(subject + description, content); messageBodyPart = multiPart.getBodyPart(1); assertEquals("text/calendar; charset=UTF8; name=ical-End-1.ics", messageBodyPart.getDataHandler().getContentType()); content = new String(getBytes(messageBodyPart.getDataHandler().getInputStream())); assertEqualsIgnoreWhitespace( "BEGIN:VCALENDARPRODID:-//iCal4j 1.0//ENCALSCALE:GREGORIANVERSION:2.0METHOD:REQUESTBEGIN:VEVENTDTSTART;TZID=UTC:", content.substring(0, 123)); assertEqualsIgnoreWhitespace( "SUMMARY:\"Task End : This is my task subject\"DESCRIPTION:\"This is my task description\"PRIORITY:55END:VEVENTEND:VCALENDAR", content.substring(content.length() - 131, content.length())); }
From source file:org.drools.task.service.TaskServiceDeadlinesBaseTest.java
public void testDelayedEmailNotificationOnDeadline() throws Exception { Map vars = new HashedMap(); vars.put("users", users); vars.put("groups", groups); vars.put("now", new Date()); DefaultEscalatedDeadlineHandler notificationHandler = new DefaultEscalatedDeadlineHandler(getConf()); WorkItemManager manager = new DefaultWorkItemManager(null); notificationHandler.setManager(manager); MockUserInfo userInfo = new MockUserInfo(); userInfo.getEmails().put(users.get("tony"), "tony@domain.com"); userInfo.getEmails().put(users.get("darth"), "darth@domain.com"); userInfo.getLanguages().put(users.get("tony"), "en-UK"); userInfo.getLanguages().put(users.get("darth"), "en-UK"); notificationHandler.setUserInfo(userInfo); taskService.setEscalatedDeadlineHandler(notificationHandler); String string = toString(/* w w w . j a v a2s.c om*/ new InputStreamReader(getClass().getResourceAsStream("../DeadlineWithNotification.mvel"))); BlockingAddTaskResponseHandler addTaskResponseHandler = new BlockingAddTaskResponseHandler(); Task task = (Task) eval(new StringReader(string), vars); client.addTask(task, null, addTaskResponseHandler); long taskId = addTaskResponseHandler.getTaskId(); Content content = new Content(); content.setContent("['subject' : 'My Subject', 'body' : 'My Body']".getBytes()); BlockingSetContentResponseHandler setContentResponseHandler = new BlockingSetContentResponseHandler(); client.setDocumentContent(taskId, content, setContentResponseHandler); long contentId = setContentResponseHandler.getContentId(); BlockingGetContentResponseHandler getResponseHandler = new BlockingGetContentResponseHandler(); client.getContent(contentId, getResponseHandler); content = getResponseHandler.getContent(); assertEquals("['subject' : 'My Subject', 'body' : 'My Body']", new String(content.getContent())); // emails should not be set yet assertEquals(0, getWiser().getMessages().size()); Thread.sleep(100); // nor yet assertEquals(0, getWiser().getMessages().size()); long time = 0; while (getWiser().getMessages().size() != 2 && time < 15000) { Thread.sleep(500); time += 500; } // 1 email with two recipients should now exist assertEquals(2, getWiser().getMessages().size()); List<String> list = new ArrayList<String>(2); list.add(getWiser().getMessages().get(0).getEnvelopeReceiver()); list.add(getWiser().getMessages().get(1).getEnvelopeReceiver()); assertTrue(list.contains("tony@domain.com")); assertTrue(list.contains("darth@domain.com")); MimeMessage msg = ((WiserMessage) getWiser().getMessages().get(0)).getMimeMessage(); assertEquals("My Body", msg.getContent()); assertEquals("My Subject", msg.getSubject()); assertEquals("from@domain.com", ((InternetAddress) msg.getFrom()[0]).getAddress()); assertEquals("replyTo@domain.com", ((InternetAddress) msg.getReplyTo()[0]).getAddress()); assertEquals("tony@domain.com", ((InternetAddress) msg.getRecipients(RecipientType.TO)[0]).getAddress()); assertEquals("darth@domain.com", ((InternetAddress) msg.getRecipients(RecipientType.TO)[1]).getAddress()); }
From source file:org.eurekastreams.server.service.actions.strategies.EmailerFactory.java
/** * Retrieves the multipart object from the message. * * @param message//from ww w. j a va 2 s . com * Email message being built. * @return multipart object. * @throws MessagingException * If there is a problem retrieving the content. */ protected Multipart getMultipart(final MimeMessage message) throws MessagingException { try { return (Multipart) message.getContent(); } catch (IOException ex) { throw new MessagingException("Failed to retrieve multipart from message being built.", ex); } }
From source file:org.ikasan.component.endpoint.email.producer.EmailProducerTest.java
@Test public void test_successful_email_withoutAttachment() throws MessagingException, IOException { EmailProducerConfiguration emailProducerConfiguration = getConfiguration(false, null); EmailProducer emailProducer = new EmailProducer(); ((Configured) emailProducer).setConfiguration(emailProducerConfiguration); ((ManagedResource) emailProducer).startManagedResource(); emailProducer.invoke(getEmailPayload(false, null)); List<WiserMessage> messages = wiser.getMessages(); Assert.assertTrue("Should be three messages - one per addressee", messages.size() == 3); for (WiserMessage message : wiser.getMessages()) { Assert.assertTrue("sender should be " + sender, sender.equals(message.getEnvelopeSender())); MimeMessage mimeMessage = message.getMimeMessage(); MimeMultipart mimeMultipart = (MimeMultipart) mimeMessage.getContent(); Assert.assertTrue("should be only 1 bodypart", mimeMultipart.getCount() == 1); BodyPart bodyPart = mimeMultipart.getBodyPart(0); String content = (String) bodyPart.getContent(); Assert.assertTrue("The email content should be empty", content.isEmpty()); Assert.assertTrue("Should fild email format as \"text/plain\"", bodyPart.getContentType().contains("text/plain")); }/*from w w w. ja va 2 s .c o m*/ }
From source file:org.ikasan.component.endpoint.email.producer.EmailProducerTest.java
@Test public void test_successful_email_contentFromConfig() throws MessagingException, IOException { EmailProducerConfiguration emailProducerConfiguration = getConfiguration(false, "This content is from config"); EmailProducer emailProducer = new EmailProducer(); ((Configured) emailProducer).setConfiguration(emailProducerConfiguration); ((ManagedResource) emailProducer).startManagedResource(); emailProducer.invoke(getEmailPayload(false, null)); List<WiserMessage> messages = wiser.getMessages(); Assert.assertTrue("Should be three messages - one per addressee", messages.size() == 3); for (WiserMessage message : wiser.getMessages()) { Assert.assertTrue("sender should be " + sender, sender.equals(message.getEnvelopeSender())); MimeMessage mimeMessage = message.getMimeMessage(); MimeMultipart mimeMultipart = (MimeMultipart) mimeMessage.getContent(); Assert.assertTrue("should be only 1 bodypart", mimeMultipart.getCount() == 1); BodyPart bodyPart = mimeMultipart.getBodyPart(0); String content = (String) bodyPart.getContent(); Assert.assertEquals("The email content should be from config", "This content is from config", content); Assert.assertTrue("Should find email format as \"text/plain\"", bodyPart.getContentType().contains("text/plain")); }/*from w w w. j a v a2 s .com*/ }
From source file:org.ikasan.component.endpoint.email.producer.EmailProducerTest.java
@Test public void test_successful_email_contentFromPayload() throws MessagingException, IOException { EmailProducerConfiguration emailProducerConfiguration = getConfiguration(false, "This content is from config"); EmailProducer emailProducer = new EmailProducer(); ((Configured) emailProducer).setConfiguration(emailProducerConfiguration); ((ManagedResource) emailProducer).startManagedResource(); emailProducer.invoke(getEmailPayload(false, "The content is from payload")); List<WiserMessage> messages = wiser.getMessages(); Assert.assertTrue("Should be three messages - one per addressee", messages.size() == 3); for (WiserMessage message : wiser.getMessages()) { Assert.assertTrue("sender should be " + sender, sender.equals(message.getEnvelopeSender())); MimeMessage mimeMessage = message.getMimeMessage(); MimeMultipart mimeMultipart = (MimeMultipart) mimeMessage.getContent(); Assert.assertTrue("should be only 1 bodypart", mimeMultipart.getCount() == 1); BodyPart bodyPart = mimeMultipart.getBodyPart(0); String content = (String) bodyPart.getContent(); Assert.assertEquals("The email content should be from payload", "The content is from payload", content); Assert.assertTrue("Should find email format as \"text/plain\"", bodyPart.getContentType().contains("text/plain")); }/*w w w. j a v a2s .com*/ }