diff --git a/src/main/java/de/thm/arsnova/services/UserService.java b/src/main/java/de/thm/arsnova/services/UserService.java index 2a5da93977f850ac71fa655dca9502f675b70b44..cafd8a1896a477b3eb79b0f2b8723728cd63687a 100644 --- a/src/main/java/de/thm/arsnova/services/UserService.java +++ b/src/main/java/de/thm/arsnova/services/UserService.java @@ -13,6 +13,8 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.regex.Pattern; import javax.annotation.PreDestroy; +import javax.mail.MessagingException; +import javax.mail.internet.MimeMessage; import org.apache.commons.lang.RandomStringUtils; import org.apache.commons.lang.StringUtils; @@ -24,8 +26,8 @@ import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.mail.MailException; -import org.springframework.mail.MailSender; -import org.springframework.mail.SimpleMailMessage; +import org.springframework.mail.javamail.JavaMailSender; +import org.springframework.mail.javamail.MimeMessageHelper; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.security.authentication.AnonymousAuthenticationToken; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; @@ -73,10 +75,7 @@ public class UserService implements IUserService { private ARSnovaSocketIOServer socketIoServer; @Autowired - private MailSender mailSender; - - @Autowired - private SimpleMailMessage regMailTemplate; + private JavaMailSender mailSender; @Value("${security.user-db.allowed-email-domains}") private String allowedEmailDomains; @@ -84,6 +83,18 @@ public class UserService implements IUserService { @Value("${security.arsnova-url}") private String arsnovaUrl; + @Value("${mail.sender.address}") + private String regMailSenderAddress; + + @Value("${mail.sender.name}") + private String regMailSenderName; + + @Value("${security.user-db.registration-mail.subject}") + private String regMailSubject; + + @Value("${security.user-db.registration-mail.body}") + private String regMailBody; + private Pattern mailPattern; private BytesKeyGenerator keygen; private BCryptPasswordEncoder encoder; @@ -321,15 +332,21 @@ public class UserService implements IUserService { } public void sendActivationEmail(DbUser dbUser) { - SimpleMailMessage msg = new SimpleMailMessage(regMailTemplate); String activationUrl = MessageFormat.format("{0}/user/activate?username={1}&key={2}", arsnovaUrl, dbUser.getUsername(), dbUser.getActivationKey()); - msg.setTo(dbUser.getUsername()); - msg.setText(MessageFormat.format(msg.getText(), activationUrl)); - LOGGER.debug("Activation mail body: {}", msg.getText()); - + MimeMessage msg = mailSender.createMimeMessage(); + MimeMessageHelper helper = new MimeMessageHelper(msg); try { - LOGGER.info("Sending activation mail to {}", dbUser.getUsername()); + helper.setFrom(regMailSenderName + "<" + regMailSenderAddress + ">"); + helper.setTo(dbUser.getUsername()); + helper.setSubject(regMailSubject); + helper.setText(MessageFormat.format(regMailBody, activationUrl)); + msg.setHeader("Content-Type", "text/plain; charset=UTF-8"); + + LOGGER.debug("Message encoding: {}", new Object[] {helper.getEncoding()}); + LOGGER.info("Sending activation mail from \"{}\" to \"{}\"", new Object[] {msg.getFrom(), dbUser.getUsername()}); mailSender.send(msg); + } catch (MessagingException e) { + LOGGER.warn("Activation mail could not be sent: {}", e); } catch (MailException e) { LOGGER.warn("Activation mail could not be sent: {}", e); } diff --git a/src/main/webapp/WEB-INF/spring/spring-main.xml b/src/main/webapp/WEB-INF/spring/spring-main.xml index 328d79f17611f93bc712a24ab4e485545500b2f2..8be99a3dac2d7ab9e731dfe2e13751af8272b8e2 100644 --- a/src/main/webapp/WEB-INF/spring/spring-main.xml +++ b/src/main/webapp/WEB-INF/spring/spring-main.xml @@ -52,12 +52,6 @@ <property name="host" value="${mail.host}"/> </bean> - <bean id="templateMessage" class="org.springframework.mail.SimpleMailMessage"> - <property name="from" value="${mail.sender.address} <${mail.sender.address}>"/> - <property name="subject" value="${security.user-db.registration-mail.subject}"/> - <property name="text" value="${security.user-db.registration-mail.body}"/> - </bean> - <!-- Example of connector client configuration --> <!-- Uncomment bean definition to activate connector --> <!-- bean id="connectorClient" scope="singleton" class="de.thm.arsnova.connector.client.ConnectorClientImpl">