0

我正在尝试使用速度框架通过我的 web 应用程序发送电子邮件。我已经以这种方式配置了我的 email-context.xml

..............    
<bean id="velocityEngine"
            class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
            <property name="velocityProperties">
                <props>
                    <prop key="resource.loader">file</prop>
                    <prop key="file.resource.loader.class">org.apache.velocity.runtime.resource.loader.FileResourceLoader</prop>
                    <prop key="file.resource.loader.path">/WEB-INF/velocity/</prop>
                </props>
            </property>
        </bean>

        <bean id="mailService" class="--.------.-----.service.MailServiceImpl">
            <property name="mailSender" ref="mailSender" />
            <property name="velocityEngine" ref="velocityEngine" />
        </bean>

然后我像这样编写了我的 mailServiceImpl 类

public class MailServiceImpl implements MailService {

    private JavaMailSender mailSender;
    private VelocityEngine velocityEngine;


    public VelocityEngine getVelocityEngine() {
        return velocityEngine;
    }

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

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

    ........................

    public void sendEmail(final SimpleMailMessage msg, final Map<Object, Object> map) throws MessagingException {
        System.out.println("Sending mail...");
        MimeMessagePreparator preparator = new MimeMessagePreparator() {
            public void prepare(MimeMessage mimeMessage) throws Exception {
                MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true);
                message.setTo(msg.getTo());
                message.setFrom(msg.getFrom());
                message.setSubject(msg.getSubject());

                String body = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, "invite.vm", map);

                message.setText(body, true);
            }
        };
        mailSender.send(preparator);
    }
...........

当我对上述方法启动测试时,我得到了这个异常

........
19 feb 2013 17:13:46 TRACE [main] TransactionAspectSupport - Completing transaction for [it.stasbranger.spidly.service.MailServiceImpl.sendEmail] after exception: org.springframework.mail.MailPreparationException: Could not prepare mail; nested exception is org.apache.velocity.exception.ResourceNotFoundException: Unable to find resource 'invite.vm'
19 feb 2013 17:13:46 TRACE [main] RuleBasedTransactionAttribute - Applying rules to determine whether transaction should rollback on org.springframework.mail.MailPreparationException: Could not prepare mail; nested exception is org.apache.velocity.exception.ResourceNotFoundException: Unable to find resource 'invite.vm'
19 feb 2013 17:13:46 TRACE [main] RuleBasedTransactionAttribute - Winning rollback rule is: null
...........

有什么建议么?

4

0 回答 0