X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Frhynodge%2Factions%2FEmailAction.java;h=0a0aca7c4fa2e2fda21bfcc5fc8469c1c6bbe57a;hb=5a536ce9ceeac8b431c24b79796835bd9271cfcf;hp=6e866c8942ff7a513e30663e325e38943e24033a;hpb=6f69aff66ba5617d0bb27874014b4274bc551ab8;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 6e866c8..0a0aca7 100644 --- a/src/main/java/net/pterodactylus/rhynodge/actions/EmailAction.java +++ b/src/main/java/net/pterodactylus/rhynodge/actions/EmailAction.java @@ -17,12 +17,16 @@ 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,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; + /** * {@link Action} implementation that sends an email containing the triggering * object to an email address. @@ -39,15 +46,15 @@ import net.pterodactylus.rhynodge.output.Output; */ public class EmailAction implements Action { - /** The name of the SMTP host. */ - private final String hostname; - /** The email address of the sender. */ private final String sender; /** 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 +66,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,15 +91,12 @@ 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(); @@ -94,7 +109,8 @@ public class EmailAction implements Action { multipart.addBodyPart(htmlPart); message.setContent(multipart); - Transport.send(message); + transport.connect(); + transport.sendMessage(message, message.getAllRecipients()); } catch (MessagingException me1) { /* swallow. */ }