Show the latest seven non-new comics below the most recent one.
[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 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.MimeBodyPart;
28 import javax.mail.internet.MimeMessage;
29 import javax.mail.internet.MimeMultipart;
30
31 import net.pterodactylus.rhynodge.Action;
32 import net.pterodactylus.rhynodge.output.Output;
33
34 /**
35  * {@link Action} implementation that sends an email containing the triggering
36  * object to an email address.
37  *
38  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
39  */
40 public class EmailAction implements Action {
41
42         /** The name of the SMTP host. */
43         private final String hostname;
44
45         /** The email address of the sender. */
46         private final String sender;
47
48         /** The email address of the recipient. */
49         private final String recipient;
50
51         /**
52          * Creates a new email action.
53          *
54          * @param hostname
55          *            The hostname of the SMTP server
56          * @param sender
57          *            The email address of the sender
58          * @param recipient
59          *            The email address of the recipient
60          */
61         public EmailAction(String hostname, String sender, String recipient) {
62                 this.hostname = hostname;
63                 this.sender = sender;
64                 this.recipient = recipient;
65         }
66
67         //
68         // ACTION METHODS
69         //
70
71         /**
72          * {@inheritDoc}
73          */
74         @Override
75         public void execute(Output output) {
76                 Properties properties = System.getProperties();
77                 properties.put("mail.smtp.host", hostname);
78                 Session session = Session.getInstance(properties);
79                 MimeMessage message = new MimeMessage(session);
80                 try {
81                         /* create message. */
82                         message.setFrom(new InternetAddress(sender));
83                         message.setRecipient(RecipientType.TO, new InternetAddress(recipient));
84                         message.setSubject(output.summary());
85
86                         /* create text and html parts. */
87                         MimeMultipart multipart = new MimeMultipart();
88                         multipart.setSubType("alternative");
89                         MimeBodyPart textPart = new MimeBodyPart();
90                         textPart.setContent(output.text("text/plain", -1), "text/plain;charset=utf-8");
91                         MimeBodyPart htmlPart = new MimeBodyPart();
92                         htmlPart.setContent(output.text("text/html", -1), "text/html;charset=utf-8");
93                         multipart.addBodyPart(textPart);
94                         multipart.addBodyPart(htmlPart);
95                         message.setContent(multipart);
96
97                         Transport.send(message);
98                 } catch (MessagingException me1) {
99                         /* swallow. */
100                 }
101         }
102
103 }