@SuppressWarnings("unchecked") @Test public void buildMimeMessagePreparator() throws Exception { MimeMessageHelper mockMessageHelper = PowerMockito.mock(MimeMessageHelper.class); PowerMockito.whenNew(MimeMessageHelper.class).withArguments(any(MimeMessage.class), anyBoolean()).thenReturn(mockMessageHelper); when(mailContentBuilderService.buildPlainTextMail(anyString(), anyMap())).thenReturn("content"); mailService = PowerMockito.spy(new MailService(mailSender, mailContentBuilderService)); String from = "from"; List<String> to = Lists.newArrayList(); to.add("test@gmail.com"); String subject = "subject"; String mailMessageUrlOrBody = "mailMessageUrlOrBody"; Map<String, Object> placeholderValues = Maps.newHashMap(); placeholderValues.put("name", "134"); final String attachmentUrl = "www.test.com"; final Boolean isPlainMessage = true; final MailService classUnderTest = PowerMockito.spy(new MailService(mailSender, mailContentBuilderService)); Whitebox.invokeMethod(classUnderTest, "buildMimeMessagePreparator", from, to, subject, mailMessageUrlOrBody, placeholderValues, attachmentUrl, isPlainMessage); }
private MimeMessagePreparator buildMimeMessagePreparator(String cc,String from, List<String> to, String subject, String mailMessageUrlOrBody, Map<String, Object> placeholderValues, final String attachmentUrl, final Boolean isPlainMessage) { MimeMessagePreparator messagePreparator = mimeMessage -> { MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true); messageHelper.setFrom(from); if(!StringUtils.isEmpty(cc)){ messageHelper.setCc(cc); } String[] toMailList = to.toArray(new String[to.size()]); messageHelper.setTo(toMailList); messageHelper.setSubject(subject); if(StringUtils.isNotEmpty(attachmentUrl) && isHttpUrl(attachmentUrl)) { URL url = new URL(attachmentUrl); String filename = url.getFile(); byte fileContent [] = getFileContent(url); messageHelper.addAttachment(filename, new ByteArrayResource(fileContent)); } String content = StringUtils.EMPTY; if(isPlainMessage) { content = mailContentBuilderService.buildPlainTextMail(mailMessageUrlOrBody, placeholderValues); } else { if(!isHttpUrl(mailMessageUrlOrBody)) { File template = new ClassPathResource("templates/".concat(mailMessageUrlOrBody).concat(".html")).getFile(); content = mailContentBuilderService.buildPlainTextMail(FileUtils.readFileToString(template, "UTF-8"), placeholderValues); } else { String mailBody = mailContentBuilderService.getRemoteMailContent(mailMessageUrlOrBody); content = mailContentBuilderService.buildPlainTextMail(mailBody, placeholderValues); } } messageHelper.setText(content, true); }; return messagePreparator; }
MailService { private MimeMessagePreparator buildMimeMessagePreparator(String cc,String from, List<String> to, String subject, String mailMessageUrlOrBody, Map<String, Object> placeholderValues, final String attachmentUrl, final Boolean isPlainMessage) { MimeMessagePreparator messagePreparator = mimeMessage -> { MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true); messageHelper.setFrom(from); if(!StringUtils.isEmpty(cc)){ messageHelper.setCc(cc); } String[] toMailList = to.toArray(new String[to.size()]); messageHelper.setTo(toMailList); messageHelper.setSubject(subject); if(StringUtils.isNotEmpty(attachmentUrl) && isHttpUrl(attachmentUrl)) { URL url = new URL(attachmentUrl); String filename = url.getFile(); byte fileContent [] = getFileContent(url); messageHelper.addAttachment(filename, new ByteArrayResource(fileContent)); } String content = StringUtils.EMPTY; if(isPlainMessage) { content = mailContentBuilderService.buildPlainTextMail(mailMessageUrlOrBody, placeholderValues); } else { if(!isHttpUrl(mailMessageUrlOrBody)) { File template = new ClassPathResource("templates/".concat(mailMessageUrlOrBody).concat(".html")).getFile(); content = mailContentBuilderService.buildPlainTextMail(FileUtils.readFileToString(template, "UTF-8"), placeholderValues); } else { String mailBody = mailContentBuilderService.getRemoteMailContent(mailMessageUrlOrBody); content = mailContentBuilderService.buildPlainTextMail(mailBody, placeholderValues); } } messageHelper.setText(content, true); }; return messagePreparator; } }
MailService { private MimeMessagePreparator buildMimeMessagePreparator(String cc,String from, List<String> to, String subject, String mailMessageUrlOrBody, Map<String, Object> placeholderValues, final String attachmentUrl, final Boolean isPlainMessage) { MimeMessagePreparator messagePreparator = mimeMessage -> { MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true); messageHelper.setFrom(from); if(!StringUtils.isEmpty(cc)){ messageHelper.setCc(cc); } String[] toMailList = to.toArray(new String[to.size()]); messageHelper.setTo(toMailList); messageHelper.setSubject(subject); if(StringUtils.isNotEmpty(attachmentUrl) && isHttpUrl(attachmentUrl)) { URL url = new URL(attachmentUrl); String filename = url.getFile(); byte fileContent [] = getFileContent(url); messageHelper.addAttachment(filename, new ByteArrayResource(fileContent)); } String content = StringUtils.EMPTY; if(isPlainMessage) { content = mailContentBuilderService.buildPlainTextMail(mailMessageUrlOrBody, placeholderValues); } else { if(!isHttpUrl(mailMessageUrlOrBody)) { File template = new ClassPathResource("templates/".concat(mailMessageUrlOrBody).concat(".html")).getFile(); content = mailContentBuilderService.buildPlainTextMail(FileUtils.readFileToString(template, "UTF-8"), placeholderValues); } else { String mailBody = mailContentBuilderService.getRemoteMailContent(mailMessageUrlOrBody); content = mailContentBuilderService.buildPlainTextMail(mailBody, placeholderValues); } } messageHelper.setText(content, true); }; return messagePreparator; } @Autowired  MailService(JavaMailSender mailSender, MailContentBuilderService mailContentBuilderService); }
MailService { private MimeMessagePreparator buildMimeMessagePreparator(String cc,String from, List<String> to, String subject, String mailMessageUrlOrBody, Map<String, Object> placeholderValues, final String attachmentUrl, final Boolean isPlainMessage) { MimeMessagePreparator messagePreparator = mimeMessage -> { MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true); messageHelper.setFrom(from); if(!StringUtils.isEmpty(cc)){ messageHelper.setCc(cc); } String[] toMailList = to.toArray(new String[to.size()]); messageHelper.setTo(toMailList); messageHelper.setSubject(subject); if(StringUtils.isNotEmpty(attachmentUrl) && isHttpUrl(attachmentUrl)) { URL url = new URL(attachmentUrl); String filename = url.getFile(); byte fileContent [] = getFileContent(url); messageHelper.addAttachment(filename, new ByteArrayResource(fileContent)); } String content = StringUtils.EMPTY; if(isPlainMessage) { content = mailContentBuilderService.buildPlainTextMail(mailMessageUrlOrBody, placeholderValues); } else { if(!isHttpUrl(mailMessageUrlOrBody)) { File template = new ClassPathResource("templates/".concat(mailMessageUrlOrBody).concat(".html")).getFile(); content = mailContentBuilderService.buildPlainTextMail(FileUtils.readFileToString(template, "UTF-8"), placeholderValues); } else { String mailBody = mailContentBuilderService.getRemoteMailContent(mailMessageUrlOrBody); content = mailContentBuilderService.buildPlainTextMail(mailBody, placeholderValues); } } messageHelper.setText(content, true); }; return messagePreparator; } @Autowired  MailService(JavaMailSender mailSender, MailContentBuilderService mailContentBuilderService); void prepareAndSendMail(String cc,String from, List<String> to, String subject, String mailMessageUrlOrBody, Map<String, Object> placeholderValues, final String attachmentUrl, final Boolean isPlainMessage); void prepareTemplateAndSendMail(String from, List<String> to, String subject, String mailMessageUrlOrBody, Map<String, Object> placeholderValues, final String attachmentUrl, final Boolean isPlainMessage); void prepareTemplateAndSendMail(String from, List<String> to, String subject, String mailContent , final String attachmentUrl); @SuppressWarnings("deprecation") String processTemplate(String templateUrl, Map<String, Object> model); }
MailService { private MimeMessagePreparator buildMimeMessagePreparator(String cc,String from, List<String> to, String subject, String mailMessageUrlOrBody, Map<String, Object> placeholderValues, final String attachmentUrl, final Boolean isPlainMessage) { MimeMessagePreparator messagePreparator = mimeMessage -> { MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true); messageHelper.setFrom(from); if(!StringUtils.isEmpty(cc)){ messageHelper.setCc(cc); } String[] toMailList = to.toArray(new String[to.size()]); messageHelper.setTo(toMailList); messageHelper.setSubject(subject); if(StringUtils.isNotEmpty(attachmentUrl) && isHttpUrl(attachmentUrl)) { URL url = new URL(attachmentUrl); String filename = url.getFile(); byte fileContent [] = getFileContent(url); messageHelper.addAttachment(filename, new ByteArrayResource(fileContent)); } String content = StringUtils.EMPTY; if(isPlainMessage) { content = mailContentBuilderService.buildPlainTextMail(mailMessageUrlOrBody, placeholderValues); } else { if(!isHttpUrl(mailMessageUrlOrBody)) { File template = new ClassPathResource("templates/".concat(mailMessageUrlOrBody).concat(".html")).getFile(); content = mailContentBuilderService.buildPlainTextMail(FileUtils.readFileToString(template, "UTF-8"), placeholderValues); } else { String mailBody = mailContentBuilderService.getRemoteMailContent(mailMessageUrlOrBody); content = mailContentBuilderService.buildPlainTextMail(mailBody, placeholderValues); } } messageHelper.setText(content, true); }; return messagePreparator; } @Autowired  MailService(JavaMailSender mailSender, MailContentBuilderService mailContentBuilderService); void prepareAndSendMail(String cc,String from, List<String> to, String subject, String mailMessageUrlOrBody, Map<String, Object> placeholderValues, final String attachmentUrl, final Boolean isPlainMessage); void prepareTemplateAndSendMail(String from, List<String> to, String subject, String mailMessageUrlOrBody, Map<String, Object> placeholderValues, final String attachmentUrl, final Boolean isPlainMessage); void prepareTemplateAndSendMail(String from, List<String> to, String subject, String mailContent , final String attachmentUrl); @SuppressWarnings("deprecation") String processTemplate(String templateUrl, Map<String, Object> model);  }