🔥 Remove unused parameter
[rhynodge.git] / src / main / java / net / pterodactylus / rhynodge / actions / EmailAction.java
index 6e866c8..e1441f3 100644 (file)
 
 package net.pterodactylus.rhynodge.actions;
 
+import static java.lang.System.getProperties;
+import static javax.mail.Session.getInstance;
+
 import java.util.Properties;
 
 import javax.mail.Message.RecipientType;
 import javax.mail.MessagingException;
 import javax.mail.Session;
 import javax.mail.Transport;
+import javax.mail.URLName;
 import javax.mail.internet.InternetAddress;
 import javax.mail.internet.MimeBodyPart;
 import javax.mail.internet.MimeMessage;
@@ -31,6 +35,10 @@ import javax.mail.internet.MimeMultipart;
 import net.pterodactylus.rhynodge.Action;
 import net.pterodactylus.rhynodge.output.Output;
 
+import com.google.common.annotations.VisibleForTesting;
+import com.sun.mail.smtp.SMTPTransport;
+import org.apache.log4j.Logger;
+
 /**
  * {@link Action} implementation that sends an email containing the triggering
  * object to an email address.
@@ -39,8 +47,7 @@ import net.pterodactylus.rhynodge.output.Output;
  */
 public class EmailAction implements Action {
 
-       /** The name of the SMTP host. */
-       private final String hostname;
+       private static final Logger logger = Logger.getLogger(EmailAction.class);
 
        /** The email address of the sender. */
        private final String sender;
@@ -48,6 +55,9 @@ public class EmailAction implements Action {
        /** The email address of the recipient. */
        private final String recipient;
 
+       private final Transport transport;
+       private final Session session;
+
        /**
         * Creates a new email action.
         *
@@ -59,9 +69,20 @@ public class EmailAction implements Action {
         *            The email address of the recipient
         */
        public EmailAction(String hostname, String sender, String recipient) {
-               this.hostname = hostname;
                this.sender = sender;
                this.recipient = recipient;
+               Properties properties = getProperties();
+               properties.put("mail.smtp.host", hostname);
+               session = getInstance(properties);
+               transport = new SMTPTransport(session, new URLName("smtp", hostname, 25, null, "", ""));
+       }
+
+       @VisibleForTesting
+       EmailAction(Transport transport, String sender, String recipient) {
+               this.transport = transport;
+               this.sender = sender;
+               this.recipient = recipient;
+               this.session = getInstance(getProperties());
        }
 
        //
@@ -73,31 +94,45 @@ public class EmailAction implements Action {
         */
        @Override
        public void execute(Output output) {
-               Properties properties = System.getProperties();
-               properties.put("mail.smtp.host", hostname);
-               Session session = Session.getInstance(properties);
                MimeMessage message = new MimeMessage(session);
                try {
                        /* create message. */
                        message.setFrom(new InternetAddress(sender));
                        message.setRecipient(RecipientType.TO, new InternetAddress(recipient));
-                       message.setSubject(output.summary());
+                       message.setSubject(output.summary(), "UTF-8");
 
                        /* create text and html parts. */
                        MimeMultipart multipart = new MimeMultipart();
                        multipart.setSubType("alternative");
-                       MimeBodyPart textPart = new MimeBodyPart();
-                       textPart.setContent(output.text("text/plain", -1), "text/plain;charset=utf-8");
-                       MimeBodyPart htmlPart = new MimeBodyPart();
-                       htmlPart.setContent(output.text("text/html", -1), "text/html;charset=utf-8");
-                       multipart.addBodyPart(textPart);
-                       multipart.addBodyPart(htmlPart);
+                       addPlainTextPart(output, multipart);
+                       addHtmlPart(output, multipart);
                        message.setContent(multipart);
 
-                       Transport.send(message);
+                       if (!transport.isConnected()) {
+                               transport.connect();
+                       }
+                       transport.sendMessage(message, message.getAllRecipients());
                } catch (MessagingException me1) {
-                       /* swallow. */
+                       logger.error("Could not send email!", me1);
+               }
+       }
+
+       private void addPlainTextPart(Output output, MimeMultipart multipart) throws MessagingException {
+               if (output.text("text/plain") == null) {
+                       return;
+               }
+               MimeBodyPart textPart = new MimeBodyPart();
+               textPart.setContent(output.text("text/plain"), "text/plain;charset=utf-8");
+               multipart.addBodyPart(textPart);
+       }
+
+       private void addHtmlPart(Output output, MimeMultipart multipart) throws MessagingException {
+               if (output.text("text/html") == null) {
+                       return;
                }
+               MimeBodyPart htmlPart = new MimeBodyPart();
+               htmlPart.setContent(output.text("text/html"), "text/html;charset=utf-8");
+               multipart.addBodyPart(htmlPart);
        }
 
 }