Address some edge cases related to bootstrapping a fresh system.
[cfb.git] / prod / net / jaekl / cfb / analyze / Notifier.java
index 62037279f6005e8b1252a0d13c71955b9146de48..7fdcfd31f1ccaff549b34e1fcce0d6ea2bf24346 100644 (file)
@@ -3,20 +3,15 @@ package net.jaekl.cfb.analyze;
 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.cfb.store.Run;
+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;
@@ -33,11 +28,32 @@ public class Notifier {
                        sendEmail(pw, report);
                }
        }
+
+       // --- end of public interface ---
+       
+       String constructSubject(HtmlReport report) {
+               String earlier;
+               Run earlierRun = report.getDelta().getEarlier();
+               if (null == earlierRun) {
+                       earlier = m_bundle.get(CfbBundle.NO_EARLIER_RUN);
+               }
+               else {
+                       earlier = report.getDelta().getEarlier().constructVersionText(m_bundle);
+               }
+               
+               String later = report.getDelta().getLater().constructVersionText(m_bundle);
+
+               String subject = m_bundle.get(CfbBundle.CFB_MAIL_SUBJECT, earlier, later);
+               return subject;
+       }
+
+       SendMail createSendMail() {
+               return new SendMail();
+       }
        
        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 = createSendMail();
+               sendMail.setSmtpHost(m_config.getMailSmtpHost());
                
                ArrayList<String> recipients = m_config.getNotify();
                if (recipients.size() < 1) {
@@ -47,27 +63,23 @@ public class Notifier {
                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.setSubject(constructSubject(report));
+                       sendMail.setFrom(m_config.getMailFrom());
                        
                        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) {