2014-08-08 16:49:02 +02:00
|
|
|
package com.commafeed.backend.service;
|
2013-04-12 13:27:30 +02:00
|
|
|
|
|
|
|
|
import java.util.Properties;
|
|
|
|
|
|
2013-05-20 21:53:13 +02:00
|
|
|
import javax.mail.Authenticator;
|
2013-04-12 13:27:30 +02:00
|
|
|
import javax.mail.Message;
|
|
|
|
|
import javax.mail.PasswordAuthentication;
|
|
|
|
|
import javax.mail.Session;
|
|
|
|
|
import javax.mail.Transport;
|
|
|
|
|
import javax.mail.internet.InternetAddress;
|
|
|
|
|
import javax.mail.internet.MimeMessage;
|
|
|
|
|
|
2014-08-11 14:55:41 +02:00
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
|
|
|
2014-08-08 16:49:02 +02:00
|
|
|
import com.commafeed.CommaFeedConfiguration;
|
|
|
|
|
import com.commafeed.CommaFeedConfiguration.ApplicationSettings;
|
2013-04-12 13:27:30 +02:00
|
|
|
import com.commafeed.backend.model.User;
|
|
|
|
|
|
2013-07-26 16:00:02 +02:00
|
|
|
/**
|
|
|
|
|
* Mailing service
|
|
|
|
|
*
|
|
|
|
|
*/
|
2014-08-11 14:55:41 +02:00
|
|
|
@RequiredArgsConstructor
|
2014-08-14 08:38:13 +02:00
|
|
|
public class MailService {
|
2013-05-20 21:53:13 +02:00
|
|
|
|
2014-08-11 14:55:41 +02:00
|
|
|
private final CommaFeedConfiguration config;
|
2013-04-12 13:27:30 +02:00
|
|
|
|
2013-07-25 09:17:33 +02:00
|
|
|
public void sendMail(User user, String subject, String content) throws Exception {
|
2013-04-12 13:27:30 +02:00
|
|
|
|
2014-08-08 16:49:02 +02:00
|
|
|
ApplicationSettings settings = config.getApplicationSettings();
|
2013-05-20 21:53:13 +02:00
|
|
|
|
2013-04-12 13:27:30 +02:00
|
|
|
final String username = settings.getSmtpUserName();
|
|
|
|
|
final String password = settings.getSmtpPassword();
|
2013-05-20 21:53:13 +02:00
|
|
|
|
2013-04-12 13:27:30 +02:00
|
|
|
String dest = user.getEmail();
|
|
|
|
|
|
|
|
|
|
Properties props = new Properties();
|
|
|
|
|
props.put("mail.smtp.auth", "true");
|
2013-05-20 21:53:13 +02:00
|
|
|
props.put("mail.smtp.starttls.enable", "" + settings.isSmtpTls());
|
2013-04-12 13:27:30 +02:00
|
|
|
props.put("mail.smtp.host", settings.getSmtpHost());
|
2013-05-20 21:53:13 +02:00
|
|
|
props.put("mail.smtp.port", "" + settings.getSmtpPort());
|
|
|
|
|
|
|
|
|
|
Session session = Session.getInstance(props, new Authenticator() {
|
2014-08-08 16:49:02 +02:00
|
|
|
@Override
|
2013-05-20 21:53:13 +02:00
|
|
|
protected PasswordAuthentication getPasswordAuthentication() {
|
|
|
|
|
return new PasswordAuthentication(username, password);
|
|
|
|
|
}
|
|
|
|
|
});
|
2013-04-12 13:27:30 +02:00
|
|
|
|
|
|
|
|
Message message = new MimeMessage(session);
|
|
|
|
|
message.setFrom(new InternetAddress(username, "CommaFeed"));
|
2013-07-25 09:17:33 +02:00
|
|
|
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(dest));
|
2013-04-12 13:27:30 +02:00
|
|
|
message.setSubject("CommaFeed - " + subject);
|
2013-05-21 07:51:37 +02:00
|
|
|
message.setContent(content, "text/html; charset=utf-8");
|
2013-04-12 13:27:30 +02:00
|
|
|
|
|
|
|
|
Transport.send(message);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|