2013-04-12 13:27:30 +02:00
|
|
|
package com.commafeed.backend.services;
|
|
|
|
|
|
2013-05-20 21:53:13 +02:00
|
|
|
import java.io.Serializable;
|
2013-04-12 13:27:30 +02:00
|
|
|
import java.util.Properties;
|
|
|
|
|
|
|
|
|
|
import javax.inject.Inject;
|
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;
|
|
|
|
|
|
2013-05-20 21:53:13 +02:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
2013-04-12 13:27:30 +02:00
|
|
|
import com.commafeed.backend.model.ApplicationSettings;
|
|
|
|
|
import com.commafeed.backend.model.User;
|
|
|
|
|
|
2013-05-20 21:53:13 +02:00
|
|
|
@SuppressWarnings("serial")
|
|
|
|
|
public class MailService implements Serializable {
|
|
|
|
|
|
2013-05-21 08:45:51 +02:00
|
|
|
protected static Logger log = LoggerFactory.getLogger(MailService.class);
|
2013-04-12 13:27:30 +02:00
|
|
|
|
|
|
|
|
@Inject
|
|
|
|
|
ApplicationSettingsService applicationSettingsService;
|
|
|
|
|
|
2013-05-20 21:53:13 +02:00
|
|
|
public void sendMail(User user, String subject, String content)
|
|
|
|
|
throws Exception {
|
2013-04-12 13:27:30 +02:00
|
|
|
|
|
|
|
|
ApplicationSettings settings = applicationSettingsService.get();
|
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() {
|
|
|
|
|
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"));
|
|
|
|
|
message.setRecipients(Message.RecipientType.TO,
|
|
|
|
|
InternetAddress.parse(dest));
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|