// Path: src/main/java/com/yyqian/imagine/exception/BadRequestException.java
// public class BadRequestException extends RuntimeException {
//   public BadRequestException() {
//   }
// 
//   public BadRequestException(String message) {
//     super(message);
//   }
// 
//   public BadRequestException(String message, Throwable cause) {
//     super(message, cause);
//   }
// 
//   public BadRequestException(Throwable cause) {
//     super(cause);
//   }
// 
//   public BadRequestException(String message, Throwable cause, boolean enableSuppression,
//                              boolean writableStackTrace) {
//     super(message, cause, enableSuppression, writableStackTrace);
//   }
// }
// 
// Path: src/main/java/com/yyqian/imagine/exception/InternalServerErrorException.java
// public class InternalServerErrorException extends RuntimeException {
//   public InternalServerErrorException() {
//   }
// 
//   public InternalServerErrorException(String message) {
//     super(message);
//   }
// 
//   public InternalServerErrorException(String message, Throwable cause) {
//     super(message, cause);
//   }
// 
//   public InternalServerErrorException(Throwable cause) {
//     super(cause);
//   }
// 
//   public InternalServerErrorException(String message, Throwable cause, boolean enableSuppression,
//                                       boolean writableStackTrace) {
//     super(message, cause, enableSuppression, writableStackTrace);
//   }
// }
// 
// Path: src/main/java/com/yyqian/imagine/service/MailService.java
// public interface MailService {
// 
//   void sendHtml(String to, String subject, String templateName, Map<String, Object> model);
// 
//   void sendRestPasswordEmail(String mailTo, String url);
// 
// }


import com.yyqian.imagine.exception.BadRequestException;
import com.yyqian.imagine.exception.InternalServerErrorException;
import com.yyqian.imagine.service.MailService;
import org.apache.commons.validator.routines.EmailValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSenderImpl;
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.util.HashMap;
import java.util.Map;
package com.yyqian.imagine.service.impl;



/**
 * Created on 12/30/15.
 *
 * @author Yinyin Qian
 */
@Service
public class MailServiceImpl implements MailService {

  @Value("${app.name}")
  private String APP_NAME;

  @Autowired
  private JavaMailSenderImpl mailSender;

  @Autowired
  private TemplateEngine templateEngine;

  @Override
  public void sendHtml(String to, String subject, String templateName, Map<String, Object> model) {
    if (!EmailValidator.getInstance().isValid(to)) {
      throw new BadRequestException("Invalid email address.");
    }
    final Context ctx = new Context();
    ctx.setVariables(model);
    final String emailText = templateEngine.process(templateName, ctx);
    final MimeMessage msg = mailSender.createMimeMessage();
    try {
      final MimeMessageHelper helper = new MimeMessageHelper(msg, true);
      helper.setFrom(mailSender.getUsername());
      helper.setTo(to);
      helper.setSubject(subject);
      helper.setText(emailText, true);
    } catch (MessagingException ex) {
// Path: src/main/java/com/yyqian/imagine/exception/BadRequestException.java
// public class BadRequestException extends RuntimeException {
//   public BadRequestException() {
//   }
// 
//   public BadRequestException(String message) {
//     super(message);
//   }
// 
//   public BadRequestException(String message, Throwable cause) {
//     super(message, cause);
//   }
// 
//   public BadRequestException(Throwable cause) {
//     super(cause);
//   }
// 
//   public BadRequestException(String message, Throwable cause, boolean enableSuppression,
//                              boolean writableStackTrace) {
//     super(message, cause, enableSuppression, writableStackTrace);
//   }
// }
// 
// Path: src/main/java/com/yyqian/imagine/exception/InternalServerErrorException.java
// public class InternalServerErrorException extends RuntimeException {
//   public InternalServerErrorException() {
//   }
// 
//   public InternalServerErrorException(String message) {
//     super(message);
//   }
// 
//   public InternalServerErrorException(String message, Throwable cause) {
//     super(message, cause);
//   }
// 
//   public InternalServerErrorException(Throwable cause) {
//     super(cause);
//   }
// 
//   public InternalServerErrorException(String message, Throwable cause, boolean enableSuppression,
//                                       boolean writableStackTrace) {
//     super(message, cause, enableSuppression, writableStackTrace);
//   }
// }
// 
// Path: src/main/java/com/yyqian/imagine/service/MailService.java
// public interface MailService {
// 
//   void sendHtml(String to, String subject, String templateName, Map<String, Object> model);
// 
//   void sendRestPasswordEmail(String mailTo, String url);
// 
// }


// Path: src/main/java/com/yyqian/imagine/service/impl/MailServiceImpl.java
import com.yyqian.imagine.exception.BadRequestException;
import com.yyqian.imagine.exception.InternalServerErrorException;
import com.yyqian.imagine.service.MailService;
import org.apache.commons.validator.routines.EmailValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSenderImpl;
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.util.HashMap;
import java.util.Map;

package com.yyqian.imagine.service.impl;



/**
 * Created on 12/30/15.
 *
 * @author Yinyin Qian
 */
@Service
public class MailServiceImpl implements MailService {

  @Value("${app.name}")
  private String APP_NAME;

  @Autowired
  private JavaMailSenderImpl mailSender;

  @Autowired
  private TemplateEngine templateEngine;

  @Override
  public void sendHtml(String to, String subject, String templateName, Map<String, Object> model) {
    if (!EmailValidator.getInstance().isValid(to)) {
      throw new BadRequestException("Invalid email address.");
    }
    final Context ctx = new Context();
    ctx.setVariables(model);
    final String emailText = templateEngine.process(templateName, ctx);
    final MimeMessage msg = mailSender.createMimeMessage();
    try {
      final MimeMessageHelper helper = new MimeMessageHelper(msg, true);
      helper.setFrom(mailSender.getUsername());
      helper.setTo(to);
      helper.setSubject(subject);
      helper.setText(emailText, true);
    } catch (MessagingException ex) {
      throw new InternalServerErrorException(ex.getMessage());