From d64c23f2e6708beeaa00c7b16b8b1b4bc64e1b53 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Thu, 3 Jan 2013 00:36:34 +0100 Subject: [PATCH] Add email action. --- pom.xml | 5 ++ .../pterodactylus/reactor/actions/EmailAction.java | 88 ++++++++++++++++++++++ 2 files changed, 93 insertions(+) create mode 100644 src/main/java/net/pterodactylus/reactor/actions/EmailAction.java diff --git a/pom.xml b/pom.xml index d16a214..c68a158 100644 --- a/pom.xml +++ b/pom.xml @@ -40,5 +40,10 @@ jsoup 1.7.1 + + javax.mail + mail + 1.4.6-rc1 + diff --git a/src/main/java/net/pterodactylus/reactor/actions/EmailAction.java b/src/main/java/net/pterodactylus/reactor/actions/EmailAction.java new file mode 100644 index 0000000..8f325aa --- /dev/null +++ b/src/main/java/net/pterodactylus/reactor/actions/EmailAction.java @@ -0,0 +1,88 @@ +/* + * Reactor - EmailAction.java - Copyright © 2013 David Roden + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.pterodactylus.reactor.actions; + +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.pterodactylus.reactor.Action; + +/** + * {@link Action} implementation that sends an email containing the triggering + * object to an email address. + * + * @author David ‘Bombe’ Roden + */ +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; + + /** + * Creates a new email action. + * + * @param hostname + * The hostname of the SMTP server + * @param sender + * The email address of the sender + * @param recipient + * The email address of the recipient + */ + public EmailAction(String hostname, String sender, String recipient) { + this.hostname = hostname; + this.sender = sender; + this.recipient = recipient; + } + + // + // ACTION METHODS + // + + /** + * {@inheritDoc} + */ + @Override + public void execute(Object trigger) { + Properties properties = System.getProperties(); + properties.put("mail.smtp.host", hostname); + Session session = Session.getInstance(properties); + MimeMessage message = new MimeMessage(session); + try { + message.setFrom(new InternetAddress(sender)); + message.setRecipient(RecipientType.TO, new InternetAddress(recipient)); + message.setSubject("Reaction Triggered!"); + message.setText(String.valueOf(trigger)); + Transport.send(message); + } catch (MessagingException me1) { + /* swallow. */ + } + } + +} -- 2.7.4