freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/EmailService.java
changeset 233 ecddc9f373bb
child 235 782848b5f533
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/EmailService.java	Thu Mar 04 19:37:58 2010 +0100
     1.3 @@ -0,0 +1,88 @@
     1.4 +/*
     1.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     1.6 + *
     1.7 + * The contents of this file are subject to the terms of either the GNU
     1.8 + * General Public License Version 2 only ("GPL") or the Common
     1.9 + * Development and Distribution License("CDDL") (collectively, the
    1.10 + * "License"). You may not use this file except in compliance with the
    1.11 + * License. You can obtain a copy of the License at
    1.12 + * http://www.netbeans.org/cddl-gplv2.html
    1.13 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    1.14 + * specific language governing permissions and limitations under the
    1.15 + * License.  When distributing the software, include this License Header
    1.16 + * Notice in each file and include the License file at
    1.17 + * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
    1.18 + * particular file as subject to the "Classpath" exception as provided
    1.19 + * by Sun in the GPL Version 2 section of the License file that
    1.20 + * accompanied this code. If applicable, add the following below the
    1.21 + * License Header, with the fields enclosed by brackets [] replaced by
    1.22 + * your own identifying information:
    1.23 + * "Portions Copyrighted [year] [name of copyright owner]"
    1.24 + *
    1.25 + * Contributor(s):
    1.26 + *
    1.27 + * Portions Copyrighted 2009 Jaroslav Tulach
    1.28 + */
    1.29 +
    1.30 +package cz.xelfi.quoridor.freemarkerdor;
    1.31 +
    1.32 +import java.io.IOException;
    1.33 +import java.util.Iterator;
    1.34 +import java.util.Properties;
    1.35 +import java.util.ServiceLoader;
    1.36 +import javax.mail.Message;
    1.37 +import javax.mail.MessagingException;
    1.38 +import javax.mail.Session;
    1.39 +import javax.mail.Transport;
    1.40 +import javax.mail.internet.InternetAddress;
    1.41 +import javax.mail.internet.MimeMessage;
    1.42 +import org.openide.util.Exceptions;
    1.43 +
    1.44 +/** Send emails.
    1.45 + *
    1.46 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    1.47 + */
    1.48 +public abstract class EmailService {
    1.49 +    private static EmailService DEFAULT;
    1.50 +    
    1.51 +    public abstract void sendEmail(
    1.52 +        String address,
    1.53 +        String subject,
    1.54 +        String text
    1.55 +    ) throws IOException;
    1.56 +
    1.57 +    public static synchronized EmailService getDefault() {
    1.58 +        if (DEFAULT == null) {
    1.59 +            Iterator<EmailService> it = ServiceLoader.load(EmailService.class).iterator();
    1.60 +            DEFAULT = it.hasNext() ? it.next() : new Impl();
    1.61 +        }
    1.62 +        return DEFAULT;
    1.63 +    }
    1.64 +
    1.65 +    private static class Impl extends EmailService {
    1.66 +        private final Session session;
    1.67 +        public Impl() {
    1.68 +            Properties props = System.getProperties();
    1.69 +            final String host = "mail.smtp.host";
    1.70 +            if (props.get(host) == null) {
    1.71 +                props.put(host,"localhost");
    1.72 +            }
    1.73 +            session = Session.getDefaultInstance(props, null);
    1.74 +        }
    1.75 +
    1.76 +        @Override
    1.77 +        public void sendEmail(String address, String subject, String text) throws IOException {
    1.78 +            try {
    1.79 +                MimeMessage message = new MimeMessage(session);
    1.80 +                message.setFrom(new InternetAddress("quoridor@xelfi.cz"));
    1.81 +                message.addRecipient(Message.RecipientType.TO, new InternetAddress(address));
    1.82 +                message.setSubject(subject);
    1.83 +                message.setText(text);
    1.84 +                Transport.send(message);
    1.85 +            } catch (MessagingException ex) {
    1.86 +                throw new IOException(ex);
    1.87 +            }
    1.88 +
    1.89 +        }
    1.90 +    }
    1.91 +}