freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/EmailService.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 14 Sep 2010 08:56:13 +0200
changeset 264 d60370059c3c
parent 236 efbcc47cd834
permissions -rw-r--r--
Changing headers to GPLv3
     1 /*
     2  * Quoridor server and related libraries
     3  * Copyright (C) 2009-2010 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     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.
     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. Look for COPYING file in the top folder.
    16  * If not, see http://www.gnu.org/licenses/.
    17  */
    18 
    19 package cz.xelfi.quoridor.freemarkerdor;
    20 
    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;
    33 
    34 /** Send emails.
    35  *
    36  * @author Jaroslav Tulach <jtulach@netbeans.org>
    37  */
    38 public abstract class EmailService {
    39     private static EmailService DEFAULT;
    40     
    41     public abstract void sendEmail(
    42         String address,
    43         String subject,
    44         String text
    45     ) throws IOException;
    46 
    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();
    51         }
    52         return DEFAULT;
    53     }
    54 
    55     private static class Impl extends EmailService {
    56         private final Session session;
    57         public Impl() {
    58             Properties props = System.getProperties();
    59             final String host = "mail.smtp.host";
    60             if (props.get(host) == null) {
    61                 props.put(host,"localhost");
    62             }
    63             session = Session.getDefaultInstance(props, null);
    64         }
    65 
    66         @Override
    67         public void sendEmail(String address, String subject, String text) throws IOException {
    68             try {
    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);
    78             }
    79 
    80         }
    81     }
    82 }