MailSender { public void sendMail(EmailBean emailBean) throws SendMailException { File tempDir = null; try { MimeMessage message = new MimeMessage(mailSessionProvider.get()); Multipart contentPart = new MimeMultipart(); MimeBodyPart bodyPart = new MimeBodyPart(); bodyPart.setText(emailBean.getBody(), "UTF-8", getSubType(emailBean.getMimeType())); contentPart.addBodyPart(bodyPart); if (emailBean.getAttachments() != null) { tempDir = Files.createTempDir(); for (Attachment attachment : emailBean.getAttachments()) { byte[] attachmentContent = Base64.getDecoder().decode(attachment.getContent()); File attachmentFile = new File(tempDir, attachment.getFileName()); Files.write(attachmentContent, attachmentFile); MimeBodyPart attachmentPart = new MimeBodyPart(); attachmentPart.attachFile(attachmentFile); attachmentPart.setContentID("<" + attachment.getContentId() + ">"); contentPart.addBodyPart(attachmentPart); } } message.setContent(contentPart); message.setSubject(emailBean.getSubject(), "UTF-8"); message.setFrom(new InternetAddress(emailBean.getFrom(), true)); message.setSentDate(new Date()); message.addRecipients(Message.RecipientType.TO, InternetAddress.parse(emailBean.getTo())); if (emailBean.getReplyTo() != null) { message.setReplyTo(InternetAddress.parse(emailBean.getReplyTo())); } LOG.info( "Sending from {} to {} with subject {}", emailBean.getFrom(), emailBean.getTo(), emailBean.getSubject()); Transport.send(message); LOG.debug("Mail sent"); } catch (Exception e) { LOG.error(e.getLocalizedMessage()); throw new SendMailException(e.getLocalizedMessage(), e); } finally { if (tempDir != null) { try { FileUtils.deleteDirectory(tempDir); } catch (IOException exception) { LOG.error(exception.getMessage()); } } } } @Inject  MailSender(MailSessionProvider mailSessionProvider, ExecutorServiceWrapper wrapper); void sendAsync(EmailBean emailBean); void sendMail(EmailBean emailBean); @PreDestroy void shutdown();  }
@Test public void shouldBeAbleToSendMessage() throws SendMailException { EmailBean emailBean = new EmailBean() .withFrom("noreply@cloud-ide.com") .withTo("dev-test@cloud-ide.com") .withReplyTo("dev-test@cloud-ide.com") .withSubject("Subject") .withMimeType("text/html") .withBody("hello user"); mailSender.sendMail(emailBean); assertMail( server, "noreply@cloud-ide.com", "dev-test@cloud-ide.com", "dev-test@cloud-ide.com", "Subject", "text/html", "hello user", null, null); }
@Test public void shouldBeAbleToSendMessageWithFormattedFields() throws SendMailException { EmailBean emailBean = new EmailBean() .withFrom("Exo IDE <noreply@cloud-ide.com>") .withTo("dev-test@cloud-ide.com") .withReplyTo("Developers to reply <dev-test@cloud-ide.com>") .withSubject("Subject") .withMimeType("text/html") .withBody("hello user"); mailSender.sendMail(emailBean); assertMail( server, "Exo IDE <noreply@cloud-ide.com>", "dev-test@cloud-ide.com", "Developers to reply <dev-test@cloud-ide.com>", "Subject", "text/html", "hello user", null, null); }
@Test public void shouldBeAbleToSendMessageToFewEmails() throws SendMailException { EmailBean emailBean = new EmailBean() .withFrom("noreply@cloud-ide.com") .withTo("dev-test@cloud-ide.com, dev-test1@cloud-ide.com, dev-test2@cloud-ide.com") .withReplyTo("dev-test@cloud-ide.com") .withSubject("Subject") .withMimeType("text/html") .withBody("hello user"); mailSender.sendMail(emailBean); assertMail( server, "noreply@cloud-ide.com", "dev-test@cloud-ide.com, dev-test1@cloud-ide.com, dev-test2@cloud-ide.com", "dev-test@cloud-ide.com", "Subject", "text/html", "hello user", null, null); }
@Test public void shouldBeAbleToSendMessageWithAttachment() throws SendMailException { EmailBean emailBean = new EmailBean() .withFrom("noreply@cloud-ide.com") .withTo("dev-test@cloud-ide.com") .withReplyTo("dev-test@cloud-ide.com") .withSubject("Subject") .withMimeType("text/html") .withBody("hello user"); Attachment attachment = new Attachment() .withContentId("attachmentId") .withFileName("attachment.txt") .withContent(Base64.getEncoder().encodeToString("attachmentContent".getBytes(UTF_8))); emailBean.setAttachments(Collections.singletonList(attachment)); mailSender.sendMail(emailBean); assertMail( server, "noreply@cloud-ide.com", "dev-test@cloud-ide.com", "dev-test@cloud-ide.com", "Subject", "text/html", "hello user", "attachmentId", "attachment.txt"); }