Example usage for org.springframework.mail.javamail JavaMailSenderImpl getPort

List of usage examples for org.springframework.mail.javamail JavaMailSenderImpl getPort

Introduction

In this page you can find the example usage for org.springframework.mail.javamail JavaMailSenderImpl getPort.

Prototype

public int getPort() 

Source Link

Document

Return the mail server port.

Usage

From source file:com.formkiq.core.service.notification.MailSenderServiceTest.java

/**
 * testAfterPropertiesSet01().// ww  w. j a  v  a 2  s.  c om
 * expect JavaMailSenderImpl
 * @throws Exception Exception
 */
@Test
public void testAfterPropertiesSet01() throws Exception {
    // given
    final int port = 587;
    String data = "mail.host=smtp.gmail.com\n" + "mail.port=" + port + "\n" + "mail.username=test\n"
            + "mail.password=pass\n" + "mail.smtp.auth=true\n" + "mail.smtp.auth=true\n"
            + "mail.smtp.starttls.enable=true\n" + "mail.smtp.quitwait=false";

    InputStream is = new ByteArrayInputStream(data.getBytes(StandardCharsets.UTF_8));

    MailSenderService ms = new MailSenderService();

    // when
    ms.afterPropertiesSet(is);

    // then
    assertTrue(ms.getMailSender() instanceof JavaMailSenderImpl);
    JavaMailSenderImpl js = (JavaMailSenderImpl) ms.getMailSender();
    assertEquals("smtp.gmail.com", js.getHost());
    assertEquals(port, js.getPort());
    assertEquals("test", js.getUsername());
    assertEquals("true", js.getJavaMailProperties().getProperty("mail.smtp.starttls.enable"));
    assertEquals("false", js.getJavaMailProperties().getProperty("mail.smtp.quitwait"));
    assertEquals("true", js.getJavaMailProperties().getProperty("mail.smtp.auth"));
}

From source file:com.formkiq.core.service.notification.MailSenderService.java

/**
 * @param prop {@link Properties}/*from   w  w  w  .j  a v  a2 s .c  om*/
 * @return {@link JavaMailSenderImpl}
 * @throws IOException IOException
 */
private JavaMailSenderImpl createJavaMailSender(final Properties prop) throws IOException {

    JavaMailSenderImpl sender = new JavaMailSenderImpl();

    sender.setHost(prop.getProperty("mail.host"));
    sender.setPort(Integer.parseInt(prop.getProperty("mail.port")));

    sender.setUsername(prop.getProperty("mail.username"));
    sender.setPassword(prop.getProperty("mail.password"));

    Properties property = sender.getJavaMailProperties();

    if (prop.containsKey("mail.smtp.auth")) {
        property.setProperty("mail.smtp.auth", prop.getProperty("mail.smtp.auth"));
    }

    if (prop.containsKey("mail.smtp.starttls.enable")) {
        property.setProperty("mail.smtp.starttls.enable", prop.getProperty("mail.smtp.starttls.enable"));
    }

    if (prop.containsKey("mail.smtp.quitwait")) {
        property.setProperty("mail.smtp.quitwait", prop.getProperty("mail.smtp.quitwait"));
    }

    LOG.log(Level.INFO, "Setting mail.host = " + sender.getHost() + ":" + sender.getPort());
    LOG.log(Level.INFO, "Setting mail.username = " + sender.getUsername());

    for (Map.Entry<Object, Object> e : sender.getJavaMailProperties().entrySet()) {
        LOG.log(Level.INFO, "Setting " + e.getKey() + " = " + e.getValue());
    }

    return sender;
}

From source file:com.springsource.insight.plugin.mail.MessageSendOperationCollectionAspectTest.java

private void testSendMessage(int port) {
    JavaMailSenderImpl sender = new JavaMailSenderImpl();
    sender.setHost(NetworkAddressUtil.LOOPBACK_ADDRESS);
    sender.setProtocol(JavaMailSenderImpl.DEFAULT_PROTOCOL);
    sender.setPort(port);//  w  ww  .  j ava 2  s  .co  m

    SimpleMailMessage message = new SimpleMailMessage();
    message.setFrom("from@com.springsource.insight.plugin.mail");
    message.setTo("to@com.springsource.insight.plugin.mail");
    message.setCc("cc@com.springsource.insight.plugin.mail");
    message.setBcc("bcc@com.springsource.insight.plugin.mail");

    Date now = new Date(System.currentTimeMillis());
    message.setSentDate(now);
    message.setSubject(now.toString());
    message.setText("Test at " + now.toString());
    sender.send(message);

    Operation op = getLastEntered();
    assertNotNull("No operation extracted", op);
    assertEquals("Mismatched operation type", MailDefinitions.SEND_OPERATION, op.getType());
    assertEquals("Mismatched protocol", sender.getProtocol(),
            op.get(MailDefinitions.SEND_PROTOCOL, String.class));
    assertEquals("Mismatched host", sender.getHost(), op.get(MailDefinitions.SEND_HOST, String.class));
    if (port == -1) {
        assertEquals("Mismatched default port", 25, op.getInt(MailDefinitions.SEND_PORT, (-1)));
    } else {
        assertEquals("Mismatched send port", sender.getPort(), op.getInt(MailDefinitions.SEND_PORT, (-1)));
    }

    if (getAspect().collectExtraInformation()) {
        assertAddresses(op, MailDefinitions.SEND_SENDERS, 1);
        assertAddresses(op, MailDefinitions.SEND_RECIPS, 3);

        OperationMap details = op.get(MailDefinitions.SEND_DETAILS, OperationMap.class);
        assertNotNull("No details extracted", details);
        assertEquals("Mismatched subject", message.getSubject(),
                details.get(MailDefinitions.SEND_SUBJECT, String.class));
    }
}