4dfece091b2eb975803c3ab4cb854c32ddac1ced
[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.google.common.annotations.VisibleForTesting;
39 import com.sun.mail.smtp.SMTPTransport;
40 import org.apache.log4j.Logger;
41
42 /**
43  * {@link Action} implementation that sends an email containing the triggering
44  * object to an email address.
45  *
46  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
47  */
48 public class EmailAction implements Action {
49
50         private static final Logger logger = Logger.getLogger(EmailAction.class);
51
52         /** The email address of the sender. */
53         private final String sender;
54
55         /** The email address of the recipient. */
56         private final String recipient;
57
58         private final Transport transport;
59         private final Session session;
60
61         /**
62          * Creates a new email action.
63          *
64          * @param hostname
65          *            The hostname of the SMTP server
66          * @param sender
67          *            The email address of the sender
68          * @param recipient
69          *            The email address of the recipient
70          */
71         public EmailAction(String hostname, String sender, String recipient) {
72                 this.sender = sender;
73                 this.recipient = recipient;
74                 Properties properties = getProperties();
75                 properties.put("mail.smtp.host", hostname);
76                 session = getInstance(properties);
77                 transport = new SMTPTransport(session, new URLName("smtp", hostname, 25, null, "", ""));
78         }
79
80         @VisibleForTesting
81         EmailAction(Transport transport, String sender, String recipient) {
82                 this.transport = transport;
83                 this.sender = sender;
84                 this.recipient = recipient;
85                 this.session = getInstance(getProperties());
86         }
87
88         //
89         // ACTION METHODS
90         //
91
92         /**
93          * {@inheritDoc}
94          */
95         @Override
96         public void execute(Output output) {
97                 MimeMessage message = new MimeMessage(session);
98                 try {
99                         /* create message. */
100                         message.setFrom(new InternetAddress(sender));
101                         message.setRecipient(RecipientType.TO, new InternetAddress(recipient));
102                         message.setSubject(output.summary(), "UTF-8");
103
104                         /* create text and html parts. */
105                         MimeMultipart multipart = new MimeMultipart();
106                         multipart.setSubType("alternative");
107                         addPlainTextPart(output, multipart);
108                         addHtmlPart(output, multipart);
109                         message.setContent(multipart);
110
111                         if (!transport.isConnected()) {
112                                 transport.connect();
113                         }
114                         transport.sendMessage(message, message.getAllRecipients());
115                 } catch (MessagingException me1) {
116                         logger.error("Could not send email!", me1);
117                 }
118         }
119
120         private void addPlainTextPart(Output output, MimeMultipart multipart) throws MessagingException {
121                 if (output.text("text/plain", -1) == null) {
122                         return;
123                 }
124                 MimeBodyPart textPart = new MimeBodyPart();
125                 textPart.setContent(output.text("text/plain", -1), "text/plain;charset=utf-8");
126                 multipart.addBodyPart(textPart);
127         }
128
129         private void addHtmlPart(Output output, MimeMultipart multipart) throws MessagingException {
130                 if (output.text("text/html", -1) == null) {
131                         return;
132                 }
133                 MimeBodyPart htmlPart = new MimeBodyPart();
134                 htmlPart.setContent(output.text("text/html", -1), "text/html;charset=utf-8");
135                 multipart.addBodyPart(htmlPart);
136         }
137
138 }