KEYCLOAK-61 Send emails
This commit is contained in:
parent
6c124a2172
commit
e5a81653d9
4 changed files with 109 additions and 0 deletions
5
pom.xml
5
pom.xml
|
@ -226,6 +226,11 @@
|
||||||
<artifactId>javase</artifactId>
|
<artifactId>javase</artifactId>
|
||||||
<version>2.2</version>
|
<version>2.2</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.icegreen</groupId>
|
||||||
|
<artifactId>greenmail</artifactId>
|
||||||
|
<version>1.3.1b</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.jboss.arquillian</groupId>
|
<groupId>org.jboss.arquillian</groupId>
|
||||||
|
|
|
@ -156,6 +156,11 @@
|
||||||
<version>3.6.6.Final</version>
|
<version>3.6.6.Final</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.icegreen</groupId>
|
||||||
|
<artifactId>greenmail</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
<build>
|
<build>
|
||||||
<plugins>
|
<plugins>
|
||||||
|
|
|
@ -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<Object, Object> 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) });
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -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());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in a new issue