2 * Reactor - EmailAction.java - Copyright © 2013 David Roden
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net.pterodactylus.reactor.actions;
20 import java.util.Properties;
22 import javax.mail.Message.RecipientType;
23 import javax.mail.MessagingException;
24 import javax.mail.Session;
25 import javax.mail.Transport;
26 import javax.mail.internet.InternetAddress;
27 import javax.mail.internet.MimeMessage;
29 import net.pterodactylus.reactor.Action;
32 * {@link Action} implementation that sends an email containing the triggering
33 * object to an email address.
35 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
37 public class EmailAction implements Action {
39 /** The name of the SMTP host. */
40 private final String hostname;
42 /** The email address of the sender. */
43 private final String sender;
45 /** The email address of the recipient. */
46 private final String recipient;
49 * Creates a new email action.
52 * The hostname of the SMTP server
54 * The email address of the sender
56 * The email address of the recipient
58 public EmailAction(String hostname, String sender, String recipient) {
59 this.hostname = hostname;
61 this.recipient = recipient;
72 public void execute(Object trigger) {
73 Properties properties = System.getProperties();
74 properties.put("mail.smtp.host", hostname);
75 Session session = Session.getInstance(properties);
76 MimeMessage message = new MimeMessage(session);
78 message.setFrom(new InternetAddress(sender));
79 message.setRecipient(RecipientType.TO, new InternetAddress(recipient));
80 message.setSubject("Reaction Triggered!");
81 message.setText(String.valueOf(trigger));
82 Transport.send(message);
83 } catch (MessagingException me1) {