Tweaked EmailSender, fixed SSL prop in console + added option for StartTLS
This commit is contained in:
parent
86a414c39f
commit
7bbeddc2a3
3 changed files with 81 additions and 9 deletions
|
@ -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>
|
||||
|
|
|
@ -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) });
|
||||
}
|
||||
|
||||
|
|
|
@ -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");
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue