速度零点异常(velocity null point exception)

2019-08-08 06:43发布

我想用Velocity模板来发送邮件。

基于Spring 3.1的文档我的配置。

我有一个配置的XML文件:

<?xml version="1.0" encoding="UTF-8"?>

http://www.springframework.org/schema/beans/spring-beans-3.1.xsd“>

<bean id="sendMail" class="com.myclass.app.SendMail">
    <property name="mailSender" ref="mailSender"/>
    <property name="velocityEngine" ref="velocityEngine"/>
</bean>

<bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
    <property name="velocityProperties">
        <value>
            resource.loader=class
            class.resource.loader.class=org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
        </value>
    </property>
</bean>

然后,我有类:

@Controller
public class SendMail{


    private static final Logger logger = Logger.getLogger(SendMail.class);

    private JavaMailSender mailSender;
    private VelocityEngine velocityEngine;

    public void setMailSender(JavaMailSender mailSender) {
        this.mailSender = mailSender;
    }

    public void setVelocityEngine(VelocityEngine velocityEngine) {
        this.velocityEngine = velocityEngine;
    }


    @RequestMapping(value = "/sendEmail", method = RequestMethod.GET)
    public @ResponseBody void send(){

        sentEmail();

    }

    private void sentEmail(){

        try {


//            SimpleMailMessage msg = new SimpleMailMessage(mailMessage);

            MimeMessage message = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message,
                    true, "UTF-8");
            helper.setFrom("from@from.com");
            helper.setTo("myEmail");

            helper.setSubject("title");

            Map map = new HashMap();
            map.put("user", "Piotr");
            map.put("test", "TEST");

            String text1 = VelocityEngineUtils.mergeTemplateIntoString(
                    velocityEngine, "velocity.vm", map );

            logger.info(text1);

            String text = "test";

                helper.setText(text, true);


            mailSender.send(message);

        } catch (Exception ex) {
            System.out.println(ex.getStackTrace());
            System.out.println(ex.getMessage());
        }


    }

}

我的虚拟机文件位于“资源”文件夹中。

和所有我收到ID“零点异常”;

响应不给我罢了。

你有任何想法,该怎么办?

Answer 1:

你的模板不能在资源,他们需要的,你可以在你的applicationContext.xml的velocityEngine配置类路径:

<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
  <property name="host" value="ADDRESS_OF_YOUR_SMTP_SERVER"/>
  <property name="defaultEncoding" value="UTF-8"/>
  <property name="port" value="XX_PORT_SERVER"/>
  <property name="javaMailProperties">  
      <props>
        <prop key="mail.smtps.auth">false</prop> <!-- depending on your smtp server -->
        <prop key="mail.smtp.starttls.enable">false</prop>
      </props>
  </property>
</bean>

<bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean" p:resourceLoaderPath="classpath:META-INF/velocity" />

不要忘了命名空间的xmlns:P =“http://www.springframework.org/schema/p”用上面的例子中的属性。

在Java类的SendMail,不要忘记添加@Autowired注释:

@Autowired
public void setMailSender(JavaMailSender mailSender) {
    this.mailSender = mailSender;
}

@Autowired
public void setVelocityEngine(VelocityEngine velocityEngine) {
    this.velocityEngine = velocityEngine;
}

最后,发送电子邮件,使用MimeMessagePreparator:

            MimeMessagePreparator preparator = new MimeMessagePreparator() {
            @Override
            public void prepare(MimeMessage mimeMessage) throws Exception {
               MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
               message.setTo("blabla@test.com");
               message.setSubject("Email test");

               String text = VelocityEngineUtils.mergeTemplateIntoString(
                   velocityEngine, template, keywords);
               message.setText(text, true);

             }
          };

          //Send email using the autowired mailSender
          this.mailSender.send(preparator);


文章来源: velocity null point exception