jaroslav@233
|
1 |
/*
|
jaroslav@264
|
2 |
* Quoridor server and related libraries
|
jaroslav@264
|
3 |
* Copyright (C) 2009-2010 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
|
jaroslav@233
|
4 |
*
|
jaroslav@264
|
5 |
* This program is free software: you can redistribute it and/or modify
|
jaroslav@264
|
6 |
* it under the terms of the GNU General Public License as published by
|
jaroslav@264
|
7 |
* the Free Software Foundation, either version 3 of the License.
|
jaroslav@233
|
8 |
*
|
jaroslav@264
|
9 |
* This program is distributed in the hope that it will be useful,
|
jaroslav@264
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
jaroslav@264
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
jaroslav@264
|
12 |
* GNU General Public License for more details.
|
jaroslav@233
|
13 |
*
|
jaroslav@264
|
14 |
* You should have received a copy of the GNU General Public License
|
jaroslav@264
|
15 |
* along with this program. Look for COPYING file in the top folder.
|
jaroslav@264
|
16 |
* If not, see http://www.gnu.org/licenses/.
|
jaroslav@233
|
17 |
*/
|
jaroslav@233
|
18 |
|
jaroslav@233
|
19 |
package cz.xelfi.quoridor.freemarkerdor;
|
jaroslav@233
|
20 |
|
jaroslav@233
|
21 |
import java.io.IOException;
|
jaroslav@236
|
22 |
import java.util.Date;
|
jaroslav@233
|
23 |
import java.util.Iterator;
|
jaroslav@233
|
24 |
import java.util.Properties;
|
jaroslav@233
|
25 |
import java.util.ServiceLoader;
|
jaroslav@233
|
26 |
import javax.mail.Message;
|
jaroslav@233
|
27 |
import javax.mail.MessagingException;
|
jaroslav@233
|
28 |
import javax.mail.Session;
|
jaroslav@233
|
29 |
import javax.mail.Transport;
|
jaroslav@233
|
30 |
import javax.mail.internet.InternetAddress;
|
jaroslav@233
|
31 |
import javax.mail.internet.MimeMessage;
|
jaroslav@233
|
32 |
import org.openide.util.Exceptions;
|
jaroslav@233
|
33 |
|
jaroslav@233
|
34 |
/** Send emails.
|
jaroslav@233
|
35 |
*
|
jaroslav@233
|
36 |
* @author Jaroslav Tulach <jtulach@netbeans.org>
|
jaroslav@233
|
37 |
*/
|
jaroslav@233
|
38 |
public abstract class EmailService {
|
jaroslav@233
|
39 |
private static EmailService DEFAULT;
|
jaroslav@233
|
40 |
|
jaroslav@233
|
41 |
public abstract void sendEmail(
|
jaroslav@233
|
42 |
String address,
|
jaroslav@233
|
43 |
String subject,
|
jaroslav@233
|
44 |
String text
|
jaroslav@233
|
45 |
) throws IOException;
|
jaroslav@233
|
46 |
|
jaroslav@233
|
47 |
public static synchronized EmailService getDefault() {
|
jaroslav@233
|
48 |
if (DEFAULT == null) {
|
jaroslav@233
|
49 |
Iterator<EmailService> it = ServiceLoader.load(EmailService.class).iterator();
|
jaroslav@233
|
50 |
DEFAULT = it.hasNext() ? it.next() : new Impl();
|
jaroslav@233
|
51 |
}
|
jaroslav@233
|
52 |
return DEFAULT;
|
jaroslav@233
|
53 |
}
|
jaroslav@233
|
54 |
|
jaroslav@233
|
55 |
private static class Impl extends EmailService {
|
jaroslav@233
|
56 |
private final Session session;
|
jaroslav@233
|
57 |
public Impl() {
|
jaroslav@233
|
58 |
Properties props = System.getProperties();
|
jaroslav@233
|
59 |
final String host = "mail.smtp.host";
|
jaroslav@233
|
60 |
if (props.get(host) == null) {
|
jaroslav@233
|
61 |
props.put(host,"localhost");
|
jaroslav@233
|
62 |
}
|
jaroslav@233
|
63 |
session = Session.getDefaultInstance(props, null);
|
jaroslav@233
|
64 |
}
|
jaroslav@233
|
65 |
|
jaroslav@233
|
66 |
@Override
|
jaroslav@233
|
67 |
public void sendEmail(String address, String subject, String text) throws IOException {
|
jaroslav@233
|
68 |
try {
|
jaroslav@233
|
69 |
MimeMessage message = new MimeMessage(session);
|
jaroslav@233
|
70 |
message.setFrom(new InternetAddress("quoridor@xelfi.cz"));
|
jaroslav@235
|
71 |
message.setSentDate(new Date());
|
jaroslav@233
|
72 |
message.addRecipient(Message.RecipientType.TO, new InternetAddress(address));
|
jaroslav@233
|
73 |
message.setSubject(subject);
|
jaroslav@233
|
74 |
message.setText(text);
|
jaroslav@233
|
75 |
Transport.send(message);
|
jaroslav@233
|
76 |
} catch (MessagingException ex) {
|
jaroslav@233
|
77 |
throw new IOException(ex);
|
jaroslav@233
|
78 |
}
|
jaroslav@233
|
79 |
|
jaroslav@233
|
80 |
}
|
jaroslav@233
|
81 |
}
|
jaroslav@233
|
82 |
}
|