Switch from javax.mail to net.jaekl.qd.SendMail.
[cfb.git] / prod / net / jaekl / cfb / analyze / Notifier.java
index 62037279f6005e8b1252a0d13c71955b9146de48..fbbaabc087bcf78cd24c6e26fc84451b8e7b2a1c 100644 (file)
@@ -3,20 +3,14 @@ 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.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;
@@ -35,9 +29,9 @@ public class Notifier {
        }
        
        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) {
@@ -47,27 +41,26 @@ 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.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(sw.toString(), TEXT_HTML);
+                       sendMail.addPart(part);
+                       sendMail.send();
                }
-               catch (MessagingException exc) {
+               catch (MailException exc) {
                        StringBuilder toList = new StringBuilder();
                        for (String recipient : recipients) {
                                if (toList.length() > 0) {