Index: lams_admin/conf/language/lams/ApplicationResources_en_AU.properties =================================================================== diff -u -r2f8150728cb7f9aeba189e77970be3b129284372 -r3b1c6836dcfb5f9b80c9284d1815139d1fc08b30 --- lams_admin/conf/language/lams/ApplicationResources_en_AU.properties (.../ApplicationResources_en_AU.properties) (revision 2f8150728cb7f9aeba189e77970be3b129284372) +++ lams_admin/conf/language/lams/ApplicationResources_en_AU.properties (.../ApplicationResources_en_AU.properties) (revision 3b1c6836dcfb5f9b80c9284d1815139d1fc08b30) @@ -371,6 +371,8 @@ label.tool.version =Tool Version label.database.version =Database Version admin.user.authentication.method =Authentication method +config.smtp.user =SMTP User +config.smtp.password =SMTP Password #======= End labels: Exported 364 labels for en AU ===== Index: lams_central/src/java/org/lamsfoundation/lams/util/Emailer.java =================================================================== diff -u -r58e82ec92a3551c959c79c9930cfe4445b5e01f2 -r3b1c6836dcfb5f9b80c9284d1815139d1fc08b30 --- lams_central/src/java/org/lamsfoundation/lams/util/Emailer.java (.../Emailer.java) (revision 58e82ec92a3551c959c79c9930cfe4445b5e01f2) +++ lams_central/src/java/org/lamsfoundation/lams/util/Emailer.java (.../Emailer.java) (revision 3b1c6836dcfb5f9b80c9284d1815139d1fc08b30) @@ -1,67 +1,93 @@ package org.lamsfoundation.lams.util; -import java.io.IOException; -import java.io.InputStream; import java.util.Properties; -import org.lamsfoundation.lams.integration.service.IntegrationService; -import org.lamsfoundation.lams.util.Configuration; - +import javax.mail.Authenticator; import javax.mail.MessagingException; +import javax.mail.PasswordAuthentication; import javax.mail.Session; import javax.mail.Transport; import javax.mail.Message.RecipientType; import javax.mail.internet.AddressException; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; + import org.apache.log4j.Logger; /** - * A class that handles emails, used for forgot password task + * A class that handles emails + * * @author lfoxton */ public class Emailer { - private static Logger log = Logger.getLogger(Emailer.class); - - public Emailer() {} - + public Emailer() { + } - /** - * Sends an email sourced from admin - * @param subject the subject of the email - * @param to the email of the recipient - * @param body the body of the email - */ - public static void sendFromSupportEmail(String subject, String to, String body) - throws AddressException, MessagingException - { - String supportEmail = Configuration.get("LamsSupportEmail"); - String smtpServer = Configuration.get("SMTPServer"); - Properties properties = new Properties(); - properties.put("mail.smtp.host", smtpServer); - send(subject, to, supportEmail, body, properties); + /** + * Sends an email sourced from support email + * + * @param subject + * the subject of the email + * @param to + * the email of the recipient + * @param body + * the body of the email + */ + public static void sendFromSupportEmail(String subject, String to, String body) throws AddressException, + MessagingException { + String supportEmail = Configuration.get(ConfigurationKeys.LAMS_ADMIN_EMAIL); + String smtpServer = Configuration.get(ConfigurationKeys.SMTP_SERVER); + Properties properties = new Properties(); + properties.put("mail.smtp.host", smtpServer); + send(subject, to, supportEmail, body, properties); + } + + /** + * Creates a mail session with authentication if it is required, ie if it + * has been set up with SMTP authentication in the config page + * + * @param properties + * @return + */ + public static Session getMailSession(Properties properties) { + Session session; + String smtpAuthUser = Configuration.get(ConfigurationKeys.SMTP_AUTH_USER); + String smtpAuthPass = Configuration.get(ConfigurationKeys.SMTP_AUTH_PASSWORD); + if (smtpAuthUser != null && !smtpAuthUser.trim().equals("")) { + + properties.setProperty("mail.smtp.submitter", smtpAuthUser); + properties.setProperty("mail.smtp.auth", "true"); + + SMTPAuthenticator auth = new SMTPAuthenticator(smtpAuthUser, smtpAuthPass); + session = Session.getInstance(properties, auth); + } else { + session = Session.getInstance(properties); } - - - /** - * Send email to recipients - * @param subject the subject of the email - * @param to the email of the recipient - * @param from the email to source the email from - * @param body the body of the email - */ - public static void send(String subject, String to, String from, String body, Properties mailServerConfig) - throws AddressException, MessagingException - { - Session session = Session.getInstance(mailServerConfig); - MimeMessage message = new MimeMessage(session); - message.setFrom(new InternetAddress(from)); - message.addRecipient(RecipientType.TO, new InternetAddress(to)); - message.setSubject(subject); - message.setText(body); - Transport.send(message); - } - - + return session; + } + + /** + * Send email to recipients + * + * @param subject + * the subject of the email + * @param to + * the email of the recipient + * @param from + * the email to source the email from + * @param body + * the body of the email + */ + public static void send(String subject, String to, String from, String body, Properties mailServerConfig) + throws AddressException, MessagingException { + Session session = getMailSession(mailServerConfig); + MimeMessage message = new MimeMessage(session); + message.setFrom(new InternetAddress(from)); + message.addRecipient(RecipientType.TO, new InternetAddress(to)); + message.setSubject(subject); + message.setText(body); + Transport.send(message); + } + } Index: lams_central/src/java/org/lamsfoundation/lams/util/SMTPAuthenticator.java =================================================================== diff -u --- lams_central/src/java/org/lamsfoundation/lams/util/SMTPAuthenticator.java (revision 0) +++ lams_central/src/java/org/lamsfoundation/lams/util/SMTPAuthenticator.java (revision 3b1c6836dcfb5f9b80c9284d1815139d1fc08b30) @@ -0,0 +1,38 @@ +/**************************************************************** + * Copyright (C) 2008 LAMS Foundation (http://lamsfoundation.org) + * ============================================================= + * License Information: http://lamsfoundation.org/licensing/lams/2.0/ + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2.0 + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 * USA + * + * http://www.gnu.org/licenses/gpl.txt + * **************************************************************** + */ + +/* $Id$ */ +package org.lamsfoundation.lams.util; + +import javax.mail.PasswordAuthentication; + +public class SMTPAuthenticator extends javax.mail.Authenticator { + private PasswordAuthentication authentication; + + public SMTPAuthenticator(String username, String password) { + authentication = new PasswordAuthentication(username, password); + } + + protected PasswordAuthentication getPasswordAuthentication() { + return authentication; + } +} \ No newline at end of file Index: lams_common/src/java/org/lamsfoundation/lams/events/DeliveryMethodMail.java =================================================================== diff -u -r6e4ed3724bd76354c2ee43c88979385c4a162a0e -r3b1c6836dcfb5f9b80c9284d1815139d1fc08b30 --- lams_common/src/java/org/lamsfoundation/lams/events/DeliveryMethodMail.java (.../DeliveryMethodMail.java) (revision 6e4ed3724bd76354c2ee43c88979385c4a162a0e) +++ lams_common/src/java/org/lamsfoundation/lams/events/DeliveryMethodMail.java (.../DeliveryMethodMail.java) (revision 3b1c6836dcfb5f9b80c9284d1815139d1fc08b30) @@ -1,102 +1,96 @@ package org.lamsfoundation.lams.events; import java.security.InvalidParameterException; -import java.util.Properties; import javax.mail.MessagingException; -import javax.mail.Session; -import javax.mail.Transport; -import javax.mail.Message.RecipientType; import javax.mail.internet.AddressException; -import javax.mail.internet.InternetAddress; -import javax.mail.internet.MimeMessage; import org.apache.commons.lang.StringUtils; import org.lamsfoundation.lams.usermanagement.User; import org.lamsfoundation.lams.util.Configuration; +import org.lamsfoundation.lams.util.Emailer; /** * Allows sending mail from the configured mail server. + * * @author Marcin Cieslak - * + * */ public class DeliveryMethodMail extends AbstractDeliveryMethod { - private static DeliveryMethodMail instance; + private static DeliveryMethodMail instance; - protected DeliveryMethodMail() { - super((short) 1, "MAIL", "Messages will be send by Mail"); - } + protected DeliveryMethodMail() { + super((short) 1, "MAIL", "Messages will be send by Mail"); + } - /** - * {@inheritDoc} - */ - @Override - public String send(Long userId, String subject, String message) throws InvalidParameterException { - if (userId == null) { - return "User ID should not be null."; - } - try { - User user = (User) EventNotificationService.getInstance().getUserManagementService().findById(User.class, - userId.intValue()); - if (user == null) { - return "User with the provided ID was not found."; - } - String email = user.getEmail(); - if (StringUtils.isBlank(email)) { - return "User's e-mail address is blank."; - } - sendFromSupportEmail(subject, email, message); - return null; - } - catch (Exception e) { - return e.getMessage(); - } + /** + * {@inheritDoc} + */ + @Override + public String send(Long userId, String subject, String message) throws InvalidParameterException { + if (userId == null) { + return "User ID should not be null."; } + try { + User user = (User) EventNotificationService.getInstance().getUserManagementService().findById(User.class, + userId.intValue()); + if (user == null) { + return "User with the provided ID was not found."; + } + String email = user.getEmail(); + if (StringUtils.isBlank(email)) { + return "User's e-mail address is blank."; + } + sendFromSupportEmail(subject, email, message); + return null; + } catch (Exception e) { + return e.getMessage(); + } + } - public static DeliveryMethodMail getInstance() { - if (DeliveryMethodMail.instance == null) { - DeliveryMethodMail.instance = new DeliveryMethodMail(); - } - return DeliveryMethodMail.instance; + public static DeliveryMethodMail getInstance() { + if (DeliveryMethodMail.instance == null) { + DeliveryMethodMail.instance = new DeliveryMethodMail(); } + return DeliveryMethodMail.instance; + } - /** - * Sends an email sourced from admin. Copied from Emailer class. - * @param subject subject of the message - * @param to address to send - * @param body text of the message - * @throws AddressException if address was incorrect - * @throws MessagingException if the operation failed - */ - public void sendFromSupportEmail(String subject, String to, String body) throws AddressException, MessagingException { - String supportEmail = Configuration.get("LamsSupportEmail"); - String smtpServer = Configuration.get("SMTPServer"); - Properties properties = new Properties(); - properties.put("mail.smtp.host", smtpServer); - Session session = Session.getInstance(properties); - MimeMessage message = new MimeMessage(session); - if (!StringUtils.isBlank(supportEmail)) { - message.setFrom(new InternetAddress(supportEmail)); - } - message.addRecipient(RecipientType.TO, new InternetAddress(to)); + /** + * Sends an email sourced from admin. Copied from Emailer class. + * + * @param subject + * subject of the message + * @param to + * address to send + * @param body + * text of the message + * @throws AddressException + * if address was incorrect + * @throws MessagingException + * if the operation failed + */ + public void sendFromSupportEmail(String subject, String to, String body) throws AddressException, + MessagingException { + Emailer.sendFromSupportEmail(subject, to, body); + } - message.setSubject(subject == null ? "" : subject); - message.setText(body == null ? "" : body); - Transport.send(message); + /** + * Sends an email to the address provided by the admin. + * + * @param subject + * subject of the message + * @param body + * text of the message + * @throws AddressException + * if address was incorrect + * @throws MessagingException + * if the operation failed + */ + void notifyAdmin(String subject, String body) throws AddressException, MessagingException { + String adminEmail = Configuration.get("LamsSupportEmail"); + if (!StringUtils.isEmpty(adminEmail)) { + sendFromSupportEmail(subject, adminEmail, body); } - - /** - * Sends an email to the address provided by the admin. - * @param subject subject of the message - * @param body text of the message - * @throws AddressException if address was incorrect - * @throws MessagingException if the operation failed - */ - void notifyAdmin(String subject, String body) throws AddressException, MessagingException { - String adminEmail = Configuration.get("LamsSupportEmail"); - if (!StringUtils.isEmpty(adminEmail)) { - sendFromSupportEmail(subject, adminEmail, body); - } - } + } } \ No newline at end of file Index: lams_common/src/java/org/lamsfoundation/lams/util/ConfigurationKeys.java =================================================================== diff -u -rc613857b45c1cb2e741462488ad1b0bffd78c62b -r3b1c6836dcfb5f9b80c9284d1815139d1fc08b30 --- lams_common/src/java/org/lamsfoundation/lams/util/ConfigurationKeys.java (.../ConfigurationKeys.java) (revision c613857b45c1cb2e741462488ad1b0bffd78c62b) +++ lams_common/src/java/org/lamsfoundation/lams/util/ConfigurationKeys.java (.../ConfigurationKeys.java) (revision 3b1c6836dcfb5f9b80c9284d1815139d1fc08b30) @@ -244,5 +244,9 @@ public static String ADMIN_SCREEN_SIZE = "AdminScreenSize"; public static String GMAP_KEY = "GmapKey"; + + public static String SMTP_AUTH_USER = "SMTPUser"; + + public static String SMTP_AUTH_PASSWORD = "SMTPPassword"; } \ No newline at end of file