MailComposerImpl implements MailComposer { @Override public String convertMessageToHTML(final Mail mail) { final StringBuilder htmlTemplate = new StringBuilder(mail.getHtmlVersion()); if (CollectionUtils.isNotEmpty(mail.getParts())) { for (final MailPart part : mail.getParts()) { final String fileName = part.getFilename(); final String resourceId = part.getResourceId(); if (resourceId.startsWith(ATTACHMENT_PREFIX)) { final Pair<String, String> contentTypeAndFile = convertAttachmentKeyIntoContentTypeAndFilename(resourceId); if (contentTypeAndFile != null) { final int pos = htmlTemplate.indexOf("</body>"); final String tag = embededTag(contentTypeAndFile.getFirst(), MimeTypesUtils.getBase64DataURL(contentTypeAndFile.getFirst(), part.getData())); if (pos != -1) { htmlTemplate.insert(pos, tag); } else { htmlTemplate.append(tag); } } } else { final String key = "cid:" + resourceId; final int pos = htmlTemplate.indexOf(key); final String contentType = MimeTypesUtils.getMimeType(fileName); final String tag = embededTag(contentType, MimeTypesUtils.getBase64DataURL(contentType, part.getData())); if (pos != -1) { htmlTemplate.replace(pos, pos + key.length(), MimeTypesUtils.getBase64DataURL(contentType, part.getData())); } else { htmlTemplate.append(tag); } } } } return htmlTemplate.toString(); } 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 testConvertMailEntityToHTMLWithAttach() 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("<img src=\"cid:mail-head_jpeg\"/><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}); final MailPart embed = mail.addPart(); embed.setFilename("mail-head.jpeg"); embed.setResourceId("mail-head_jpeg"); embed.setData(new byte[]{2, 2, 2}); MailComposerImpl mailComposer = new MailComposerImpl(null, templates); final String html = mailComposer.convertMessageToHTML(mail); String str = html; assertNotNull(str); assertTrue(str.contains("<img src=\"data:image/jpeg;base64,AgIC\"/>")); assertTrue(str.contains("<embed type=\"image/jpeg\" src=\"data:image/jpeg;base64,AQEB\" width=\"100%\" height=\"400\"/>")); assertTrue(str.contains("<h2>Bender</h2> lives in theme park with:<br> blackjack<br>poetess<br>")); mockery.assertIsSatisfied(); }