title: Java Spring Boot - forgot password - email service with velocity?
body: <p>I am fairly new to Java and Spring Boot. I've started to try and map out the various functions and parameters.</p>
  
  <ol start="3">
  <li><p>Forgot password -- email sending.</p>
  
  <pre><code>    //__ send email notification
      SimpleEmailService.generateEmail("forgotPasswordTemplate", data);
  </code></pre></li>
  </ol>
  
  <p>this is what the old email service was like -- what would be the equivlant with Java Spring Boot? -- What libs to import?</p>
  
  <pre><code>package controller;
  
  import org.apache.velocity.Template;
  import org.apache.velocity.VelocityContext;
  import org.apache.velocity.app.VelocityEngine;
  import org.springframework.mail.javamail.JavaMailSender;
  import org.springframework.mail.javamail.MimeMessageHelper;
  import org.springframework.mail.javamail.MimeMessagePreparator;
  
  import com.mongodb.BasicDBObject;
  
  import javax.mail.internet.MimeMessage;
  
  import java.io.StringWriter;
  
  public class SimpleEmailService {
     private static JavaMailSender mailSender;
     private static VelocityEngine velocityEngine;
  
     public void setMailSender(JavaMailSender mailSender) {
        this.mailSender = mailSender;
     }
  
     public void setVelocityEngine(VelocityEngine velocityEngine) {
        this.velocityEngine = velocityEngine;
     }
  
     public static void generateEmail(final String templateType, final BasicDBObject user) {
        MimeMessagePreparator preparator = new MimeMessagePreparator() {
           public void prepare(MimeMessage mimeMessage) throws Exception { 
               String velocityTemplateName = null;
               String subject = null;
  
              if(templateType.equals("forgotPasswordTemplate")){
                   velocityTemplateName = "forgot_password";
                   subject = "Forgot Password";
                   user.put("imgPath", "http://livesite.com/images/emails/forgot_password/");
              }
  
  
              if(templateType.equals("registerTemplate")){
                  velocityTemplateName = "registration_confirmation";
                  subject = "Registration";
                  user.put("imgPath", "http://livesite.com/images/emails/registration_confirmation/");
              }
  
  
              MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
                  message.setTo("my@test.com");
                  message.setFrom("hello@test.com"); // could be parameterized...
  
  
                  message.setSubject(subject);
  
              VelocityContext model = new VelocityContext();
                  model.put("user", user);            
  
              String text = applyTemplate(model, velocityTemplateName+".vm");
                  message.setText(text, true);
           }
        };
        mailSender.send(preparator);
     }
  
     public static String applyTemplate(VelocityContext data, String templateName){                  
         StringWriter sw= new StringWriter();        
         Template t = velocityEngine.getTemplate("net/velocity/templates/"+templateName);
         t.merge(data, sw);
         return sw.toString();                        
     }
  }
  </code></pre>
  
  <hr>
  
  <p>This is what my current code base looks like -- I essentially want to power a reactjs build with an api -- so in terms of architecture -- the /api would be a get service -- the other's are post's from forms.</p>
  
  <p>--</p>
  
  <pre><code>package controller;
  
  
  import java.util.List;
  
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpSession;
  
  import org.apache.tomcat.util.log.SystemLogHandler;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.http.HttpStatus;
  import org.springframework.http.ResponseEntity;
  import org.springframework.stereotype.Controller;
  import org.springframework.web.bind.annotation.CrossOrigin;
  import org.springframework.web.bind.annotation.RequestMapping;
  import org.springframework.web.bind.annotation.RequestMethod;
  import org.springframework.web.bind.annotation.RequestParam;
  import org.springframework.web.bind.annotation.RestController;
  
  import domain.Account;
  import repo.AccountRepository;
  
  
  @RestController
  public class MyService {
  
      @Autowired
      private AccountRepository accountRepository;
  
  
      @RequestMapping(value = "/api", method = RequestMethod.GET)
      @CrossOrigin(origins = {"*"})
      public ResponseEntity&lt;?&gt; index(@RequestParam(value="name", required=false, defaultValue="admin1") String name) throws Exception {
              Account acc = accountRepository.findByUsername(name);
              return new ResponseEntity&lt;&gt;(acc, HttpStatus.OK);
      }
  
  
  
  
      @RequestMapping(value = "/login", method = RequestMethod.GET)
      @CrossOrigin(origins = {"*"})
      public ResponseEntity&lt;?&gt; login(
              @RequestParam(value="email", required=false, defaultValue="email") String email,
              @RequestParam(value="password", required=false, defaultValue="password") String password, 
              HttpServletRequest request
              ) throws  Exception {
              List&lt;Account&gt; acc = accountRepository.findByEmail(email);
  
              HttpSession session = request.getSession(true);
              session.setAttribute("user", acc);
  
              System.out.println("logged&gt;&gt;&gt;"+acc);
              System.out.println("session&gt;&gt;&gt;"+session);
  
              return new ResponseEntity&lt;&gt;(acc, HttpStatus.OK);
      }
  
      @SuppressWarnings("unchecked")
      @RequestMapping(value = "/logout", method = RequestMethod.GET)
      @CrossOrigin(origins = {"*"})
      public ResponseEntity&lt;?&gt; logout(
              HttpServletRequest request
              ) throws  Exception {
  
              HttpSession session = request.getSession(true);
              List&lt;Account&gt; acc = (List&lt;Account&gt;) session.getAttribute("user");
  
              session.removeAttribute("user");            
  
              System.out.println("logout&gt;&gt;&gt;"+acc);
              System.out.println("session&gt;&gt;&gt;"+session);
  
              return new ResponseEntity&lt;&gt;(acc, HttpStatus.OK);
      }
  
      @RequestMapping(value = "/forgot", method = RequestMethod.GET)
      @CrossOrigin(origins = {"*"})
      public ResponseEntity&lt;?&gt; forgot(
              @RequestParam(value="email", required=false, defaultValue="email") String email
              ) throws  Exception {
              List&lt;Account&gt; acc = accountRepository.findByEmail(email);
              return new ResponseEntity&lt;&gt;(acc, HttpStatus.OK);
      }
  
      @RequestMapping(value = "/edit", method = RequestMethod.GET)
      @CrossOrigin(origins = {"*"})
      public ResponseEntity&lt;?&gt; edit(
              @RequestParam(value="username", required=false, defaultValue="username") String username,
              @RequestParam(value="password", required=false, defaultValue="password") String password,
              @RequestParam(value="email", required=false, defaultValue="email") String email
              ) throws  Exception {
              Account acc = accountRepository.findByUsername(username);
              return new ResponseEntity&lt;&gt;(acc, HttpStatus.OK);
      }
  
      @RequestMapping(value = "/register", method = RequestMethod.GET)
      @CrossOrigin(origins = {"*"})
      public ResponseEntity&lt;?&gt; register(
              @RequestParam(value="username", required=false, defaultValue="username") String username,
              @RequestParam(value="password", required=false, defaultValue="password") String password,
              @RequestParam(value="email", required=false, defaultValue="email") String email
              ) throws  Exception {
          Account checkAccount = accountRepository.findByUsername(username);
          System.out.println("checkAccount&gt;&gt;&gt;"+checkAccount);
          if (checkAccount == null) {
              try {               
                  // if not create some
                  Account newAcc = accountRepository.saveAndFlush(new Account(
                              username, 
                              password, 
                              email
                          ));
                  System.out.println("newAcc&gt;&gt;&gt;"+checkAccount);
                  return new ResponseEntity&lt;&gt;(newAcc, HttpStatus.OK);
              }
              catch (Exception e) {
                  e.printStackTrace();
                  System.out.println("error adding user&gt;&gt;&gt;"+checkAccount);
                  return new ResponseEntity&lt;&gt;(e, HttpStatus.OK);
              }
          }else{
              // String e = "user exists";
              System.out.println("user already exists&gt;&gt;&gt;"+checkAccount);
              return new ResponseEntity&lt;&gt;(checkAccount, HttpStatus.OK);
          }
      }   
  
  }
  </code></pre>