import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;
-import java.util.Properties;
-
-import javax.mail.Message.RecipientType;
-import javax.mail.MessagingException;
-import javax.mail.Session;
-import javax.mail.Transport;
-import javax.mail.internet.InternetAddress;
-import javax.mail.internet.MimeMessage;
import net.jaekl.cfb.CfbBundle;
import net.jaekl.cfb.Config;
+import net.jaekl.qd.util.MailException;
+import net.jaekl.qd.util.MimePart;
+import net.jaekl.qd.util.SendMail;
public class Notifier {
- private static final String MAIL_SMTP_HOST = "mail.smtp.host";
private static final String TEXT_HTML = "text/html";
CfbBundle m_bundle;
}
void sendEmail(PrintWriter pw, HtmlReport report) {
- Properties props = System.getProperties();
- props.setProperty(MAIL_SMTP_HOST, m_config.getMailSmtpHost());
- Session sess = Session.getDefaultInstance(props);
+ SendMail sendMail = new SendMail();
+
+ sendMail.setSmtpHost(m_config.getMailSmtpHost());
ArrayList<String> recipients = m_config.getNotify();
if (recipients.size() < 1) {
PrintWriter mailWriter = null;
try {
- MimeMessage msg = new MimeMessage(sess);
-
String earlier = report.getDelta().getEarlier().constructVersionText(m_bundle);
String later = report.getDelta().getLater().constructVersionText(m_bundle);
- msg.setFrom(new InternetAddress(m_config.getMailFrom()));
- msg.setSubject(m_bundle.get(CfbBundle.CFB_MAIL_SUBJECT, earlier, later));
+ sendMail.setFrom(m_config.getMailFrom());
+ sendMail.setSubject(m_bundle.get(CfbBundle.CFB_MAIL_SUBJECT, earlier, later));
for (String recipient : recipients) {
- msg.addRecipient(RecipientType.TO, new InternetAddress(recipient));
+ sendMail.addTo(recipient);
}
StringWriter sw = new StringWriter();
mailWriter = new PrintWriter(sw);
report.write(mailWriter);
mailWriter.flush();
-
- msg.setContent(sw.toString(), TEXT_HTML);
- Transport.send(msg);
+
+ MimePart part = new MimePart(TEXT_HTML, sw.toString());
+ sendMail.addPart(part);
+ sendMail.send();
}
- catch (MessagingException exc) {
+ catch (MailException exc) {
StringBuilder toList = new StringBuilder();
for (String recipient : recipients) {
if (toList.length() > 0) {