diff --git a/pom.xml b/pom.xml index e2ce4190bd..e537f9b0ba 100755 --- a/pom.xml +++ b/pom.xml @@ -226,6 +226,11 @@ javase 2.2 + + com.icegreen + greenmail + 1.3.1b + org.jboss.arquillian diff --git a/services/pom.xml b/services/pom.xml index 02f2c8ae9a..dce0df7d58 100755 --- a/services/pom.xml +++ b/services/pom.xml @@ -156,6 +156,11 @@ 3.6.6.Final test + + com.icegreen + greenmail + test + diff --git a/services/src/main/java/org/keycloak/services/email/EmailSender.java b/services/src/main/java/org/keycloak/services/email/EmailSender.java new file mode 100644 index 0000000000..da91a851cc --- /dev/null +++ b/services/src/main/java/org/keycloak/services/email/EmailSender.java @@ -0,0 +1,44 @@ +package org.keycloak.services.email; + +import java.util.Map.Entry; +import java.util.Properties; + +import javax.mail.Message; +import javax.mail.MessagingException; +import javax.mail.Session; +import javax.mail.Transport; +import javax.mail.internet.AddressException; +import javax.mail.internet.InternetAddress; +import javax.mail.internet.MimeMessage; + +public class EmailSender { + + private Properties properties; + + public EmailSender() { + properties = new Properties(); + for (Entry e : System.getProperties().entrySet()) { + String key = (String) e.getKey(); + if (key.startsWith("keycloak.mail.smtp.")) { + key = key.replace("keycloak.mail.smtp.", "mail.smtp."); + properties.put(key, e.getValue()); + } + } + } + + public void send(String address, String subject, String body) throws AddressException, MessagingException { + + Session session = Session.getDefaultInstance(properties); + + Message msg = new MimeMessage(session); + msg.setFrom(new InternetAddress(properties.getProperty("mail.smtp.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")); + 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 new file mode 100644 index 0000000000..a2f09614bb --- /dev/null +++ b/services/src/test/java/org/keycloak/services/email/EmailSenderTest.java @@ -0,0 +1,55 @@ +package org.keycloak.services.email; + +import java.io.IOException; + +import javax.mail.MessagingException; +import javax.mail.internet.AddressException; +import javax.mail.internet.MimeMessage; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import com.icegreen.greenmail.util.GreenMail; +import com.icegreen.greenmail.util.ServerSetup; + +public class EmailSenderTest { + + private GreenMail greenMail; + + @Before + public void before() { + ServerSetup setup = new ServerSetup(3025, "localhost", "smtp"); + + greenMail = new GreenMail(setup); + greenMail.start(); + + System.setProperty("keycloak.mail.smtp.from", "auto@keycloak.org"); + System.setProperty("keycloak.mail.smtp.host", "localhost"); + System.setProperty("keycloak.mail.smtp.port", "3025"); + } + + @After + public void after() throws InterruptedException { + if (greenMail != null) { + greenMail.stop(); + } + } + + @Test + public void sendMail() throws AddressException, MessagingException, IOException { + EmailSender emailSender = new EmailSender(); + emailSender.send("test@test.com", "Test subject", "Test body"); + + MimeMessage[] receivedMessages = greenMail.getReceivedMessages(); + Assert.assertEquals(1, receivedMessages.length); + + MimeMessage msg = receivedMessages[0]; + Assert.assertEquals(1, msg.getFrom().length); + Assert.assertEquals("auto@keycloak.org", msg.getFrom()[0].toString()); + Assert.assertEquals("Test subject", msg.getSubject()); + Assert.assertEquals("Test body", ((String) msg.getContent()).trim()); + } + +}