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;
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) {
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) {