Java Spring Boot - 忘记密码 - 带速度的电子邮件服务?

时间:2017-09-03 12:00:04

标签: java spring spring-boot velocity

我是Java和Spring Boot的新手。我已经开始尝试绘制各种函数和参数。

  1. 忘记密码 - 发送电子邮件。

    malloc
  2. 这就是旧电子邮件服务的样子 - 与Java Spring Boot的对比是什么? - 要导入哪些库?

        //__ send email notification
        SimpleEmailService.generateEmail("forgotPasswordTemplate", data);
    

    这就是我目前的代码库 - 我基本上想用api为反应器构建提供支持 - 所以在架构方面 - / api将是一个获取服务 - 另一个是来自表单的帖子

    -

    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();                        
       }
    }
    

1 个答案:

答案 0 :(得分:0)

具体到Spring Boot速度支持为dropped,因为速度1更新到2010年,这里是example of usageexplanation on velocity with spring boot 1.5

这是在velocity 2发布之前。

Freemarker也受支持,此处example使用Spring Boot 1.4

所以这取决于你的Spring Boot版本。