MailComposerImpl implements MailComposer { @Override public void convertMessage(final Mail mail, final MimeMessage mimeMessage) throws MessagingException, IOException, ClassNotFoundException { final MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "UTF-8"); helper.setTo(mail.getRecipients()); helper.setSentDate(new Date()); if (mail.getCc() != null) { helper.setCc(mail.getCc()); } if (mail.getBcc() != null) { helper.setBcc(mail.getBcc()); } final String textTemplate = mail.getTextVersion(); final String htmlTemplate = mail.getHtmlVersion(); helper.setSubject(mail.getSubject()); helper.setFrom(mail.getFrom()); if (textTemplate == null || htmlTemplate == null) { if (textTemplate != null) { helper.setText(textTemplate, false); } if (htmlTemplate != null) { helper.setText(htmlTemplate, true); inlineResources(helper, mail); } } else { helper.setText(textTemplate, htmlTemplate); inlineResources(helper, mail); } } MailComposerImpl(final MailTemplateResourcesProvider mailTemplateResourcesProvider,
                            final TemplateProcessor templateSupport); String getResourceExpression(); void setResourceExpression(final String resourceExpression); @Override void composeMessage(final Mail mail,
                               final String shopCode,
                               final String locale,
                               final List<String> mailTemplateChain,
                               final String templateName,
                               final String from,
                               final String toEmail,
                               final String ccEmail,
                               final String bccEmail,
                               final Map<String, Object> model); @Override void convertMessage(final Mail mail, final MimeMessage mimeMessage); @Override String convertMessageToTXT(final Mail mail); @Override String convertMessageToHTML(final Mail mail);  }
@Test public void testConvertMailEntityToMimeMessage() throws MessagingException, IOException, ClassNotFoundException { final CacheManager cacheManager = mockery.mock(CacheManager.class); final Cache cache = mockery.mock(Cache.class); mockery.checking(new Expectations() {{ allowing(cacheManager).getCache("contentService-templateSupport"); will(returnValue(cache)); allowing(cache).get("<% \n %>${name} is awesome!"); will(returnValue(null)); allowing(cache).put(with(equal("<% \n %>${name} is awesome!")), with(any(Object.class))); }}); final TemplateProcessor templates = new MailComposerTemplateSupportGroovyImpl(new GroovyGStringTemplateSupportImpl(cacheManager)); final Mail mail = new MailEntity(); mail.setShopCode("SHOP10"); mail.setSubject("Тема письма"); mail.setFrom("test@localhost.lo"); mail.setRecipients("to@somedomain.com"); mail.setCc("cc@somedomain.com"); mail.setBcc("bcc@somedomain.com"); mail.setTextVersion("Bender lives in theme park with blackjack poetess"); mail.setHtmlVersion("<h2>Bender</h2> lives in theme park with:<br> blackjack<br>poetess<br>"); JavaMailSenderImpl sender = new JavaMailSenderImpl(); sender.setHost("localhost"); MimeMessage message = sender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8"); MailComposerImpl mailComposer = new MailComposerImpl(null, templates); mailComposer.convertMessage(mail, message); assertTrue(helper.isMultipart()); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); helper.getMimeMessage().writeTo(byteArrayOutputStream); String str = byteArrayOutputStream.toString("UTF-8"); assertNotNull(str); assertTrue(str.contains("Bender lives in theme park with blackjack poetess")); assertTrue(str.contains("<h2>Bender</h2> lives in theme park with:<br> blackjack<br>poetess<br>")); assertTrue(str.contains("From: test@localhost.lo")); assertTrue(str.contains("To: to@somedomain.com")); assertTrue(str.contains("cc@somedomain.com")); assertTrue(str.contains("Bcc: bcc@somedomain.com")); assertEquals("Тема письма", message.getSubject()); mockery.assertIsSatisfied(); }
@Test public void testConvertMailEntityToMimeMessageWithAttach() throws MessagingException, IOException, ClassNotFoundException { final CacheManager cacheManager = mockery.mock(CacheManager.class); final Cache cache = mockery.mock(Cache.class); mockery.checking(new Expectations() {{ allowing(cacheManager).getCache("contentService-templateSupport"); will(returnValue(cache)); allowing(cache).get("<% \n %>${name} is awesome!"); will(returnValue(null)); allowing(cache).put(with(equal("<% \n %>${name} is awesome!")), with(any(Object.class))); }}); final TemplateProcessor templates = new MailComposerTemplateSupportGroovyImpl(new GroovyGStringTemplateSupportImpl(cacheManager)); final Mail mail = new MailEntity(); mail.setShopCode("SHOP10"); mail.setSubject("Тема письма"); mail.setFrom("test@localhost.lo"); mail.setRecipients("to@somedomain.com"); mail.setCc("cc@somedomain.com"); mail.setBcc("bcc@somedomain.com"); mail.setTextVersion("Bender lives in theme park with blackjack poetess"); mail.setHtmlVersion("<h2>Bender</h2> lives in theme park with:<br> blackjack<br>poetess<br>"); final MailPart attach = mail.addPart(); attach.setFilename("myimage.jpg"); attach.setResourceId("attachment:image/jpeg;myimage.jpg"); attach.setData(new byte[] { 1, 1, 1 }); JavaMailSenderImpl sender = new JavaMailSenderImpl(); sender.setHost("localhost"); MimeMessage message = sender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8"); MailComposerImpl mailComposer = new MailComposerImpl(null, templates); mailComposer.convertMessage(mail, message); assertTrue(helper.isMultipart()); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); helper.getMimeMessage().writeTo(byteArrayOutputStream); String str = byteArrayOutputStream.toString("UTF-8"); assertNotNull(str); assertTrue(str.contains("Bender lives in theme park with blackjack poetess")); assertTrue(str.contains("<h2>Bender</h2> lives in theme park with:<br> blackjack<br>poetess<br>")); assertTrue(str.contains("From: test@localhost.lo")); assertTrue(str.contains("To: to@somedomain.com")); assertTrue(str.contains("cc@somedomain.com")); assertTrue(str.contains("Bcc: bcc@somedomain.com")); assertEquals("Тема письма", message.getSubject()); assertTrue(str.contains("Content-Type: image/jpeg; name=myimage.jpg")); assertTrue(str.contains("Content-Transfer-Encoding: base64")); assertTrue(str.contains("Content-Disposition: attachment; filename=myimage.jpg")); assertTrue(str.contains("AQEB")); mockery.assertIsSatisfied(); }