src/core/net/sf/basedb/util/EmailUtil.java

Code
Comments
Other
Rev Date Author Line
5442 07 Oct 10 nicklas 1 /**
5442 07 Oct 10 nicklas 2   $Id$
5442 07 Oct 10 nicklas 3
5442 07 Oct 10 nicklas 4   Copyright (C) 2010 Nicklas Nordborg
5442 07 Oct 10 nicklas 5
5442 07 Oct 10 nicklas 6   This file is part of BASE - BioArray Software Environment.
5442 07 Oct 10 nicklas 7   Available at http://base.thep.lu.se/
5442 07 Oct 10 nicklas 8
5442 07 Oct 10 nicklas 9   BASE is free software; you can redistribute it and/or
5442 07 Oct 10 nicklas 10   modify it under the terms of the GNU General Public License
5442 07 Oct 10 nicklas 11   as published by the Free Software Foundation; either version 3
5442 07 Oct 10 nicklas 12   of the License, or (at your option) any later version.
5442 07 Oct 10 nicklas 13
5442 07 Oct 10 nicklas 14   BASE is distributed in the hope that it will be useful,
5442 07 Oct 10 nicklas 15   but WITHOUT ANY WARRANTY; without even the implied warranty of
5442 07 Oct 10 nicklas 16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
5442 07 Oct 10 nicklas 17   GNU General Public License for more details.
5442 07 Oct 10 nicklas 18
5442 07 Oct 10 nicklas 19   You should have received a copy of the GNU General Public License
5442 07 Oct 10 nicklas 20   along with BASE. If not, see <http://www.gnu.org/licenses/>.
5442 07 Oct 10 nicklas 21 */
5442 07 Oct 10 nicklas 22 package net.sf.basedb.util;
5442 07 Oct 10 nicklas 23
5442 07 Oct 10 nicklas 24 import java.util.regex.Matcher;
5442 07 Oct 10 nicklas 25 import java.util.regex.Pattern;
5442 07 Oct 10 nicklas 26
5442 07 Oct 10 nicklas 27 import org.apache.commons.mail.Email;
5442 07 Oct 10 nicklas 28 import org.apache.commons.mail.EmailException;
5442 07 Oct 10 nicklas 29 import org.apache.commons.mail.SimpleEmail;
5442 07 Oct 10 nicklas 30
5442 07 Oct 10 nicklas 31 import net.sf.basedb.core.Application;
5442 07 Oct 10 nicklas 32 import net.sf.basedb.core.Config;
5442 07 Oct 10 nicklas 33 import net.sf.basedb.core.User;
5442 07 Oct 10 nicklas 34
5442 07 Oct 10 nicklas 35 /**
5442 07 Oct 10 nicklas 36   Utility class for email sending. Before use it must be initialized 
5442 07 Oct 10 nicklas 37   (this is done automatically when starting BASE). Configuration options
5442 07 Oct 10 nicklas 38   in the base.config file are used.
5442 07 Oct 10 nicklas 39   
5442 07 Oct 10 nicklas 40   <ul>
5442 07 Oct 10 nicklas 41   <li>mail.server.host: Host name of the outgoing smtp server. This
5442 07 Oct 10 nicklas 42     must be set or email sending will be disabled.
5442 07 Oct 10 nicklas 43   <li>mail.server.port: Port the outgoing smtp server is listening to.
5442 07 Oct 10 nicklas 44     If not set the default port is used (25 for regular mail, 465 for
5442 07 Oct 10 nicklas 45     SSL).
5442 07 Oct 10 nicklas 46   <li>mail.server.ssl: If SSL is enabled or not.
5442 07 Oct 10 nicklas 47   <li>mail.server.tsl: If TLS is enabled or not.
5442 07 Oct 10 nicklas 48   <li>mail.from.email: Email address to use as the sender of outgoing emails.
5442 07 Oct 10 nicklas 49     This must be set or email seding will be disabled.
5442 07 Oct 10 nicklas 50   <li>mail.from.name: Name to use as the sender of outgoing emails. If not
5442 07 Oct 10 nicklas 51     set a default name is created based on the host name of the BASE server.
5442 07 Oct 10 nicklas 52   </ul>
5442 07 Oct 10 nicklas 53   
5442 07 Oct 10 nicklas 54   @author Nicklas
5442 07 Oct 10 nicklas 55   @since 2.16
5442 07 Oct 10 nicklas 56   @base.modified $Date$
5442 07 Oct 10 nicklas 57 */
5442 07 Oct 10 nicklas 58 public class EmailUtil
5442 07 Oct 10 nicklas 59 {
5442 07 Oct 10 nicklas 60
5442 07 Oct 10 nicklas 61   /**
5442 07 Oct 10 nicklas 62     This pattern can be used to find email addresses. The pattern will check that
5442 07 Oct 10 nicklas 63     an @ symbol is present and that it is preceded with at least one character and
5442 07 Oct 10 nicklas 64     followed by at least one subdomain and one topdomain. The pattern will allow
5442 07 Oct 10 nicklas 65     any unicode letters, digits, underscore and hyphen in the address. It will
5442 07 Oct 10 nicklas 66     not check that the domain or email address actually exists.
5442 07 Oct 10 nicklas 67   */
5442 07 Oct 10 nicklas 68   public static final Pattern EMAIL_REGEXP = Pattern.compile("[\\p{L}\\d_-]+(\\.[\\p{L}\\d_-]+)*@[\\p{L}\\d_-]+(\\.[\\p{L}\\d_-]+)+");
5442 07 Oct 10 nicklas 69
5442 07 Oct 10 nicklas 70   private static boolean initialized;
5442 07 Oct 10 nicklas 71   private static boolean enabled;
5442 07 Oct 10 nicklas 72   
5442 07 Oct 10 nicklas 73   private static String hostName;
5442 07 Oct 10 nicklas 74   private static int hostPort;
5442 07 Oct 10 nicklas 75   private static boolean ssl;
5442 07 Oct 10 nicklas 76   private static boolean tls;
5442 07 Oct 10 nicklas 77   private static String senderEmail;
5442 07 Oct 10 nicklas 78   private static String senderName;
5442 07 Oct 10 nicklas 79   
5442 07 Oct 10 nicklas 80   public static synchronized void init()
5442 07 Oct 10 nicklas 81   {
5442 07 Oct 10 nicklas 82     if (initialized) return;
5442 07 Oct 10 nicklas 83     
5442 07 Oct 10 nicklas 84     hostName = Config.getString("mail.server.host", null);
5442 07 Oct 10 nicklas 85     ssl = Config.getBoolean("mail.server.ssl");
5442 07 Oct 10 nicklas 86     tls = Config.getBoolean("mail.server.tls");
5442 07 Oct 10 nicklas 87     hostPort = Config.getInt("mail.server.port", ssl ? 465 : 25);
5442 07 Oct 10 nicklas 88     senderEmail = Config.getString("mail.from.email", null);
5442 07 Oct 10 nicklas 89     senderName = Config.getString("mail.from.name", "BASE " + Application.getMajorVersion() + "." + 
5442 07 Oct 10 nicklas 90       Application.getMinorVersion() + "." + Application.getMaintenanceVersion() + "@" + Application.getHostName());
5442 07 Oct 10 nicklas 91     enabled = hostName != null && senderEmail != null;
5442 07 Oct 10 nicklas 92     initialized = true;
5442 07 Oct 10 nicklas 93   }
5442 07 Oct 10 nicklas 94   
5442 07 Oct 10 nicklas 95   /**
5442 07 Oct 10 nicklas 96     Unload all settings.
5442 07 Oct 10 nicklas 97   */
5442 07 Oct 10 nicklas 98   public static synchronized void unload()
5442 07 Oct 10 nicklas 99   {
5442 07 Oct 10 nicklas 100     initialized = false;
5442 07 Oct 10 nicklas 101     enabled = false;
5442 07 Oct 10 nicklas 102     hostName = null;
5442 07 Oct 10 nicklas 103     ssl = false;
5442 07 Oct 10 nicklas 104     tls = false;
5442 07 Oct 10 nicklas 105     hostPort = 0;
5442 07 Oct 10 nicklas 106     senderEmail = null;
5442 07 Oct 10 nicklas 107     senderName = null;
5442 07 Oct 10 nicklas 108   }
5442 07 Oct 10 nicklas 109
5442 07 Oct 10 nicklas 110   /**
5442 07 Oct 10 nicklas 111     Check if email sending has been enabled for the server or not. Sending is disabled
5442 07 Oct 10 nicklas 112     if the {@link #init()} method hasn't been called, the server admin hasn't
5442 07 Oct 10 nicklas 113     specified an outgoing smtp server or an outgoing sender email.
5442 07 Oct 10 nicklas 114     @return TRUE if mails are enabled, FALSE otherwise
5442 07 Oct 10 nicklas 115   */
5442 07 Oct 10 nicklas 116   public static boolean isEnabled()
5442 07 Oct 10 nicklas 117   {
5442 07 Oct 10 nicklas 118     return initialized && enabled;
5442 07 Oct 10 nicklas 119   }
5442 07 Oct 10 nicklas 120   
5442 07 Oct 10 nicklas 121   /**
5442 07 Oct 10 nicklas 122     Check if email sending is enabled for the given user. All of the
5442 07 Oct 10 nicklas 123     following conditions must apply:
5442 07 Oct 10 nicklas 124     <ul>
5442 07 Oct 10 nicklas 125     <li>The server must be configured for sending emails (see {@link #isEnabled()}).
5442 07 Oct 10 nicklas 126     <li>The user must have selected to have emails ({@link User#getSendMessagesAsEmail()}).
5442 07 Oct 10 nicklas 127     <li>The user must have a valid email address.
5442 07 Oct 10 nicklas 128     </ul>
5442 07 Oct 10 nicklas 129
5442 07 Oct 10 nicklas 130     @param user The user to check
5442 07 Oct 10 nicklas 131   */
5442 07 Oct 10 nicklas 132   public static boolean isEnabled(User user)
5442 07 Oct 10 nicklas 133   {
5442 07 Oct 10 nicklas 134     return isEnabled() && user.getSendMessagesAsEmail() && isValidEmail(user.getEmail());
5442 07 Oct 10 nicklas 135   }
5442 07 Oct 10 nicklas 136   
5442 07 Oct 10 nicklas 137   /**
5442 07 Oct 10 nicklas 138     Checks if the given string looks like an email address. This is done
5442 07 Oct 10 nicklas 139     by trying to match it against the {@link #EMAIL_REGEXP}
5442 07 Oct 10 nicklas 140     pattern.
5442 07 Oct 10 nicklas 141   
5442 07 Oct 10 nicklas 142     @param email The string to check
5442 07 Oct 10 nicklas 143     @return TRUE or FALSE
5442 07 Oct 10 nicklas 144     @see #EMAIL_REGEXP
5442 07 Oct 10 nicklas 145   */
5442 07 Oct 10 nicklas 146   public static boolean isValidEmail(String email)
5442 07 Oct 10 nicklas 147   {
5442 07 Oct 10 nicklas 148     if (email == null) return false;
5442 07 Oct 10 nicklas 149     Matcher m = EMAIL_REGEXP.matcher(email);
5442 07 Oct 10 nicklas 150     return m.matches();
5442 07 Oct 10 nicklas 151   }
5442 07 Oct 10 nicklas 152   
5442 07 Oct 10 nicklas 153   /**
5442 07 Oct 10 nicklas 154     Create a simple text email. Host name, sender, etc. will be set
5442 07 Oct 10 nicklas 155     according to the settigs from base.config. The caller need
5442 07 Oct 10 nicklas 156     to set a 'To' address, 'Subject' and the actual message.
5442 07 Oct 10 nicklas 157     
5442 07 Oct 10 nicklas 158     @return An Email object, or null if mail sending is not enabled
5442 07 Oct 10 nicklas 159     @throws EmailException If there is something wrong with the setup
5442 07 Oct 10 nicklas 160   */
5442 07 Oct 10 nicklas 161   public static Email createSimpleEmail()
5442 07 Oct 10 nicklas 162     throws EmailException
5442 07 Oct 10 nicklas 163   {
5442 07 Oct 10 nicklas 164     if (!isEnabled()) return null;
5442 07 Oct 10 nicklas 165     Email email = new SimpleEmail();
5442 07 Oct 10 nicklas 166     email.setHostName(hostName);
5442 07 Oct 10 nicklas 167     if (ssl)
5442 07 Oct 10 nicklas 168     {
6237 01 Feb 13 nicklas 169       email.setSSLOnConnect(true);
5442 07 Oct 10 nicklas 170       email.setSslSmtpPort(Integer.toString(hostPort));
5442 07 Oct 10 nicklas 171     }
5442 07 Oct 10 nicklas 172     else
5442 07 Oct 10 nicklas 173     {
5442 07 Oct 10 nicklas 174       email.setSmtpPort(hostPort);
5442 07 Oct 10 nicklas 175     }
6237 01 Feb 13 nicklas 176     email.setStartTLSEnabled(tls);
5442 07 Oct 10 nicklas 177     email.setCharset("UTF-8");
5442 07 Oct 10 nicklas 178     email.setFrom(senderEmail, senderName, "UTF-8");
5442 07 Oct 10 nicklas 179     email.addReplyTo(senderEmail, senderName, "UTF-8");
5442 07 Oct 10 nicklas 180     return email;
5442 07 Oct 10 nicklas 181   }
5442 07 Oct 10 nicklas 182   
5442 07 Oct 10 nicklas 183   
5442 07 Oct 10 nicklas 184 }