freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/EmailService.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 11 Apr 2010 15:39:11 +0200
changeset 236 efbcc47cd834
parent 235 782848b5f533
child 264 d60370059c3c
permissions -rw-r--r--
Adding missing import
     1 /*
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * The contents of this file are subject to the terms of either the GNU
     5  * General Public License Version 2 only ("GPL") or the Common
     6  * Development and Distribution License("CDDL") (collectively, the
     7  * "License"). You may not use this file except in compliance with the
     8  * License. You can obtain a copy of the License at
     9  * http://www.netbeans.org/cddl-gplv2.html
    10  * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    11  * specific language governing permissions and limitations under the
    12  * License.  When distributing the software, include this License Header
    13  * Notice in each file and include the License file at
    14  * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
    15  * particular file as subject to the "Classpath" exception as provided
    16  * by Sun in the GPL Version 2 section of the License file that
    17  * accompanied this code. If applicable, add the following below the
    18  * License Header, with the fields enclosed by brackets [] replaced by
    19  * your own identifying information:
    20  * "Portions Copyrighted [year] [name of copyright owner]"
    21  *
    22  * Contributor(s):
    23  *
    24  * Portions Copyrighted 2009 Jaroslav Tulach
    25  */
    26 
    27 package cz.xelfi.quoridor.freemarkerdor;
    28 
    29 import java.io.IOException;
    30 import java.util.Date;
    31 import java.util.Iterator;
    32 import java.util.Properties;
    33 import java.util.ServiceLoader;
    34 import javax.mail.Message;
    35 import javax.mail.MessagingException;
    36 import javax.mail.Session;
    37 import javax.mail.Transport;
    38 import javax.mail.internet.InternetAddress;
    39 import javax.mail.internet.MimeMessage;
    40 import org.openide.util.Exceptions;
    41 
    42 /** Send emails.
    43  *
    44  * @author Jaroslav Tulach <jtulach@netbeans.org>
    45  */
    46 public abstract class EmailService {
    47     private static EmailService DEFAULT;
    48     
    49     public abstract void sendEmail(
    50         String address,
    51         String subject,
    52         String text
    53     ) throws IOException;
    54 
    55     public static synchronized EmailService getDefault() {
    56         if (DEFAULT == null) {
    57             Iterator<EmailService> it = ServiceLoader.load(EmailService.class).iterator();
    58             DEFAULT = it.hasNext() ? it.next() : new Impl();
    59         }
    60         return DEFAULT;
    61     }
    62 
    63     private static class Impl extends EmailService {
    64         private final Session session;
    65         public Impl() {
    66             Properties props = System.getProperties();
    67             final String host = "mail.smtp.host";
    68             if (props.get(host) == null) {
    69                 props.put(host,"localhost");
    70             }
    71             session = Session.getDefaultInstance(props, null);
    72         }
    73 
    74         @Override
    75         public void sendEmail(String address, String subject, String text) throws IOException {
    76             try {
    77                 MimeMessage message = new MimeMessage(session);
    78                 message.setFrom(new InternetAddress("quoridor@xelfi.cz"));
    79                 message.setSentDate(new Date());
    80                 message.addRecipient(Message.RecipientType.TO, new InternetAddress(address));
    81                 message.setSubject(subject);
    82                 message.setText(text);
    83                 Transport.send(message);
    84             } catch (MessagingException ex) {
    85                 throw new IOException(ex);
    86             }
    87 
    88         }
    89     }
    90 }