Java tutorial
/* * Copyright (C) 2018 FormKiQ Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.formkiq.core.service.notification; import java.io.IOException; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Multipart; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; import org.apache.commons.text.StringSubstitutor; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import com.formkiq.core.domain.User; import com.formkiq.core.service.AuthenticationFailureException; import com.formkiq.core.service.SpringSecurityService; import com.formkiq.core.service.SystemPropertyService; import com.formkiq.core.service.UserService; import com.formkiq.core.util.Resources; import com.google.common.collect.ImmutableMap; /** * Central location for external email communication. * */ @Service public class ExternalMailSender { /** {@link SystemPropertyService}. */ @Autowired private SystemPropertyService systemProperties; /** {@link MailSenderService}. */ @Autowired private MailSenderService mail; /** {@link SpringSecurityService}. */ @Autowired private SpringSecurityService securityService; /** {@link UserService}. */ @Autowired private UserService userservice; /** * Sends Lost Password Email. * @param email {@link String} * @throws AuthenticationFailureException thrown if email doesn't exist * @throws IOException IOException */ @Transactional public void sendLostPasswordEmail(final String email) throws AuthenticationFailureException, IOException { String html = Resources.getResourceAsString("/email/lostpassword.html"); String text = Resources.getResourceAsString("/email/lostpassword.txt"); try { sendResetEmail(email, email, "FormKiQ Lost Password", text, html); } catch (MessagingException e) { throw new IOException(e); } } /** * Send New User Welcome Email. * @param email {@link String} * @throws IOException IOException */ @Transactional public void sendNewUser(final String email) throws IOException { User user = (User) this.securityService.getUserDetails(); String html = Resources.getResourceAsString("/email/newuser.html"); String text = Resources.getResourceAsString("/email/newuser.txt"); try { String subject = "Welcome to FormKiQ"; sendResetEmail(email, user.getEmail(), subject, text, html); } catch (MessagingException e) { throw new IOException(e); } } /** * Send Reset Email. * @param to {@link String} * @param email {@link String} * @param subject {@link String} * @param text {@link String} * @param html {@link String} * @throws MessagingException MessagingException */ private void sendResetEmail(final String to, final String email, final String subject, final String text, final String html) throws MessagingException { String hostname = this.systemProperties.getSystemHostname(); String resetToken = this.userservice.generateResetToken(to); StringSubstitutor s = new StringSubstitutor( ImmutableMap.of("hostname", hostname, "to", to, "email", email, "resettoken", resetToken)); MimeBodyPart textPart = new MimeBodyPart(); textPart.setText(s.replace(text), "UTF-8"); s.replace(html); MimeBodyPart htmlPart = new MimeBodyPart(); htmlPart.setContent(s.replace(html), "text/html;charset=UTF-8"); final Multipart mp = new MimeMultipart("alternative"); mp.addBodyPart(textPart); mp.addBodyPart(htmlPart); MimeMessage msg = this.mail.createMimeMessage(); msg.setContent(mp); msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); msg.setSubject(subject); this.mail.send(msg); } }