@Test
public void testStartTLSWithJavamail() throws Exception {
InetSocketAddress address = new InetSocketAddress("127.0.0.1", TestUtils.getFreePort());
ProtocolServer server = null;
try {
TestMessageHook hook = new TestMessageHook();
server = createServer(createProtocol(hook) , address, Encryption.createStartTls(BogusSslContextFactory.getServerContext()));
server.bind();
Properties mailProps = new Properties();
mailProps.put("mail.smtp.from", "test@localhost");
mailProps.put("mail.smtp.host", address.getHostName());
mailProps.put("mail.smtp.port", address.getPort());
mailProps.put("mail.smtp.socketFactory.class", BogusSSLSocketFactory.class.getName());
mailProps.put("mail.smtp.socketFactory.fallback", "false");
mailProps.put("mail.smtp.starttls.enable", "true");
Session mailSession = Session.getDefaultInstance(mailProps);
MimeMessage message = new MimeMessage(mailSession);
message.setFrom(new InternetAddress("test@localhost"));
String[] emails = { "valid@localhost" };
Address rcpts[] = new Address[emails.length];
for (int i = 0; i < emails.length; i++) {
rcpts[i] = new InternetAddress(emails[i].trim().toLowerCase());
}
message.setRecipients(Message.RecipientType.TO, rcpts);
message.setSubject("Testmail", "UTF-8");
message.setText("Test.....");
SMTPTransport transport = (SMTPTransport) mailSession.getTransport("smtps");
transport.connect(new Socket(address.getHostName(), address.getPort()));
transport.sendMessage(message, rcpts);
assertEquals(1, hook.getQueued().size());
} finally {
if (server != null) {
server.unbind();
}
}
}