Java tutorial
/** * Product : Hiperium Project * Architect: Andres Solorzano. * Created : 08-05-2009 - 23:30:00 * * The contents of this file are copyrighted by Andres Solorzano * and it is protected by the license: "GPL V3." You can find a copy of this * license at: http://www.hiperium.com/about/licence.html * * Copyright 2014 Andres Solorzano. All rights reserved. * */ package com.hiperium.bo.manager.mail; import java.text.MessageFormat; import java.util.Locale; import java.util.Properties; import java.util.ResourceBundle; import javax.ejb.LocalBean; import javax.ejb.Stateless; import javax.inject.Inject; import javax.interceptor.ExcludeDefaultInterceptors; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.AddressException; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import org.apache.commons.lang.StringUtils; import com.hiperium.bo.utils.CloudEmailUser; import com.hiperium.commons.EnumHiperiumTier; import com.hiperium.commons.EnumI18N; import com.hiperium.commons.HiperiumTier; import com.hiperium.commons.log.HiperiumLogger; import com.hiperium.model.security.User; /** * * @author Andres Solorzano * */ @Stateless @LocalBean @ExcludeDefaultInterceptors public class EmailMessageManager { /** The property GENERATED_USER_PASSWD_SUBJECT. */ private static final String GENERATED_USER_PASSWD_SUBJECT = "email.generatedUserPasswdSubject"; /** The property GENERATED_USER_PASSWD_CONTENT. */ private static final String GENERATED_USER_PASSWD_CONTENT = "email.generatedUserPasswdContent"; /** */ @Inject @HiperiumTier(EnumHiperiumTier.BUSINESS) private HiperiumLogger log; /** * Sends an email with the new user password. * * @param user * @throws javax.mail.internet.AddressException * @throws javax.mail.MessagingException */ public void sendNewPassword(User user) { try { this.log.debug("sendNewPassword() - START"); // Create the corresponding user locale. Locale locale = null; if (StringUtils.isBlank(user.getLanguageId())) { locale = Locale.getDefault(); } else { locale = new Locale(user.getLanguageId()); } ResourceBundle resource = ResourceBundle.getBundle(EnumI18N.COMMON.value(), locale); // Get system properties Properties properties = System.getProperties(); // Setup mail server properties.setProperty("mail.smtp.host", CloudEmailUser.HOST); properties.setProperty("mail.user", CloudEmailUser.USERNAME); properties.setProperty("mail.password", CloudEmailUser.PASSWORD); // Get the default Session object. Session session = Session.getDefaultInstance(properties); // Create a default MimeMessage object. MimeMessage message = new MimeMessage(session); // Set From: header field of the header. message.setFrom(new InternetAddress(CloudEmailUser.ADDRESS)); // Set To: header field of the header. message.addRecipient(Message.RecipientType.TO, new InternetAddress(user.getEmail())); // Set Subject: header field message.setSubject(resource.getString(GENERATED_USER_PASSWD_SUBJECT)); // Send the actual HTML message, as big as you like message.setContent(MessageFormat.format(resource.getString(GENERATED_USER_PASSWD_CONTENT), user.getFirstname(), user.getLastname(), user.getPassword()), "text/html"); // Send message Transport.send(message); this.log.debug("sendNewPassword() - END"); } catch (AddressException e) { this.log.error("AddressException to send email to " + user.getEmail(), e); } catch (MessagingException e) { this.log.error("MessagingException to send email to " + user.getEmail(), e); } } }