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