@Test public void testSendEmailWithAttachments() throws Exception { final String email = "user_email"; final String header = "header"; final String body = "body"; final String footer = "footer"; final String note = header + "\\s*" + body + "\\s*" + footer; List<String> addresses = new ArrayList<>(); addresses.add(email); EmailWithAttachmentsDTO inputDTO = new EmailWithAttachmentsDTO(); inputDTO.setEmailAddresses(addresses); inputDTO.setHeader(header); inputDTO.setBody(body); inputDTO.setFooter(footer); senderConfig.setEncryption("off"); List<Long> attachmentIds = new ArrayList<>(); attachmentIds.add(999L); inputDTO.setAttachmentIds(attachmentIds); List<String> filePaths = new ArrayList<>(); Resource resource = new ClassPathResource("temp.zip"); filePaths.add(resource.getFile().getAbsolutePath()); inputDTO.setFilePaths(filePaths); ArgumentCaptor<String> capturedNote = ArgumentCaptor.forClass(String.class); ArgumentCaptor<ArrayList<InputStreamDataSource>> capturedAttachments = ArgumentCaptor.forClass((Class) ArrayList.class); when(mockEmailSenderConfigurationService.readConfiguration()).thenReturn(senderConfig); ArgumentCaptor<byte[]> read = ArgumentCaptor.forClass(byte[].class); when(mockEcmFileService.downloadAsInputStream(attachmentIds.get(0))).thenReturn(mockInputStream); when(mockEcmFileService.downloadAsInputStream(attachmentIds.get(0), null)).thenReturn(mockInputStream); when(mockEcmFileService.findById(attachmentIds.get(0))).thenReturn(mockEcmFile); when(mockInputStream.read(read.capture(), eq(0), eq(16384))).thenReturn(-1); when(mockEcmFile.getFileName()).thenReturn("fileName"); when(mockEcmFile.getFileActiveVersionNameExtension()).thenReturn(".extension"); mockInputStream.close(); when(mockAcmUser.getUserId()).thenReturn("ann-acm"); when(mockEcmFile.getParentObjectId()).thenReturn(103L); when(mockEcmFile.getParentObjectType()).thenReturn("COMPLAINT"); when(mockEcmFile.getId()).thenReturn(attachmentIds.get(0)); when(mockEcmFile.getObjectType()).thenReturn(EcmFileConstants.OBJECT_FILE_TYPE); whenNew(File.class).withArguments(filePaths.get(0)).thenReturn(mockFile); whenNew(FileInputStream.class).withArguments(mockFile).thenReturn(mockFileInputStream); when(mockFile.getName()).thenReturn("temp.zip"); service.sendEmailWithAttachments(inputDTO, mockAuthentication, mockAcmUser); verify(mockMailSender).sendMultipartEmail(eq(email), anyString(), capturedNote.capture(), capturedAttachments.capture()); assertThat(Pattern.compile(note).matcher(capturedNote.getValue()).matches(), is(true)); assertThat(capturedAttachments.getValue(), notNullValue()); assertThat(capturedAttachments.getValue().size(), is(2)); assertThat(capturedAttachments.getValue().get(0).getName(), notNullValue()); assertThat(capturedAttachments.getValue().get(0).getContentType(), notNullValue()); assertThat(capturedAttachments.getValue().get(1).getName(), notNullValue()); assertThat(capturedAttachments.getValue().get(1).getContentType(), notNullValue()); mockApplicationEventPublisher.publishEvent(any(SmtpEventSentEvent.class)); }
@Override public void sendEmailWithAttachments(EmailWithAttachmentsDTO in, Authentication authentication, AcmUser user) throws Exception { in.setTemplatingEngine(getTemplatingEngine()); Exception exception = null; List<SmtpEventSentEvent> sentEvents = new ArrayList<>(); Map<String, InputStreamDataSource> attachments = processAttachments(in, user, sentEvents); for (String emailAddress : in.getEmailAddresses()) { try { acmMailSender.sendMultipartEmail(emailAddress, in.getSubject(), in.getMessageBody(), new ArrayList<>(attachments.values())); } catch (Exception e) { exception = e; log.error("Failed to send email to [{}].", emailAddress, exception); } } for (SmtpEventSentEvent event : sentEvents) { boolean success = (exception == null); event.setSucceeded(success); eventPublisher.publishEvent(event); } }
SmtpService implements AcmEmailSenderService, ApplicationEventPublisherAware { @Override public void sendEmailWithAttachments(EmailWithAttachmentsDTO in, Authentication authentication, AcmUser user) throws Exception { in.setTemplatingEngine(getTemplatingEngine()); Exception exception = null; List<SmtpEventSentEvent> sentEvents = new ArrayList<>(); Map<String, InputStreamDataSource> attachments = processAttachments(in, user, sentEvents); for (String emailAddress : in.getEmailAddresses()) { try { acmMailSender.sendMultipartEmail(emailAddress, in.getSubject(), in.getMessageBody(), new ArrayList<>(attachments.values())); } catch (Exception e) { exception = e; log.error("Failed to send email to [{}].", emailAddress, exception); } } for (SmtpEventSentEvent event : sentEvents) { boolean success = (exception == null); event.setSucceeded(success); eventPublisher.publishEvent(event); } } }
SmtpService implements AcmEmailSenderService, ApplicationEventPublisherAware { @Override public void sendEmailWithAttachments(EmailWithAttachmentsDTO in, Authentication authentication, AcmUser user) throws Exception { in.setTemplatingEngine(getTemplatingEngine()); Exception exception = null; List<SmtpEventSentEvent> sentEvents = new ArrayList<>(); Map<String, InputStreamDataSource> attachments = processAttachments(in, user, sentEvents); for (String emailAddress : in.getEmailAddresses()) { try { acmMailSender.sendMultipartEmail(emailAddress, in.getSubject(), in.getMessageBody(), new ArrayList<>(attachments.values())); } catch (Exception e) { exception = e; log.error("Failed to send email to [{}].", emailAddress, exception); } } for (SmtpEventSentEvent event : sentEvents) { boolean success = (exception == null); event.setSucceeded(success); eventPublisher.publishEvent(event); } }  }
SmtpService implements AcmEmailSenderService, ApplicationEventPublisherAware { @Override public void sendEmailWithAttachments(EmailWithAttachmentsDTO in, Authentication authentication, AcmUser user) throws Exception { in.setTemplatingEngine(getTemplatingEngine()); Exception exception = null; List<SmtpEventSentEvent> sentEvents = new ArrayList<>(); Map<String, InputStreamDataSource> attachments = processAttachments(in, user, sentEvents); for (String emailAddress : in.getEmailAddresses()) { try { acmMailSender.sendMultipartEmail(emailAddress, in.getSubject(), in.getMessageBody(), new ArrayList<>(attachments.values())); } catch (Exception e) { exception = e; log.error("Failed to send email to [{}].", emailAddress, exception); } } for (SmtpEventSentEvent event : sentEvents) { boolean success = (exception == null); event.setSucceeded(success); eventPublisher.publishEvent(event); } }  @Override void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher); @Override void sendPlainEmail(Stream<T> emailsDataStream, EmailBuilder<T> emailBuilder, EmailBodyBuilder<T> emailBodyBuilder); @Override void sendEmailWithAttachments(EmailWithAttachmentsDTO in, Authentication authentication, AcmUser user); @Override void sendEmail(EmailWithAttachmentsDTO in, Authentication authentication, AcmUser user); @Override void sendEmailWithAttachmentsAndLinks(EmailWithAttachmentsAndLinksDTO in, Authentication authentication, AcmUser user); @Override void sendEmail(EmailWithAttachmentsAndLinksDTO in, Authentication authentication, AcmUser user); @Override List<EmailWithEmbeddedLinksResultDTO> sendEmailWithEmbeddedLinks(EmailWithEmbeddedLinksDTO in, Authentication authentication,
            AcmUser user); void setEcmFileService(EcmFileService ecmFileService); AcmEmailContentGeneratorService getAcmEmailContentGeneratorService(); void setAcmEmailContentGeneratorService(AcmEmailContentGeneratorService acmEmailContentGeneratorService); EmailSenderConfig getEmailSenderConfig(); void setEmailSenderConfig(EmailSenderConfig emailSenderConfig); TemplatingEngine getTemplatingEngine(); void setTemplatingEngine(TemplatingEngine templatingEngine); DefaultFolderAndFileConverter getDefaultFolderAndFileConverter(); void setDefaultFolderAndFileConverter(DefaultFolderAndFileConverter defaultFolderAndFileConverter); AcmMailSender getAcmMailSender(); void setAcmMailSender(AcmMailSender acmMailSender); }
SmtpService implements AcmEmailSenderService, ApplicationEventPublisherAware { @Override public void sendEmailWithAttachments(EmailWithAttachmentsDTO in, Authentication authentication, AcmUser user) throws Exception { in.setTemplatingEngine(getTemplatingEngine()); Exception exception = null; List<SmtpEventSentEvent> sentEvents = new ArrayList<>(); Map<String, InputStreamDataSource> attachments = processAttachments(in, user, sentEvents); for (String emailAddress : in.getEmailAddresses()) { try { acmMailSender.sendMultipartEmail(emailAddress, in.getSubject(), in.getMessageBody(), new ArrayList<>(attachments.values())); } catch (Exception e) { exception = e; log.error("Failed to send email to [{}].", emailAddress, exception); } } for (SmtpEventSentEvent event : sentEvents) { boolean success = (exception == null); event.setSucceeded(success); eventPublisher.publishEvent(event); } }  @Override void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher); @Override void sendPlainEmail(Stream<T> emailsDataStream, EmailBuilder<T> emailBuilder, EmailBodyBuilder<T> emailBodyBuilder); @Override void sendEmailWithAttachments(EmailWithAttachmentsDTO in, Authentication authentication, AcmUser user); @Override void sendEmail(EmailWithAttachmentsDTO in, Authentication authentication, AcmUser user); @Override void sendEmailWithAttachmentsAndLinks(EmailWithAttachmentsAndLinksDTO in, Authentication authentication, AcmUser user); @Override void sendEmail(EmailWithAttachmentsAndLinksDTO in, Authentication authentication, AcmUser user); @Override List<EmailWithEmbeddedLinksResultDTO> sendEmailWithEmbeddedLinks(EmailWithEmbeddedLinksDTO in, Authentication authentication,
            AcmUser user); void setEcmFileService(EcmFileService ecmFileService); AcmEmailContentGeneratorService getAcmEmailContentGeneratorService(); void setAcmEmailContentGeneratorService(AcmEmailContentGeneratorService acmEmailContentGeneratorService); EmailSenderConfig getEmailSenderConfig(); void setEmailSenderConfig(EmailSenderConfig emailSenderConfig); TemplatingEngine getTemplatingEngine(); void setTemplatingEngine(TemplatingEngine templatingEngine); DefaultFolderAndFileConverter getDefaultFolderAndFileConverter(); void setDefaultFolderAndFileConverter(DefaultFolderAndFileConverter defaultFolderAndFileConverter); AcmMailSender getAcmMailSender(); void setAcmMailSender(AcmMailSender acmMailSender);  }