8f325aa95ef86760c2e39cf0da494d4a37499c59
[rhynodge.git] / src / main / java / net / pterodactylus / reactor / actions / EmailAction.java
1 /*
2  * Reactor - EmailAction.java - Copyright © 2013 David Roden
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.pterodactylus.reactor.actions;
19
20 import java.util.Properties;
21
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;
28
29 import net.pterodactylus.reactor.Action;
30
31 /**
32  * {@link Action} implementation that sends an email containing the triggering
33  * object to an email address.
34  *
35  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
36  */
37 public class EmailAction implements Action {
38
39         /** The name of the SMTP host. */
40         private final String hostname;
41
42         /** The email address of the sender. */
43         private final String sender;
44
45         /** The email address of the recipient. */
46         private final String recipient;
47
48         /**
49          * Creates a new email action.
50          *
51          * @param hostname
52          *            The hostname of the SMTP server
53          * @param sender
54          *            The email address of the sender
55          * @param recipient
56          *            The email address of the recipient
57          */
58         public EmailAction(String hostname, String sender, String recipient) {
59                 this.hostname = hostname;
60                 this.sender = sender;
61                 this.recipient = recipient;
62         }
63
64         //
65         // ACTION METHODS
66         //
67
68         /**
69          * {@inheritDoc}
70          */
71         @Override
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);
77                 try {
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) {
84                         /* swallow. */
85                 }
86         }
87
88 }