发送订单确认电子邮件 - Broadleaf

时间:2017-10-14 09:20:11

标签: java spring hibernate email-integration broadleaf-commerce

I went through the documentation of Broadleaf to send Email confirmation 
Below is my applicationcontext-email.xml file

<?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-4.1.xsd">

    <!-- A dummy mail sender has been set to send emails for testing purposes 
        only To view the emails sent use "DevNull SMTP" (download separately) with 
        the following setting: Port: 30000 -->


    <!-- Broadleaf step 3 -->
    <bean id="blServerInfo"
        class="org.broadleafcommerce.common.email.service.info.ServerInfo">
        <property name="serverName" value="smtp.office365.com" />
        <property name="serverPort" value="587" />
    </bean>

    <bean id="blEmailTemplateResolver"
        class="org.thymeleaf.templateresolver.ClassLoaderTemplateResolver">
        <property name="prefix" value="emailTemplates/" />
        <property name="suffix" value=".html" />
        <property name="cacheable" value="${cache.page.templates}" />
        <property name="cacheTTLMs" value="${cache.page.templates.ttl}" />
    </bean>

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

    <!-- Broadleaf email step 1 -->
    <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>mulaygaurav3@gmail.com</value>
        </property>
        <property name="sendAsyncPriority">
            <value>2</value>
        </property>
        <property name="sendEmailReliableAsync">
            <value>false</value>
        </property>
    </bean>

    <bean id="blMailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
        <property name="host">
            <value>localhost</value>
        </property>
        <property name="port">
            <value>30000</value>
        </property>
        <property name="protocol">
            <value>smtp</value>
        </property>
        <property name="username">
            <value>gaurav</value>
        </property>
        <property name="password">
            <value>mypassword</value>
        </property>
        <property name="javaMailProperties">
            <props>
                <prop key="mail.smtp.starttls.enable">true</prop>
                <prop key="mail.smtp.timeout">25000</prop>
            </props>
        </property>
    </bean>










    <bean id="blVelocityEngine"
        class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
        <property name="resourceLoaderPath" value="/WEB-INF/emailTemplates/" />
        <property name="velocityProperties">
            <value>
                resource.loader=file,class
                class.resource.loader.class=org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
                runtime.references.strict = false
            </value>
        </property>
    </bean>

    <bean id="blMessageCreator"
        class="org.broadleafcommerce.common.email.service.message.VelocityMessageCreator">
        <constructor-arg ref="blVelocityEngine" />
        <constructor-arg ref="blMailSender" />
        <constructor-arg>
            <map>
                <entry key="number">
                    <bean class="org.apache.velocity.tools.generic.NumberTool"
                        scope="prototype" />
                </entry>
                <entry key="date">
                    <bean class="org.apache.velocity.tools.generic.ComparisonDateTool"
                        scope="prototype" />
                </entry>
                <entry key="list">
                    <bean class="org.apache.velocity.tools.generic.ListTool"
                        scope="prototype" />
                </entry>
                <entry key="math">
                    <bean class="org.apache.velocity.tools.generic.MathTool"
                        scope="prototype" />
                </entry>
                <entry key="iterator">
                    <bean class="org.apache.velocity.tools.generic.IteratorTool"
                        scope="prototype" />
                </entry>
                <entry key="alternator">
                    <bean class="org.apache.velocity.tools.generic.AlternatorTool"
                        scope="prototype" />
                </entry>
                <entry key="sorter">
                    <bean class="org.apache.velocity.tools.generic.SortTool"
                        scope="prototype" />
                </entry>
                <entry key="esc">
                    <bean class="org.apache.velocity.tools.generic.EscapeTool"
                        scope="prototype" />
                </entry>
                <entry key="serverInfo" value-ref="blServerInfo" />
            </map>
        </constructor-arg>
    </bean>



    <!-- <bean id="blMessageCreator" class="org.broadleafcommerce.common.email.service.message.NullMessageCreator"> 
        <constructor-arg ref="blMailSender"/> </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>

    <!-- Broadleaf email step 2 -->
    <bean id="orderConfirmationEmailInfo" class="org.broadleafcommerce.common.email.service.info.EmailInfo" parent="blEmailInfo" scope="prototype">
    <property name="messageBody" value="This is a test email!"/>
    <property name="emailType" value="ORDERCONFIRMATION"/>
    <property name="subject" value="Thank You For Your Order!"/>
</bean>

</beans>

I have also created an activity as mentioned in the documentation:
package com.mycompany.worklow.emailactivity;

import javax.annotation.Resource;

import org.broadleafcommerce.core.checkout.service.workflow.CheckoutSeed;
import org.broadleafcommerce.core.checkout.service.workflow.CompleteOrderActivity;
import org.broadleafcommerce.core.order.domain.Order;
import org.broadleafcommerce.core.workflow.ProcessContext;

import com.mycompany.service.MyEmailWebService;

    enter code here

    public class MyCompleteOrderActivity extends CompleteOrderActivity {

        @Resource(name="myEmailService")
        protected MyEmailWebService myEmailService;

        @Override
        public ProcessContext execute(ProcessContext context) throws Exception {

            CheckoutSeed seed = (CheckoutSeed) context.getSeedData();

            Order order = seed.getOrder();
            myEmailService.sendOrderConfirmation(order.getSubmitDate(), order.getId().toString(), order.getCustomer().getEmailAddress());
            return super.execute(context);
        }
        }
}

还在applicationcontext-workflow.xml

中添加了此活动

我得到以下exxception:  无法合并源位置和补丁位置;嵌套异常是org.broadleafcommerce.common.extensibility.context.merge.exceptions.MergeException:java.lang.NullPointerException

有人可以解释一步一步的程序来实现吗? 提前致谢

1 个答案:

答案 0 :(得分:0)

你必须使用名为blMessageCreator的bean,删除其中一个。假设您使用的是使用百万美元模板的第5版演示站点,请尝试删除:

<bean id="blMessageCreator" class="org.broadleafcommerce.common.email.service.message.VelocityMessageCreator">
...
...
</bean>