1 package de.spring.webservices.rest.controller;
 
   3 import java.time.OffsetDateTime;
 
   4 import java.time.format.DateTimeFormatter;
 
   5 import java.util.HashMap;
 
   6 import java.util.Locale;
 
   9 import javax.mail.MessagingException;
 
  11 import org.slf4j.Logger;
 
  12 import org.slf4j.LoggerFactory;
 
  13 import org.springframework.beans.factory.annotation.Autowired;
 
  14 import org.springframework.core.io.ClassPathResource;
 
  15 import org.springframework.core.io.Resource;
 
  16 import org.springframework.http.HttpStatus;
 
  17 import org.springframework.web.bind.annotation.RequestMapping;
 
  18 import org.springframework.web.bind.annotation.RequestMethod;
 
  19 import org.springframework.web.bind.annotation.ResponseStatus;
 
  20 import org.springframework.web.bind.annotation.RestController;
 
  22 import de.spring.emails.services.EmailMakerService;
 
  23 import de.spring.emails.services.EmailService;
 
  26 @RequestMapping("/api/emails/")
 
  27 public class EmailController {
 
  28         private static final Logger LOGGER = LoggerFactory.getLogger(EmailController.class);
 
  29         private static final String USER = "Gustavo Martin Morcuende";
 
  30         private static final String USER_ADDRESS = "noemail@gumartinm.name";
 
  31         private static final String TEMPLATE = "email-template";
 
  32         private static final String SUBJECT_MESSAGE_KEY = "email.subject";
 
  34         private final EmailService emailService;
 
  35         private final EmailMakerService emailMakerService;
 
  38     public EmailController(EmailService emailService, EmailMakerService emailMakerService) {
 
  39                 this.emailService = emailService;
 
  40                 this.emailMakerService = emailMakerService;
 
  43         @RequestMapping(method = RequestMethod.GET)
 
  44     @ResponseStatus(HttpStatus.OK)
 
  45     public void emails() throws MessagingException {
 
  46                 final String emailSubject = emailMakerService.getSubject(SUBJECT_MESSAGE_KEY, Locale.getDefault());
 
  47                 final String emailText = doEmailText();
 
  48                 final String[] to = { USER_ADDRESS };   
 
  49                 final Map<String, Resource> inline = new HashMap<>();
 
  50                 inline.put("cid:mainlogo", new ClassPathResource("email/logo.png"));
 
  52                         emailService.sendEmailAsync(to, emailSubject, emailText, true, null, inline);
 
  53                 } catch (MessagingException ex) {
 
  54                         LOGGER.error("Send email error", ex);
 
  58         private String doEmailText() {
 
  59             final String isoDateTime = OffsetDateTime.now().format(DateTimeFormatter.ISO_OFFSET_DATE_TIME);
 
  60                 final Map<String, String> text = new HashMap<>();
 
  61                 text.put("user", USER);
 
  62                 text.put("date", isoDateTime);
 
  63                 return emailMakerService.emailMaker(text, TEMPLATE, Locale.getDefault());