diff --git a/admin-ui/src/main/resources/META-INF/resources/admin/partials/realm-smtp.html b/admin-ui/src/main/resources/META-INF/resources/admin/partials/realm-smtp.html
index b3e6d80..6abd4ee 100755
--- a/admin-ui/src/main/resources/META-INF/resources/admin/partials/realm-smtp.html
+++ b/admin-ui/src/main/resources/META-INF/resources/admin/partials/realm-smtp.html
@@ -43,7 +43,7 @@
<div class="form-group clearfix">
<label for="smtpSSL" class="control-label">Enable SSL</label>
<div class="onoffswitch">
- <input type="checkbox" data-ng-model="realm.smtpServer.ssl.enable" class="onoffswitch-checkbox" name="smtpSSL" id="smtpSSL">
+ <input type="checkbox" data-ng-model="realm.smtpServer.ssl" class="onoffswitch-checkbox" name="smtpSSL" id="smtpSSL">
<label for="smtpSSL" class="onoffswitch-label">
<span class="onoffswitch-inner">
<span class="onoffswitch-active">ON</span>
@@ -53,6 +53,19 @@
</label>
</div>
</div>
+ <div class="form-group clearfix">
+ <label for="smtpStartTLS" class="control-label">Enable StartTLS</label>
+ <div class="onoffswitch">
+ <input type="checkbox" data-ng-model="realm.smtpServer.starttls" class="onoffswitch-checkbox" name="smtpStartTLS" id="smtpStartTLS">
+ <label for="smtpStartTLS" class="onoffswitch-label">
+ <span class="onoffswitch-inner">
+ <span class="onoffswitch-active">ON</span>
+ <span class="onoffswitch-inactive">OFF</span>
+ </span>
+ <span class="onoffswitch-switch"></span>
+ </label>
+ </div>
+ </div>
</fieldset>
<fieldset>
<legend collapsed><span class="text">Authentication</span></legend>
diff --git a/services/src/main/java/org/keycloak/services/email/EmailSender.java b/services/src/main/java/org/keycloak/services/email/EmailSender.java
index 872f1fe..960410c 100755
--- a/services/src/main/java/org/keycloak/services/email/EmailSender.java
+++ b/services/src/main/java/org/keycloak/services/email/EmailSender.java
@@ -51,26 +51,53 @@ public class EmailSender {
private static final Logger log = Logger.getLogger(EmailSender.class);
- private Properties properties;
+ private Map<String, String> config;
public EmailSender(Map<String, String> config) {
- properties = new Properties();
- for (Entry<String, String> e : config.entrySet()) {
- properties.put("mail.smtp." + e.getKey(), e.getValue());
- }
+ this.config = config;
}
public void send(String address, String subject, String body) throws MessagingException {
- Session session = Session.getInstance(properties);
+ Properties props = new Properties();
+ props.setProperty("mail.smtp.host", config.get("host"));
+
+ boolean auth = "true".equals(config.get("auth"));
+ boolean ssl = "true".equals(config.get("ssl"));
+ boolean starttls = "true".equals(config.get("starttls"));
+
+ if (config.containsKey("port")) {
+ props.setProperty("mail.smtp.port", config.get("port"));
+ }
+
+ if (auth) {
+ props.put("mail.smtp.auth", "true");
+ }
+
+ if (ssl) {
+ props.put("mail.smtp.socketFactory.port", config.get("port"));
+ props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
+ }
+
+ if (starttls) {
+ props.put("mail.smtp.starttls.enable", "true");
+ }
+
+ String from = config.get("from");
+
+ Session session = Session.getInstance(props);
Message msg = new MimeMessage(session);
- msg.setFrom(new InternetAddress(properties.getProperty("mail.smtp.from")));
+ msg.setFrom(new InternetAddress(from));
msg.setSubject(subject);
msg.setText(body);
msg.saveChanges();
Transport transport = session.getTransport("smtp");
- transport.connect(properties.getProperty("mail.smtp.user"), properties.getProperty("mail.smtp.password"));
+ if (auth) {
+ transport.connect(config.get("user"), config.get("password"));
+ } else {
+ transport.connect();
+ }
transport.sendMessage(msg, new InternetAddress[] { new InternetAddress(address) });
}
diff --git a/services/src/test/java/org/keycloak/services/email/EmailSenderTest.java b/services/src/test/java/org/keycloak/services/email/EmailSenderTest.java
index 720af17..77863f5 100644
--- a/services/src/test/java/org/keycloak/services/email/EmailSenderTest.java
+++ b/services/src/test/java/org/keycloak/services/email/EmailSenderTest.java
@@ -70,4 +70,36 @@ public class EmailSenderTest {
Assert.assertEquals("Test body", ((String) msg.getContent()).trim());
}
+ @Test
+ public void googleTTLS() throws MessagingException, IOException {
+ HashMap<String,String> config = new HashMap<String, String>();
+ config.put("from", "stianst@gmail.com");
+ config.put("host", "smtp.gmail.com");
+ config.put("port", "587");
+ config.put("auth", "true");
+ config.put("user", "stianst@gmail.com");
+ config.put("password", "ahqsbktqbfhwmhrw");
+ config.put("starttls", "true");
+
+ emailSender = new EmailSender(config);
+
+ emailSender.send("stianst@gmail.com", "TTLS " + System.currentTimeMillis(), "Test body");
+ }
+
+ @Test
+ public void googleSSL() throws MessagingException, IOException {
+ HashMap<String,String> config = new HashMap<String, String>();
+ config.put("from", "stianst@gmail.com");
+ config.put("host", "smtp.gmail.com");
+ config.put("port", "465");
+ config.put("auth", "true");
+ config.put("user", "stianst@gmail.com");
+ config.put("password", "ahqsbktqbfhwmhrw");
+ config.put("ssl", "true");
+
+ emailSender = new EmailSender(config);
+
+ emailSender.send("stianst@gmail.com", "SSL " + System.currentTimeMillis(), "Test body");
+ }
+
}