1 /**
2 * Copyright (C) 2007 Joern Krueger surething@users.sourceforge.net
3 *
4 * This program is free software; you can redistribute
5 * it and/or modify it under the terms of the GNU General
6 * Public License version 2 as published by the Free Software
7 * Foundation.
8 *
9 * This program is distributed in the hope that it will be
10 * useful, but WITHOUT ANY WARRANTY; without even the implied
11 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12 * PURPOSE. See the GNU General Public License for more
13 * details.
14 *
15 * You should have received a copy of the GNU General Public
16 * License along with this program; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place,
18 * Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21 package de.surething.lda.actions;
22
23 import java.awt.Frame;
24 import java.io.IOException;
25 import java.util.Properties;
26
27 import javax.activation.DataHandler;
28 import javax.mail.Message;
29 import javax.mail.MessagingException;
30 import javax.mail.Session;
31 import javax.mail.Transport;
32 import javax.mail.internet.AddressException;
33 import javax.mail.internet.InternetAddress;
34 import javax.mail.internet.MimeMessage;
35 import javax.mail.util.ByteArrayDataSource;
36
37 import de.surething.lda.locations.Location;
38 import de.surething.lda.resources.Messages;
39 import de.surething.lda.ui.PasswordDialog;
40 import de.surethingies.properties.ParameterFactory;
41 import de.surethingies.ui.ExceptionDialog;
42
43 public class MailAction implements ActionTask {
44
45 private String identifier;
46
47 /**
48 * Init Key / Path Mapping And Default Values
49 */
50 public MailAction(String identifier) {
51 this.identifier = identifier;
52
53 if (ParameterFactory.instance().get(MailActionSettings.GROUP, identifier).size() == 0) {
54 ParameterFactory.instance().addParameters(MailActionSettings.getDefaultParameters(identifier));
55 }
56 }
57
58 public String getIdentifier() {
59 return identifier;
60 }
61
62 public void execute(Frame owner, Location location) {
63 PasswordDialog passwordDialog = new PasswordDialog(owner, Messages.getString("MailAction.PasswordDialog"));
64 passwordDialog.showDialog();
65
66 char[] pwd = passwordDialog.getPassword();
67 try {
68 if (pwd == null || pwd.length <= 0) {
69 throw new IllegalArgumentException(Messages.getString("MailAction.NoPassword"));
70 }
71
72 sendMail(
73 ParameterFactory.instance().get(MailActionSettings.SERVER.param(), identifier).getValue(),
74 ParameterFactory.instance().get(MailActionSettings.USERNAME.param(), identifier).getValue(),
75 pwd,
76 ParameterFactory.instance().get(MailActionSettings.SENDER_ADDRESS.param(), identifier).getValue(),
77 ParameterFactory.instance().get(MailActionSettings.RECIPIENT.param(), identifier).getValue(),
78 ParameterFactory.instance().get(MailActionSettings.CC_ADDRESSES.param(), identifier).getValue(),
79 ParameterFactory.instance().get(MailActionSettings.BCC_ADDRESSES.param(), identifier).getValue(),
80 false,
81 (String) ParameterFactory.instance().get(MailActionSettings.SUBJECT.param(), identifier).getValue(),
82 replacePlaceholder (
83 (String) ParameterFactory.instance().get(MailActionSettings.BODY.param(), identifier).getValue()));
84
85 } catch (Exception e) {
86 for (int i = 0; i < pwd.length; i++) {
87 pwd[i] = 'x';
88 }
89 new ExceptionDialog(owner, e);
90
91 } finally {
92 for (int i = 0; i < pwd.length; i++) {
93 pwd[i] = 'x';
94 }
95 }
96
97 }
98
99
100 private String replacePlaceholder(String text) {
101 return text;
102 }
103
104
105 private void sendMail(String server, String userName, char[] password, String fromAddress, String toAddress, String cCs,
106 String bCCs, boolean isHtmlFormat, String subject, String body) throws AddressException, MessagingException, IOException {
107
108 Properties properties = new Properties();
109 properties.put("mail.smtps.host", "localhost");
110 properties.put("mail.smtps.auth", "true");
111
112 Session ses = Session.getInstance(properties);
113 ses.setDebug(true);
114
115 MimeMessage msg = new MimeMessage(ses);
116 msg.setFrom(new InternetAddress(fromAddress));
117
118 if (toAddress != null) {
119 msg.addRecipients(Message.RecipientType.TO, toAddress);
120 }
121
122 if (cCs != null && cCs.length() > 0) {
123 for (String cc : cCs.split(";")) {
124 msg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(cc, false));
125 }
126 }
127
128 if (bCCs != null && bCCs.length() > 0) {
129 for (String bcc : bCCs.split(";")) {
130 msg.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(bcc, false));
131 }
132 }
133
134 if (isHtmlFormat) {
135 msg.setDataHandler(new DataHandler(new ByteArrayDataSource(body, "text/html")));
136 } else {
137 msg.setDataHandler(new DataHandler(new ByteArrayDataSource(body, "text/plain")));
138 }
139
140 msg.setSubject(subject);
141 msg.saveChanges();
142
143
144
145 Transport tr = ses.getTransport("smtps");
146 tr.connect(server, userName, String.valueOf(password));
147 tr.sendMessage(msg, msg.getAllRecipients());
148 tr.close();
149 }
150
151
152
153 public String getDescription() {
154 return Messages.getString("MailAction.Description");
155 }
156
157
158
159 @Override
160 public String toString() {
161 return getDescription();
162 }
163 }