@Test public void disabled() throws MessagingException { mailSender.smtpEnabled = false; mailSender.init(); ExternalServiceStatus result = mailSender.getServiceStatus(); assertThat(result, is(ExternalServiceStatus.DISABLED)); verifyZeroInteractions(objectFactoryMock, transportMock); } }
/** * Initialize E-Mail service. * * @param sendTestMailIfEnabled * send a test email if this feature is enabled */ private void init(boolean sendTestMailIfEnabled) { if (!smtpEnabled) { return; } else if (log.isInfoEnabled()) { log.info("|-eMail Service initialized"); } parseRecipientsString(); onSmtpPropertiesChanged(sendTestMailIfEnabled); }
@Test(expectedExceptions = IllegalArgumentException.class) public void htmlBodyIsEmpty() throws Exception { try { mailSender.smtpEnabled = true; mailSender.sendEMail("subject", "", "textBody", Arrays.asList("three@example.com")); } finally { verifyZeroInteractions(mailMock, transportMock, objectFactoryMock); } }
/** * Parses the SMTP properties and checks whether a connection can be established. * * @param sendTestMailIfEnabled * send a test email if this feature is enabled */ private void onSmtpPropertiesChanged(boolean sendTestMailIfEnabled) { parseAdditionalPropertiesString(); checkConnection(sendTestMailIfEnabled && testMailEnabled); }
@Test public void notConnected() throws MessagingException { when(objectFactoryMock.getSmtpTransport()).thenReturn(transportMock); doThrow(MessagingException.class).when(transportMock).connect(any(String.class), any(Integer.class), any(String.class), any(String.class)); mailSender.smtpEnabled = true; mailSender.init(); boolean result = mailSender.isConnected(); assertThat(result, is(false)); verify(objectFactoryMock).getSmtpTransport(); verify(transportMock).connect(any(String.class), any(Integer.class), any(String.class), any(String.class)); verifyNoMoreInteractions(objectFactoryMock, transportMock); } }
@Test public void testMailFailedNoConnection() throws Exception { when(objectFactoryMock.getSmtpTransport()).thenReturn(transportMock); doThrow(MessagingException.class).when(transportMock).connect("host", 25, "user", "passwd"); mailSender.smtpHost = "host"; mailSender.smtpPort = 25; mailSender.smtpUser = "user"; mailSender.smtpPassword = "passwd"; mailSender.testMailEnabled = true; mailSender.testMailRecipient = "@test@example.com"; mailSender.smtpEnabled = true; mailSender.onSmtpPropertiesChanged(); verify(objectFactoryMock).getSmtpTransport(); verify(transportMock).connect("host", 25, "user", "passwd"); verifyNoMoreInteractions(mailMock, objectFactoryMock, transportMock); assertThat(mailSender.isConnected(), is(false)); assertThat(getAdditionalProperties().entrySet(), hasSize(0)); assertThat(getDefaultRecipients(), hasSize(0)); }
mailSender.senderName = "Sender Name"; mailSender.smtpEnabled = true; mailSender.init(); boolean result = mailSender.sendEMail("subject", "htmlBody", "textBody", Arrays.asList("three@example.com")); assertThat(mailSender.getServiceStatus(), is(ExternalServiceStatus.CONNECTED)); assertThat(mailSender.getServiceType(), is(ExternalServiceType.MAIL_SENDER)); verify(objectFactoryMock).getSmtpTransport(); verify(objectFactoryMock).createHtmlEmail();
@Test public void badSmtpPort() throws Exception { doThrow(IllegalArgumentException.class).when(mailMock).setSmtpPort(-1); when(objectFactoryMock.createHtmlEmail()).thenReturn(mailMock); when(objectFactoryMock.getSmtpTransport()).thenReturn(transportMock); mailSender.smtpHost = "host"; mailSender.smtpPort = -1; mailSender.smtpEnabled = true; mailSender.init(); boolean result = mailSender.sendEMail("subject", "htmlBody", "textBody", Arrays.asList("one@example.com")); assertThat(result, is(false)); verify(objectFactoryMock).getSmtpTransport(); verify(objectFactoryMock).createHtmlEmail(); verify(transportMock).connect(any(String.class), any(Integer.class), any(String.class), any(String.class)); verify(transportMock).close(); verify(mailMock).setHostName("host"); verify(mailMock).setSmtpPort(-1); verifyNoMoreInteractions(objectFactoryMock, transportMock, mailMock); }
@Test public void emptyRecipients() throws Exception { Session session = Session.getInstance(new Properties()); when(mailMock.getMailSession()).thenReturn(session); when(objectFactoryMock.createHtmlEmail()).thenReturn(mailMock); when(objectFactoryMock.getSmtpTransport()).thenReturn(transportMock); mailSender.defaultRecipientString = ""; mailSender.smtpHost = "host"; mailSender.smtpPort = 25; mailSender.smtpUser = "user"; mailSender.smtpPassword = "passwd"; mailSender.senderAddress = "sender@example.com"; mailSender.senderName = "Sender Name"; mailSender.smtpEnabled = true; mailSender.init(); boolean result = mailSender.sendEMail("subject", "htmlBody", "textBody", Collections.emptyList()); assertThat(result, is(false)); assertThat(mailSender.getServiceStatus(), is(ExternalServiceStatus.CONNECTED)); }
if (getServiceStatus() == ExternalServiceStatus.DISABLED) { return; } else if (!isConnected()) { if (log.isInfoEnabled()) { log.info("Cannot send a test e-mail because the SMTP connection is not established."); HtmlEmail htmlEmail = prepareHtmlEmail(Collections.singletonList(testMailRecipient), false); htmlEmail.setSubject("inspectIT Test E-Mail"); htmlEmail.setTextMsg("Hello, this is a test e-mail. You have successfully configured the SMTP server used by inspectIT!");
@Test public void notConnected() { mailSender.smtpEnabled = true; boolean result = mailSender.sendEMail("subject", "htmlBody", "textBody", Arrays.asList("one@example.com")); assertThat(result, is(false)); assertThat(mailSender.getServiceStatus(), is(ExternalServiceStatus.DISCONNECTED)); verifyZeroInteractions(mailMock); verifyZeroInteractions(transportMock); verifyZeroInteractions(objectFactoryMock); }
/** * Method which is executed after bean is created. */ @PostConstruct public void postConstruct() { init(false); }
/** * {@inheritDoc} */ public boolean isConnected() { return getServiceStatus() == ExternalServiceStatus.CONNECTED; }
/** * Parses the SMTP properties and checks whether a connection can be established. */ @PropertyUpdate(properties = { "mail.smtp.host", "mail.smtp.port", "mail.smtp.user", "mail.smtp.passwd", "mail.smtp.properties", "mail.test.enabled" }) public void onSmtpPropertiesChanged() { if (!smtpEnabled) { return; } onSmtpPropertiesChanged(true); }
/** * Prepares an email object including the default recipients. * * @param recipients * recipient to send to. * @return Returns a prepared {@link HtmlEmail} object. * @throws EmailException * is thrown when the from address could not be set */ private HtmlEmail prepareHtmlEmail(List<String> recipients) throws EmailException { return prepareHtmlEmail(recipients, true); }
@Test public void connected() throws MessagingException { when(objectFactoryMock.getSmtpTransport()).thenReturn(transportMock); mailSender.smtpEnabled = true; mailSender.init(); boolean result = mailSender.isConnected(); assertThat(result, is(true)); verify(objectFactoryMock).getSmtpTransport(); verify(transportMock).connect(any(String.class), any(Integer.class), any(String.class), any(String.class)); verify(transportMock).close(); verifyNoMoreInteractions(objectFactoryMock, transportMock); }
@Test public void testMailFailedNoRecipient() throws Exception { when(objectFactoryMock.getSmtpTransport()).thenReturn(transportMock); mailSender.smtpHost = "host"; mailSender.smtpPort = 25; mailSender.smtpUser = "user"; mailSender.smtpPassword = "passwd"; mailSender.testMailEnabled = true; mailSender.smtpEnabled = true; mailSender.onSmtpPropertiesChanged(); verify(objectFactoryMock).getSmtpTransport(); verify(transportMock).connect("host", 25, "user", "passwd"); verify(transportMock).close(); verifyNoMoreInteractions(mailMock, objectFactoryMock, transportMock); assertThat(mailSender.isConnected(), is(true)); assertThat(getAdditionalProperties().entrySet(), hasSize(0)); assertThat(getDefaultRecipients(), hasSize(0)); }
@Test public void badSenderAdress() throws Exception { when(objectFactoryMock.createHtmlEmail()).thenReturn(mailMock); when(objectFactoryMock.getSmtpTransport()).thenReturn(transportMock); doThrow(EmailException.class).when(mailMock).setFrom(eq("invalid"), any(String.class)); mailSender.defaultRecipientString = "one@example.com,two@example.com,invalid"; mailSender.smtpPropertiesString = "key1=val1,key2=val2,=noKey,noVal=,=,invalid"; mailSender.smtpHost = "host"; mailSender.smtpPort = 25; mailSender.smtpUser = "user"; mailSender.smtpPassword = "passwd"; mailSender.senderAddress = "invalid"; mailSender.senderName = "Sender Name"; mailSender.smtpEnabled = true; mailSender.init(); boolean result = mailSender.sendEMail("subject", "htmlBody", "textBody", Arrays.asList("three@example.com")); assertThat(result, is(false)); verify(objectFactoryMock).getSmtpTransport(); verify(objectFactoryMock).createHtmlEmail(); verify(transportMock).connect(any(String.class), any(Integer.class), any(String.class), any(String.class)); verify(transportMock).close(); verify(mailMock).setHostName("host"); verify(mailMock).setSmtpPort(25); verify(mailMock).setAuthentication("user", "passwd"); verify(mailMock).setFrom("invalid", "Sender Name"); verifyNoMoreInteractions(objectFactoryMock, transportMock, mailMock); }
/** * Initialize E-Mail service. */ @PropertyUpdate(properties = { "mail.enable" }) public void init() { init(true); }
HtmlEmail email = prepareHtmlEmail(recipients); email.setSubject(subject); email.setHtmlMsg(htmlMessage);