daily pastebin goal
21%
SHARE
TWEET

SimpleEmailService

kolbus May 16th, 2018 96 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.crud.tasks.service;
  2.  
  3. import com.crud.tasks.domain.Mail;
  4. import org.slf4j.Logger;
  5. import org.slf4j.LoggerFactory;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.mail.MailException;
  8. import org.springframework.mail.SimpleMailMessage;
  9. import org.springframework.mail.javamail.JavaMailSender;
  10. import org.springframework.mail.javamail.MimeMessageHelper;
  11. import org.springframework.mail.javamail.MimeMessagePreparator;
  12. import org.springframework.stereotype.Service;
  13.  
  14. @Service
  15. public class SimpleEmailService {
  16.     private static final Logger LOGGER = LoggerFactory.getLogger(SimpleMailMessage.class);
  17.  
  18.     @Autowired
  19.     private JavaMailSender javaMailSender;
  20.  
  21.     @Autowired
  22.     private MailCreatorService mailCreatorService;
  23.  
  24.     public void send(final Mail mail){
  25.         LOGGER.info("Starting email preparation...");
  26.  
  27.         try {
  28.             //SimpleMailMessage mailMessage = createMailMessage(mail);
  29.             javaMailSender.send(createMimeMessage(mail));
  30.             LOGGER.info("Email has been sent.");
  31.         }catch (MailException e){
  32.             LOGGER.error("Failed to process email sending: ", e.getMessage(), e);
  33.         }
  34.     }
  35.  
  36.     private MimeMessagePreparator createMimeMessage(final Mail mail){
  37.         return mimeMessage -> {
  38.             MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage);
  39.             messageHelper.setTo(mail.getMailTo());
  40.             messageHelper.setSubject(mail.getSubject());
  41.             messageHelper.setText(mailCreatorService.buildTrelloCardEmail(mail.getMessage()), true);
  42.         };
  43.     }
  44.     private SimpleMailMessage createMailMessage(final Mail mail){
  45.         SimpleMailMessage mailMessage = new SimpleMailMessage();
  46.         mailMessage.setTo(mail.getMailTo());
  47.         mailMessage.setSubject(mail.getSubject());
  48.         mailMessage.setText(mailCreatorService.buildTrelloCardEmail(mail.getMessage()));
  49.         if(mail.getToCc()!=null){
  50.             mailMessage.setCc(mail.getToCc());
  51.         }
  52.         return mailMessage;
  53.     }
  54. }
RAW Paste Data
Top