Only add mail parts if they’re necessary.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 31 Jul 2014 11:12:57 +0000 (13:12 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 31 Jul 2014 11:12:57 +0000 (13:12 +0200)
src/main/java/net/pterodactylus/rhynodge/actions/EmailAction.java

index 0a0aca7..b781f4d 100644 (file)
@@ -101,12 +101,8 @@ public class EmailAction implements Action {
                        /* 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.connect();
@@ -116,4 +112,22 @@ public class EmailAction implements Action {
                }
        }
 
+       private void addPlainTextPart(Output output, MimeMultipart multipart) throws MessagingException {
+               if (output.text("text/plain", -1) == null) {
+                       return;
+               }
+               MimeBodyPart textPart = new MimeBodyPart();
+               textPart.setContent(output.text("text/plain", -1), "text/plain;charset=utf-8");
+               multipart.addBodyPart(textPart);
+       }
+
+       private void addHtmlPart(Output output, MimeMultipart multipart) throws MessagingException {
+               if (output.text("text/html", -1) == null) {
+                       return;
+               }
+               MimeBodyPart htmlPart = new MimeBodyPart();
+               htmlPart.setContent(output.text("text/html", -1), "text/html;charset=utf-8");
+               multipart.addBodyPart(htmlPart);
+       }
+
 }