Package net.sf.basedb.util
Class EmailUtil
- java.lang.Object
-
- net.sf.basedb.util.EmailUtil
-
public class EmailUtil extends Object
Utility class for email sending. Before use it must be initialized (this is done automatically when starting BASE). Configuration options in the base.config file are used.- mail.server.host: Host name of the outgoing smtp server. This must be set or email sending will be disabled.
- mail.server.port: Port the outgoing smtp server is listening to. If not set the default port is used (25 for regular mail, 465 for SSL).
- mail.server.ssl: If SSL is enabled or not.
- mail.server.tsl: If TLS is enabled or not.
- mail.from.email: Email address to use as the sender of outgoing emails. This must be set or email seding will be disabled.
- mail.from.name: Name to use as the sender of outgoing emails. If not set a default name is created based on the host name of the BASE server.
- Since:
- 2.16
- Author:
- Nicklas
- Last modified
- $Date: 2013-02-01 10:23:04 +0100 (fr, 01 feb 2013) $
-
-
Field Summary
Fields Modifier and Type Field Description static Pattern
EMAIL_REGEXP
This pattern can be used to find email addresses.private static boolean
enabled
private static String
hostName
private static int
hostPort
private static boolean
initialized
private static String
senderEmail
private static String
senderName
private static boolean
ssl
private static boolean
tls
-
Constructor Summary
Constructors Constructor Description EmailUtil()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static org.apache.commons.mail.Email
createSimpleEmail()
Create a simple text email.static void
init()
static boolean
isEnabled()
Check if email sending has been enabled for the server or not.static boolean
isEnabled(User user)
Check if email sending is enabled for the given user.static boolean
isValidEmail(String email)
Checks if the given string looks like an email address.static void
unload()
Unload all settings.
-
-
-
Field Detail
-
EMAIL_REGEXP
public static final Pattern EMAIL_REGEXP
This pattern can be used to find email addresses. The pattern will check that an @ symbol is present and that it is preceded with at least one character and followed by at least one subdomain and one topdomain. The pattern will allow any unicode letters, digits, underscore and hyphen in the address. It will not check that the domain or email address actually exists.
-
initialized
private static boolean initialized
-
enabled
private static boolean enabled
-
hostName
private static String hostName
-
hostPort
private static int hostPort
-
ssl
private static boolean ssl
-
tls
private static boolean tls
-
senderEmail
private static String senderEmail
-
senderName
private static String senderName
-
-
Method Detail
-
init
public static void init()
-
unload
public static void unload()
Unload all settings.
-
isEnabled
public static boolean isEnabled()
Check if email sending has been enabled for the server or not. Sending is disabled if theinit()
method hasn't been called, the server admin hasn't specified an outgoing smtp server or an outgoing sender email.- Returns:
- TRUE if mails are enabled, FALSE otherwise
-
isEnabled
public static boolean isEnabled(User user)
Check if email sending is enabled for the given user. All of the following conditions must apply:- The server must be configured for sending emails (see
isEnabled()
). - The user must have selected to have emails (
User.getSendMessagesAsEmail()
). - The user must have a valid email address.
- Parameters:
user
- The user to check
- The server must be configured for sending emails (see
-
isValidEmail
public static boolean isValidEmail(String email)
Checks if the given string looks like an email address. This is done by trying to match it against theEMAIL_REGEXP
pattern.- Parameters:
email
- The string to check- Returns:
- TRUE or FALSE
- See Also:
EMAIL_REGEXP
-
createSimpleEmail
public static org.apache.commons.mail.Email createSimpleEmail() throws org.apache.commons.mail.EmailException
Create a simple text email. Host name, sender, etc. will be set according to the settigs from base.config. The caller need to set a 'To' address, 'Subject' and the actual message.- Returns:
- An Email object, or null if mail sending is not enabled
- Throws:
org.apache.commons.mail.EmailException
- If there is something wrong with the setup
-
-