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;
|
|
|
|
|
|
|
|
|
|
import com.commafeed.backend.model.ApplicationSettings;
|
|
|
|
|
import com.commafeed.backend.model.User;
|
|
|
|
|
|
2013-07-26 16:00:02 +02:00
|
|
|
/**
|
|
|
|
|
* Mailing service
|
|
|
|
|
*
|
|
|
|
|
*/
|
2013-05-20 21:53:13 +02:00
|
|
|
@SuppressWarnings("serial")
|
|
|
|
|
public class MailService implements Serializable {
|
|
|
|
|
|
2013-04-12 13:27:30 +02:00
|
|
|
@Inject
|
|
|
|
|
ApplicationSettingsService applicationSettingsService;
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
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"));
|
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);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|