2 * Quoridor server and related libraries
3 * Copyright (C) 2009-2010 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License.
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.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. Look for COPYING file in the top folder.
16 * If not, see http://www.gnu.org/licenses/.
19 package cz.xelfi.quoridor.freemarkerdor;
21 import java.io.IOException;
22 import java.util.Date;
23 import java.util.Iterator;
24 import java.util.Properties;
25 import java.util.ServiceLoader;
26 import javax.mail.Message;
27 import javax.mail.MessagingException;
28 import javax.mail.Session;
29 import javax.mail.Transport;
30 import javax.mail.internet.InternetAddress;
31 import javax.mail.internet.MimeMessage;
32 import org.openide.util.Exceptions;
36 * @author Jaroslav Tulach <jtulach@netbeans.org>
38 public abstract class EmailService {
39 private static EmailService DEFAULT;
41 public abstract void sendEmail(
47 public static synchronized EmailService getDefault() {
48 if (DEFAULT == null) {
49 Iterator<EmailService> it = ServiceLoader.load(EmailService.class).iterator();
50 DEFAULT = it.hasNext() ? it.next() : new Impl();
55 private static class Impl extends EmailService {
56 private final Session session;
58 Properties props = System.getProperties();
59 final String host = "mail.smtp.host";
60 if (props.get(host) == null) {
61 props.put(host,"localhost");
63 session = Session.getDefaultInstance(props, null);
67 public void sendEmail(String address, String subject, String text) throws IOException {
69 MimeMessage message = new MimeMessage(session);
70 message.setFrom(new InternetAddress("quoridor@xelfi.cz"));
71 message.setSentDate(new Date());
72 message.addRecipient(Message.RecipientType.TO, new InternetAddress(address));
73 message.setSubject(subject);
74 message.setText(text);
75 Transport.send(message);
76 } catch (MessagingException ex) {
77 throw new IOException(ex);