Create transport in constructor.
[rhynodge.git] / src / main / java / net / pterodactylus / rhynodge / actions / EmailAction.java
1 /*
2  * Rhynodge - 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.rhynodge.actions;
19
20 import static java.lang.System.getProperties;
21 import static javax.mail.Session.getInstance;
22
23 import java.util.Properties;
24
25 import javax.mail.Message.RecipientType;
26 import javax.mail.MessagingException;
27 import javax.mail.Session;
28 import javax.mail.Transport;
29 import javax.mail.URLName;
30 import javax.mail.internet.InternetAddress;
31 import javax.mail.internet.MimeBodyPart;
32 import javax.mail.internet.MimeMessage;
33 import javax.mail.internet.MimeMultipart;
34
35 import net.pterodactylus.rhynodge.Action;
36 import net.pterodactylus.rhynodge.output.Output;
37
38 import com.sun.mail.smtp.SMTPTransport;
39
40 /**
41  * {@link Action} implementation that sends an email containing the triggering
42  * object to an email address.
43  *
44  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
45  */
46 public class EmailAction implements Action {
47
48         /** The email address of the sender. */
49         private final String sender;
50
51         /** The email address of the recipient. */
52         private final String recipient;
53
54         private final Transport transport;
55         private final Session session;
56
57         /**
58          * Creates a new email action.
59          *
60          * @param hostname
61          *            The hostname of the SMTP server
62          * @param sender
63          *            The email address of the sender
64          * @param recipient
65          *            The email address of the recipient
66          */
67         public EmailAction(String hostname, String sender, String recipient) {
68                 this.sender = sender;
69                 this.recipient = recipient;
70                 Properties properties = getProperties();
71                 properties.put("mail.smtp.host", hostname);
72                 session = getInstance(properties);
73                 transport = new SMTPTransport(session, new URLName("smtp", hostname, 25, null, "", ""));
74         }
75
76         //
77         // ACTION METHODS
78         //
79
80         /**
81          * {@inheritDoc}
82          */
83         @Override
84         public void execute(Output output) {
85                 MimeMessage message = new MimeMessage(session);
86                 try {
87                         /* create message. */
88                         message.setFrom(new InternetAddress(sender));
89                         message.setRecipient(RecipientType.TO, new InternetAddress(recipient));
90                         message.setSubject(output.summary(), "UTF-8");
91
92                         /* create text and html parts. */
93                         MimeMultipart multipart = new MimeMultipart();
94                         multipart.setSubType("alternative");
95                         MimeBodyPart textPart = new MimeBodyPart();
96                         textPart.setContent(output.text("text/plain", -1), "text/plain;charset=utf-8");
97                         MimeBodyPart htmlPart = new MimeBodyPart();
98                         htmlPart.setContent(output.text("text/html", -1), "text/html;charset=utf-8");
99                         multipart.addBodyPart(textPart);
100                         multipart.addBodyPart(htmlPart);
101                         message.setContent(multipart);
102
103                         transport.sendMessage(message, message.getAllRecipients());
104                 } catch (MessagingException me1) {
105                         /* swallow. */
106                 }
107         }
108
109 }