Page 1 of 1

[Resolved]Problem configuring mail service

Posted: Mon Dec 10, 2012 4:58 am
by pokemon007
I've configured mail service for admin using 1.6 scheme. I want to try the new email template with Thymeleaf resolver engine. Somehow VelocityEngine can't locate the template:

Code: Select all

[ERROR] 00:53:45 VelocityEngine - ResourceManager : unable to find resource 'register-email' in any resource loader.


I checked tomcat6 and see the template html files are under $tomcat_home/webapps/myapp/WEB-INF/classes/emailTemplates/. Don't know why it doesn't locate the template. Here is my applicationContext-email.xml:

Code: Select all

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

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
      http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">

   <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
      <description>Property configurer for notification service properties input.</description>
      <property name="locations">
         <list>
            <value>file:///${CATALINA_HOME}/config/commerce/email/email.properties</value>
         </list>
      </property>
   </bean>

   <bean id="blServerInfo" class="org.broadleafcommerce.common.email.service.info.ServerInfo">
      <property name="serverName" value="${server.name}" />
      <property name="serverPort" value="${server.port}" />
   </bean>

   <bean id="blMailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
      <property name="host"><value>${mail.smtp.host}</value></property>
      <property name="port"><value>${mail.smtp.port}</value></property>
      <property name="protocol"><value>${mail.smtp.protocol}</value></property>
      <property name="username"><value>${mail.smtp.username}</value></property>
      <property name="password"><value>${mail.smtp.password}</value></property>
      <property name="javaMailProperties">
         <props>
            <prop key="mail.smtp.starttls.enable">false</prop>
            <prop key="mail.smtp.auth">true</prop>
            <prop key="mail.smtp.timeout">25000</prop>
         </props>
      </property>
   </bean>
   
    <bean id="blEmailTemplateResolver" class="org.thymeleaf.templateresolver.ClassLoaderTemplateResolver">
        <property name="prefix" value="emailTemplates/" />
        <property name="suffix" value=".html" />
        <property name="templateMode" value="HTML5" />
        <property name="cacheable" value="false"/>
    </bean>

    <bean id="blEmailTemplateEngine" class="org.thymeleaf.spring3.SpringTemplateEngine">
        <property name="templateResolvers">
            <set>
                <ref bean="blEmailTemplateResolver" />
            </set>
        </property>
        <property name="dialects">
            <set>
                <bean class="org.thymeleaf.spring3.dialect.SpringStandardDialect" />
                <ref bean="blDialect" />
            </set>
        </property>
    </bean>

    <bean id="blMessageCreator" class="org.broadleafcommerce.common.email.service.message.ThymeleafMessageCreator">
        <constructor-arg ref="blEmailTemplateEngine"/>
        <constructor-arg ref="blMailSender"/>
    </bean>
 
   <bean id="blEmailInfo" class="org.broadleafcommerce.common.email.service.info.EmailInfo">
      <property name="fromAddress" value="${mail.smtp.from}" />
      <property name="sendEmailReliableAsync" value="false" />
      <property name="sendAsyncPriority" value="2" />
   </bean>
     
   <bean id="blRegistrationEmailInfo" parent="blEmailInfo">
      <property name="subject" value="You have successfully registered!"/>      
      <property name="emailTemplate" value="register-email"/>
   </bean>
   
   <bean id="blForgotPasswordEmailInfo" parent="blEmailInfo">
      <property name="subject" value="Reset password request"/>
      <property name="emailTemplate" value="resetPassword-email"/>
   </bean>
   
   <bean id="blOrderConfirmationEmailInfo" parent="blEmailInfo">
      <property name="subject" value="Your order with My Company"/>
      <property name="emailTemplate" value="orderConfirmation-email"/>
   </bean>            
</beans>


I can't see anything wrong. Can anyone point out the problem?

Thank you in advance!

-Charlie

Re: Problem configuring mail service

Posted: Mon Dec 10, 2012 5:41 pm
by pokemon007
[Resolved] Don't know why, I had to let VelocityEngine use classpath resource loader. What puzzles me is that this was configured already in bl-common-applicationContext.xml in broadcommerce-common project:

Code: Select all

   <bean id="blVelocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
      <property name="velocityProperties">
         <value>
            resource.loader=class
            class.resource.loader.class=org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
            <!-- class.resource.loader.path=classpath:/config/velocity/templates/ -->
            <!-- Note that jar specification for the .path configuration property conforms to the same rules for the java.net.JarUrlConnection class-->
            <!-- jar.resource.loader.class =org.apache.velocity.runtime.resource.loader.JarResourceLoader
         jar.resource.loader.path = jar:file:/broadleaf-profile.jar/emailTemplates
         file.resource.loader.class=org.apache.velocity.runtime.resource.loader.FileResourceLoader
         file.resource.loader.cache = false
         file.resource.loader.path=${file.root}/WEB-INF/config/velocity/templates-->
         </value>
      </property>
   </bean>


Thank you!

Re: Problem configuring mail service

Posted: Mon Dec 10, 2012 10:38 pm
by phillipuniverse
Hm good to know. I know that a fair amount of people are already using Velocity for their emails and probably want to keep them as-is rather than try to convert them to Thymeleaf (which we use OOB). Thanks for posting the solution for others to benefit!

Re: Problem configuring mail service

Posted: Wed Dec 12, 2012 3:54 pm
by pokemon007
I'm still using OOTB Thymeleaf as email template resolver, but it still references VelocityEngine which by default uses resource path loader, not resource class loader. I thought the VelocityEngine defined in broadleaf should be referenced, but it's not. We have to define our own in site project such that we can specify resource loader so it can find the email templates on the class path. I believe we should add VelocityEngine configuration to demo site applicationContext-email.xml (still a puzzle to me why OOTB demo doesn't have this problem).

Thanks.

-Charlie