/* * ============================================================================= * * Copyright (c) 2011-2014, The THYMELEAF team (http://www.thymeleaf.org) * * 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.neblina.balero.service; import com.neblina.balero.domain.Property; import com.neblina.balero.service.repository.PropertyRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.io.ByteArrayResource; import org.springframework.core.io.InputStreamSource; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.MimeMessageHelper; import org.springframework.stereotype.Service; import org.thymeleaf.TemplateEngine; import org.thymeleaf.context.Context; import javax.mail.MessagingException; import javax.mail.internet.MimeMessage; import java.io.UnsupportedEncodingException; import java.util.Arrays; import java.util.Base64; import java.util.Date; import java.util.Locale; @Service public class EmailService { @Autowired private JavaMailSender mailSender; @Autowired private TemplateEngine templateEngine; @Autowired private PropertyRepository propertyRepository; /* * Send HTML mail (simple) */ public void sendSimpleMail( final String recipientName, final String recipientEmail, final String subject, final String messageBody, final Locale locale) throws MessagingException, UnsupportedEncodingException { // Prepare the evaluation context final Context ctx = new Context(locale); ctx.setVariable("name", recipientName); ctx.setVariable("subscriptionDate", new Date()); ctx.setVariable("hobbies", Arrays.asList("Cinema", "Sports", "Music")); ctx.setVariable("message", messageBody); // Unsubscribe Link Base64.Encoder encoder = Base64.getEncoder(); String base64email = encoder.encodeToString(recipientEmail.getBytes("utf-8")); Property property = propertyRepository.findOneById(1L); String fullLink = property.getUrl() + "user/subscribe?unsubscribe=" + base64email; ctx.setVariable("unsubscribe", fullLink); // Prepare message using a Spring helper final MimeMessage mimeMessage = this.mailSender.createMimeMessage(); final MimeMessageHelper message = new MimeMessageHelper(mimeMessage, "UTF-8"); message.setSubject(subject); message.setFrom("thymeleaf@example.com"); message.setTo(recipientEmail); // Create the HTML body using Thymeleaf final String htmlContent = this.templateEngine.process("email-simple.html", ctx); message.setText(htmlContent, true /* isHtml */); // Send email this.mailSender.send(mimeMessage); } /* * Send HTML mail with attachment. */ public void sendMailWithAttachment( final String recipientName, final String recipientEmail, final String attachmentFileName, final byte[] attachmentBytes, final String attachmentContentType, final Locale locale) throws MessagingException { // Prepare the evaluation context final Context ctx = new Context(locale); ctx.setVariable("name", recipientName); ctx.setVariable("subscriptionDate", new Date()); ctx.setVariable("hobbies", Arrays.asList("Cinema", "Sports", "Music")); // Prepare message using a Spring helper final MimeMessage mimeMessage = this.mailSender.createMimeMessage(); final MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true /* multipart */, "UTF-8"); message.setSubject("Example HTML email with attachment"); message.setFrom("thymeleaf@example.com"); message.setTo(recipientEmail); // Create the HTML body using Thymeleaf final String htmlContent = this.templateEngine.process("email-withattachment.html", ctx); message.setText(htmlContent, true /* isHtml */); // Add the attachment final InputStreamSource attachmentSource = new ByteArrayResource(attachmentBytes); message.addAttachment( attachmentFileName, attachmentSource, attachmentContentType); // Send mail this.mailSender.send(mimeMessage); } /* * Send HTML mail with inline image */ public void sendMailWithInline( final String recipientName, final String recipientEmail, final String imageResourceName, final byte[] imageBytes, final String imageContentType, final Locale locale) throws MessagingException { // Prepare the evaluation context final Context ctx = new Context(locale); ctx.setVariable("name", recipientName); ctx.setVariable("subscriptionDate", new Date()); ctx.setVariable("hobbies", Arrays.asList("Cinema", "Sports", "Music")); ctx.setVariable("imageResourceName", imageResourceName); // so that we can reference it from HTML // Prepare message using a Spring helper final MimeMessage mimeMessage = this.mailSender.createMimeMessage(); final MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true /* multipart */, "UTF-8"); message.setSubject("Example HTML email with inline image"); message.setFrom("thymeleaf@example.com"); message.setTo(recipientEmail); // Create the HTML body using Thymeleaf final String htmlContent = this.templateEngine.process("email-inlineimage.html", ctx); message.setText(htmlContent, true /* isHtml */); // Add the inline image, referenced from the HTML code as "cid:${imageResourceName}" final InputStreamSource imageSource = new ByteArrayResource(imageBytes); message.addInline(imageResourceName, imageSource, imageContentType); // Send mail this.mailSender.send(mimeMessage); } }