@Test public void testComposeMimeMessageInternalTextAndHtmlVersion() 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)); }}); final TemplateProcessor templates = new MailComposerTemplateSupportGroovyImpl(new GroovyGStringTemplateSupportImpl(cacheManager)); JavaMailSenderImpl sender = new JavaMailSenderImpl(); sender.setHost("localhost"); MimeMessage message = sender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8"); String textTemplate = "Bender lives in theme park with blackjack poetess"; String htmlTemplate = "<h2>Bender</h2> lives in theme park with:<br> blackjack<br>poetess<br>"; Map<String, byte[]> attachments = Collections.emptyMap(); MailComposerImpl mailComposer = new MailComposerImpl(null, templates); mailComposer.composeMessage(helper, textTemplate, htmlTemplate, attachments, Collections.EMPTY_LIST, "SHOP10", "en", "test"); 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>")); mockery.assertIsSatisfied(); }
void composeMessage(final MimeMessage message, 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) throws MessagingException, IOException, ClassNotFoundException { final MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8"); helper.setTo(toEmail); helper.setSentDate(new Date()); if (ccEmail != null) { helper.setCc(ccEmail); } if (bccEmail != null) { helper.setBcc(bccEmail); } final String textContent = processTemplate(mailTemplateChain, shopCode, locale, templateName, ".txt", model); final String htmlContent = processTemplate(mailTemplateChain, shopCode, locale, templateName, ".html", model); final String propString = processTemplate(mailTemplateChain, shopCode, locale, templateName, ".properties", model); final Properties prop = new Properties(); if (propString != null) { prop.load(new StringReader(propString)); } helper.setSubject(prop.getProperty("subject")); if (from == null) { helper.setFrom(prop.getProperty("from")); } else { helper.setFrom(from); } final Map<String, byte[]> attachments = collectAttachments(model); composeMessage(helper, textContent, htmlContent, attachments, mailTemplateChain, shopCode, locale, templateName); }
MailComposerImpl implements MailComposer { void composeMessage(final MimeMessage message, 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) throws MessagingException, IOException, ClassNotFoundException { final MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8"); helper.setTo(toEmail); helper.setSentDate(new Date()); if (ccEmail != null) { helper.setCc(ccEmail); } if (bccEmail != null) { helper.setBcc(bccEmail); } final String textContent = processTemplate(mailTemplateChain, shopCode, locale, templateName, ".txt", model); final String htmlContent = processTemplate(mailTemplateChain, shopCode, locale, templateName, ".html", model); final String propString = processTemplate(mailTemplateChain, shopCode, locale, templateName, ".properties", model); final Properties prop = new Properties(); if (propString != null) { prop.load(new StringReader(propString)); } helper.setSubject(prop.getProperty("subject")); if (from == null) { helper.setFrom(prop.getProperty("from")); } else { helper.setFrom(from); } final Map<String, byte[]> attachments = collectAttachments(model); composeMessage(helper, textContent, htmlContent, attachments, mailTemplateChain, shopCode, locale, templateName); } }
MailComposerImpl implements MailComposer { void composeMessage(final MimeMessage message, 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) throws MessagingException, IOException, ClassNotFoundException { final MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8"); helper.setTo(toEmail); helper.setSentDate(new Date()); if (ccEmail != null) { helper.setCc(ccEmail); } if (bccEmail != null) { helper.setBcc(bccEmail); } final String textContent = processTemplate(mailTemplateChain, shopCode, locale, templateName, ".txt", model); final String htmlContent = processTemplate(mailTemplateChain, shopCode, locale, templateName, ".html", model); final String propString = processTemplate(mailTemplateChain, shopCode, locale, templateName, ".properties", model); final Properties prop = new Properties(); if (propString != null) { prop.load(new StringReader(propString)); } helper.setSubject(prop.getProperty("subject")); if (from == null) { helper.setFrom(prop.getProperty("from")); } else { helper.setFrom(from); } final Map<String, byte[]> attachments = collectAttachments(model); composeMessage(helper, textContent, htmlContent, attachments, mailTemplateChain, shopCode, locale, templateName); } MailComposerImpl(final MailTemplateResourcesProvider mailTemplateResourcesProvider,
                            final TemplateProcessor templateSupport); }
MailComposerImpl implements MailComposer { void composeMessage(final MimeMessage message, 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) throws MessagingException, IOException, ClassNotFoundException { final MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8"); helper.setTo(toEmail); helper.setSentDate(new Date()); if (ccEmail != null) { helper.setCc(ccEmail); } if (bccEmail != null) { helper.setBcc(bccEmail); } final String textContent = processTemplate(mailTemplateChain, shopCode, locale, templateName, ".txt", model); final String htmlContent = processTemplate(mailTemplateChain, shopCode, locale, templateName, ".html", model); final String propString = processTemplate(mailTemplateChain, shopCode, locale, templateName, ".properties", model); final Properties prop = new Properties(); if (propString != null) { prop.load(new StringReader(propString)); } helper.setSubject(prop.getProperty("subject")); if (from == null) { helper.setFrom(prop.getProperty("from")); } else { helper.setFrom(from); } final Map<String, byte[]> attachments = collectAttachments(model); composeMessage(helper, textContent, htmlContent, attachments, mailTemplateChain, shopCode, locale, templateName); } 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); }
MailComposerImpl implements MailComposer { void composeMessage(final MimeMessage message, 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) throws MessagingException, IOException, ClassNotFoundException { final MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8"); helper.setTo(toEmail); helper.setSentDate(new Date()); if (ccEmail != null) { helper.setCc(ccEmail); } if (bccEmail != null) { helper.setBcc(bccEmail); } final String textContent = processTemplate(mailTemplateChain, shopCode, locale, templateName, ".txt", model); final String htmlContent = processTemplate(mailTemplateChain, shopCode, locale, templateName, ".html", model); final String propString = processTemplate(mailTemplateChain, shopCode, locale, templateName, ".properties", model); final Properties prop = new Properties(); if (propString != null) { prop.load(new StringReader(propString)); } helper.setSubject(prop.getProperty("subject")); if (from == null) { helper.setFrom(prop.getProperty("from")); } else { helper.setFrom(from); } final Map<String, byte[]> attachments = collectAttachments(model); composeMessage(helper, textContent, htmlContent, attachments, mailTemplateChain, shopCode, locale, templateName); } 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);  }