@Test public void testEmailAlerterSendsMailSuccessfullyUsingMockTransport() throws Exception { expect(emailAlertsConfigService.getEmailAlertsConfig()) .andReturn(someEmailAlertsConfigWithAlertsEnabledAndSmtpConfig()); PowerMock.mockStatic(Transport.class); Transport.send(EasyMock.anyObject(Message.class)); PowerMock.replayAll(); final EmailAlerter emailAlerter = new EmailAlerter(emailAlertsConfigService); emailAlerter.sendMessage(EMAIL_SUBJECT, EMAIL_MSG); PowerMock.verifyAll(); }
public void sendMessage(String subject, String msgContent) { if (sendEmailAlertsEnabled) { final Session session = Session.getInstance( smtpProps, new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication( smtpConfig.getAccountUsername(), smtpConfig.getAccountPassword()); } }); try { final Message message = new MimeMessage(session); message.setFrom(new InternetAddress(smtpConfig.getFromAddress())); message.setRecipients( Message.RecipientType.TO, InternetAddress.parse(smtpConfig.getToAddress())); message.setSubject(subject); message.setText(msgContent); LOG.info(() -> "About to send following Email Alert with message content: " + msgContent); Transport.send(message); } catch (MessagingException e) { LOG.error(() -> "Failed to send Email Alert. Details: " + e.getMessage(), e); } } else { LOG.warn( () -> "Email Alerts are disabled. Not sending the following message: Subject: " + subject + " Content: " + msgContent); } }
EmailAlerter { public void sendMessage(String subject, String msgContent) { if (sendEmailAlertsEnabled) { final Session session = Session.getInstance( smtpProps, new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication( smtpConfig.getAccountUsername(), smtpConfig.getAccountPassword()); } }); try { final Message message = new MimeMessage(session); message.setFrom(new InternetAddress(smtpConfig.getFromAddress())); message.setRecipients( Message.RecipientType.TO, InternetAddress.parse(smtpConfig.getToAddress())); message.setSubject(subject); message.setText(msgContent); LOG.info(() -> "About to send following Email Alert with message content: " + msgContent); Transport.send(message); } catch (MessagingException e) { LOG.error(() -> "Failed to send Email Alert. Details: " + e.getMessage(), e); } } else { LOG.warn( () -> "Email Alerts are disabled. Not sending the following message: Subject: " + subject + " Content: " + msgContent); } } }
EmailAlerter { public void sendMessage(String subject, String msgContent) { if (sendEmailAlertsEnabled) { final Session session = Session.getInstance( smtpProps, new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication( smtpConfig.getAccountUsername(), smtpConfig.getAccountPassword()); } }); try { final Message message = new MimeMessage(session); message.setFrom(new InternetAddress(smtpConfig.getFromAddress())); message.setRecipients( Message.RecipientType.TO, InternetAddress.parse(smtpConfig.getToAddress())); message.setSubject(subject); message.setText(msgContent); LOG.info(() -> "About to send following Email Alert with message content: " + msgContent); Transport.send(message); } catch (MessagingException e) { LOG.error(() -> "Failed to send Email Alert. Details: " + e.getMessage(), e); } } else { LOG.warn( () -> "Email Alerts are disabled. Not sending the following message: Subject: " + subject + " Content: " + msgContent); } } @Autowired  EmailAlerter(EmailAlertsConfigService emailAlertsConfigService); }
EmailAlerter { public void sendMessage(String subject, String msgContent) { if (sendEmailAlertsEnabled) { final Session session = Session.getInstance( smtpProps, new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication( smtpConfig.getAccountUsername(), smtpConfig.getAccountPassword()); } }); try { final Message message = new MimeMessage(session); message.setFrom(new InternetAddress(smtpConfig.getFromAddress())); message.setRecipients( Message.RecipientType.TO, InternetAddress.parse(smtpConfig.getToAddress())); message.setSubject(subject); message.setText(msgContent); LOG.info(() -> "About to send following Email Alert with message content: " + msgContent); Transport.send(message); } catch (MessagingException e) { LOG.error(() -> "Failed to send Email Alert. Details: " + e.getMessage(), e); } } else { LOG.warn( () -> "Email Alerts are disabled. Not sending the following message: Subject: " + subject + " Content: " + msgContent); } } @Autowired  EmailAlerter(EmailAlertsConfigService emailAlertsConfigService); void sendMessage(String subject, String msgContent); }
EmailAlerter { public void sendMessage(String subject, String msgContent) { if (sendEmailAlertsEnabled) { final Session session = Session.getInstance( smtpProps, new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication( smtpConfig.getAccountUsername(), smtpConfig.getAccountPassword()); } }); try { final Message message = new MimeMessage(session); message.setFrom(new InternetAddress(smtpConfig.getFromAddress())); message.setRecipients( Message.RecipientType.TO, InternetAddress.parse(smtpConfig.getToAddress())); message.setSubject(subject); message.setText(msgContent); LOG.info(() -> "About to send following Email Alert with message content: " + msgContent); Transport.send(message); } catch (MessagingException e) { LOG.error(() -> "Failed to send Email Alert. Details: " + e.getMessage(), e); } } else { LOG.warn( () -> "Email Alerts are disabled. Not sending the following message: Subject: " + subject + " Content: " + msgContent); } } @Autowired  EmailAlerter(EmailAlertsConfigService emailAlertsConfigService); void sendMessage(String subject, String msgContent);  }