Java tutorial
package com.miserablemind.butter.domain.service.email; import com.miserablemind.butter.apps.ConfigApp; import com.miserablemind.butter.domain.model.user.user.AppUser; import org.apache.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; import javax.mail.internet.InternetAddress; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; /* * Miserable Mind * http://www.butter.miserablemind.com * The MIT License (MIT) */ /** * Service layer bean with defined methods that prepare {@link EmailMessage} and send it through {@link EmailService}. * <p>Velocity template engine uses {@code model} in {@link EmailMessage} to prepare the body.</p> * <p>To make the http request faster {@code @Async} annotation is used for methods to send the e-mail in a different thread.</p> * <p>The Service does not check if user e-mail address is verified. This should be done in a layer that calls the service.</p> * * @author <a href="http://www.miserablemind.com" target="_blank">miserablemind</a> */ @Service public class EmailManager { @Autowired private EmailService emailService; private static final Logger logger = Logger.getLogger(EmailManager.class); /** * Sends e-mail address verification message to a user. * The method can be used for initial sign-up process as well as user e-mail address update. * * @param appUser user instance an e-mail message is being sent to * @param verificationToken a token to build a verification URL that user will be identified against * @param configApp app specific configuration in order to retrieve "from" value and base url */ @Async public void sendEmailVerification(AppUser appUser, String verificationToken, ConfigApp configApp) { String subjectLine = "E-mail Verification for " + configApp.getAppName() + "!"; try { InternetAddress fromField = new InternetAddress(configApp.getEmailAddressNoReply(), configApp.getAppName() + " - No Reply"); EmailMessage emailMessage = new EmailMessage(appUser.getEmail(), fromField, subjectLine, null, "signup-confirmation.vm"); emailMessage.addModelProperty("userName", appUser.getUsername()); emailMessage.addModelProperty("appName", configApp.getAppName()); emailMessage.addModelProperty("verificationUrl", configApp.getBaseAppUrl() + "/user/verify?token=" + verificationToken); this.emailService.sendMimeMail(emailMessage); } catch (UnsupportedEncodingException e) { logger.error("Could Not create InternetAddress for " + configApp.getEmailAddressNoReply(), e); } } /** * Sends e-mail to user when user requests a password reset. * * @param appUser user instance an e-mail message is being sent to * @param passwordResetToken a token that is used for building URL that user clicks on to reset password. * @param configApp app specific configuration in order to retrieve "from" value and base url * @throws UnsupportedEncodingException */ @Async public void sendEmailResetPassword(AppUser appUser, String passwordResetToken, ConfigApp configApp) throws UnsupportedEncodingException { String subjectLine = "Password Reset - " + configApp.getAppName(); InternetAddress fromField = new InternetAddress(configApp.getEmailAddressNoReply(), configApp.getAppName() + " - No Reply"); EmailMessage emailMessage = new EmailMessage(appUser.getEmail(), fromField, subjectLine, null, "password-reset.vm"); emailMessage.addModelProperty("userName", appUser.getUsername()); emailMessage.addModelProperty("appName", configApp.getAppName()); emailMessage.addModelProperty("resetUrl", configApp.getBaseAppUrl() + "/reset-password/?token=" + passwordResetToken + "&email=" + URLEncoder.encode(appUser.getEmail(), "UTF-8")); this.emailService.sendMimeMail(emailMessage); } }