X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Frhynodge%2Factions%2FEmailAction.java;h=4dfece091b2eb975803c3ab4cb854c32ddac1ced;hb=cf2f4441705f9a5941022fb0837d08c95a1d0633;hp=5c57822775a9a15c0c6e75de6660a471423872d6;hpb=4fea375ff20fbbbaaad6feba574d4b6c6380ed93;p=rhynodge.git diff --git a/src/main/java/net/pterodactylus/rhynodge/actions/EmailAction.java b/src/main/java/net/pterodactylus/rhynodge/actions/EmailAction.java index 5c57822..4dfece0 100644 --- a/src/main/java/net/pterodactylus/rhynodge/actions/EmailAction.java +++ b/src/main/java/net/pterodactylus/rhynodge/actions/EmailAction.java @@ -35,7 +35,9 @@ 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 @@ -45,6 +47,8 @@ import com.sun.mail.smtp.SMTPTransport; */ public class EmailAction implements Action { + private static final Logger logger = Logger.getLogger(EmailAction.class); + /** The email address of the sender. */ private final String sender; @@ -73,6 +77,14 @@ public class EmailAction implements Action { 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()); + } + // // ACTION METHODS // @@ -92,18 +104,35 @@ 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); + 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", -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); } }