Quantcast

Spring mail context error with XML configuration

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Spring mail context error with XML configuration

ckulathunga
This post was updated on .
Hi. I'm trying to configure spring mail with thymeleaf (2.1.4) template engine using xml configuration. and I'm having below error.

 INFO  | WorkManager.losWorkManager : 32 |  | TemplateEngine | [THYMELEAF] INITIALIZING TEMPLATE ENGINE
 INFO  | WorkManager.losWorkManager : 32 |  | AbstractTemplateResolver | [THYMELEAF] INITIALIZING TEMPLATE RESOLVER: org.thymeleaf.templateresolver.ServletContextTemplateResolver
 INFO  | WorkManager.losWorkManager : 32 |  | AbstractTemplateResolver | [THYMELEAF] TEMPLATE RESOLVER INITIALIZED OK
 INFO  | WorkManager.losWorkManager : 32 |  | AbstractTemplateResolver | [THYMELEAF] INITIALIZING TEMPLATE RESOLVER: org.thymeleaf.templateresolver.ClassLoaderTemplateResolver
 INFO  | WorkManager.losWorkManager : 32 |  | AbstractTemplateResolver | [THYMELEAF] TEMPLATE RESOLVER INITIALIZED OK
 INFO  | WorkManager.losWorkManager : 32 |  | AbstractMessageResolver | [THYMELEAF] INITIALIZING MESSAGE RESOLVER: org.thymeleaf.spring4.messageresolver.SpringMessageResolver
 INFO  | WorkManager.losWorkManager : 32 |  | AbstractMessageResolver | [THYMELEAF] MESSAGE RESOLVER INITIALIZED OK
 INFO  | WorkManager.losWorkManager : 32 |  | CONFIG | [THYMELEAF] TEMPLATE ENGINE CONFIGURATION:
[THYMELEAF] * Cache Factory implementation: [no caches]
[THYMELEAF] * Template modes:
[THYMELEAF]     * HTML5
[THYMELEAF]     * XHTML
[THYMELEAF]     * VALIDXHTML
[THYMELEAF]     * LEGACYHTML5
[THYMELEAF]     * VALIDXML
[THYMELEAF]     * XML
[THYMELEAF] * Template resolvers (in order):
[THYMELEAF]     * [1] org.thymeleaf.templateresolver.ServletContextTemplateResolver
[THYMELEAF]     * [2] org.thymeleaf.templateresolver.ClassLoaderTemplateResolver
[THYMELEAF] * Message resolvers (in order):
[THYMELEAF]     * org.thymeleaf.spring4.messageresolver.SpringMessageResolver
[THYMELEAF] * Dialect: org.thymeleaf.spring4.dialect.SpringStandardDialect
[THYMELEAF]     * Prefix: "th"
[THYMELEAF] TEMPLATE ENGINE CONFIGURED OK
 INFO  | WorkManager.losWorkManager : 32 |  | TemplateEngine | [THYMELEAF] TEMPLATE ENGINE INITIALIZED
 ERROR | WorkManager.losWorkManager : 32 |  | TemplateEngine | [THYMELEAF][WorkManager.losWorkManager : 32] Exception processing template "secure-email-template": Resource resolution by ServletContext with org.thymeleaf.resourceresolver.ServletContextResourceResolver can only be performed when context implements org.thymeleaf.context.IWebContext [current context: org.thymeleaf.context.Context]
org.thymeleaf.exceptions.TemplateProcessingException: Resource resolution by ServletContext with org.thymeleaf.resourceresolver.ServletContextResourceResolver can only be performed when context implements org.thymeleaf.context.IWebContext [current context: org.thymeleaf.context.Context]
        at org.thymeleaf.resourceresolver.ServletContextResourceResolver.getResourceAsStream(ServletContextResourceResolver.java:78)

And this is my xml configuration

<bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
                <property name="templateEngine" ref="templateEngine" />
                <property name="order" value="1" />
                <property name="characterEncoding" value="UTF-8" />
        </bean>
       
       
        <bean id="emailTemplateResolver" class="org.thymeleaf.templateresolver.ClassLoaderTemplateResolver">
                 <property name="prefix" value="/mvcviews/templates/mail" />
               
            <property name="suffix" value=".html" />
                <property name="templateMode" value="HTML5"/>
           <property name="cacheable" value="false" />
           <property name="order" value="2"/>
        </bean>

        <bean id="templateResolver"
                class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
                <property name="prefix" value="/mvcviews/templates/" />
                <property name="suffix" value=".html" />
                <property name="templateMode" value="HTML5" />
               
           <property name="cacheable" value="false" />
           <property name="order" value="1"/>
        </bean>
      <bean id="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine">
                <property name="templateResolvers">
                   <set>
                       <ref bean="templateResolver"/>
                       <ref bean="emailTemplateResolver"/>
                   </set>
                </property>
                <property name="messageSource" ref="messageSource" />
        </bean>


And this is the my java implementation. I cannot use the annotation configuration because this is legacy code.


        @Override
        public void sendSecureMail(JavaMailSender mailSender, MailMessageDto mailMessageDto) {
                try {
                        logger.info("Starting to send the secure email to the sender....");
                        final SpringTemplateEngine templateEngine = (SpringTemplateEngine)SpringBeanFactory.getBean(SpringTemplateEngine.class);
                        final MimeMessage message = mailSender.createMimeMessage();
                        final MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8"); // true = multipart
                        final String htmlContent;
                final Context ctx = new Context(Locale.US);
               
                ctx.setVariable("name", "Hello User XYZ");
                ctx.setVariable("subscriptionDate", new Date());
                ctx.setVariable("hobbies", Arrays.asList("Cinema", "Sports", "Music"));
               
                        helper.setFrom(mailMessageDto.getFrom());
                        helper.setTo(mailMessageDto.getTo());
                        helper.setSubject(mailMessageDto.getSubject());
                        System.out.println("templateEngine: "+templateEngine);
                        htmlContent = templateEngine.process("secure-email-template", ctx);
                        helper.setText(htmlContent, true); // true = isHtml
                        mailSender.send(message);
                       
                } catch (Exception e) {
                        logger.error("Error occured while sending the secure email..", e);
                }
        }

And my secure-email-template.html,

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
    <head>
        <title data-th-remove="all">Template for HTML email (editable)</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    </head>
    <body>
        <div style="background-color: #ccc; background-image: url(images/background.png)"
             data-th-style="|background-color: #ccc; background-image: url(cid:background)|">
            <div style="padding: 40px;">
                <div style="margin-left: auto; margin-right: auto; min-width: 320px; border: solid 1px #555; border-radius: 6px;">
                    <div style="background-color: #555; padding: 4px 4px 2px 4px;">
                        <div style="background-image: url(images/logo-background.png);"
                             data-th-style="|background-image: url(cid:logo-background)|">
                           
                        </div>
                    </div>
                    <div style="padding: 15px 15px 30px 15px; font-family: Verdana, Arial, Helvetica, sans-serif; font-size:1em; background-color: #fff; color:#333;">
                        <p>Hello Peter! (this is a placeholder for dynamic recipient name)</p>
                        <p>
                            You have been successfully subscribed to the Fake newsletter on
                            28-12-2012
                        </p>
                        <p>Your hobbies are:</p>
                        <ul data-th-remove="all-but-first">
                            <li data-th-each="hobby : ${hobbies}"
                                data-th-text="${hobby}">Reading</li>
                            <li>Writing</li>
                            <li>Bowling</li>
                        </ul>
                        <p>
                            Regards, <br />
                              <em>The Thymeleaf Team</em>
                        </p>
                    </div>
                    <div style="background-color: #555; padding: 4px 4px 0px 4px; text-align: right;">
                        <div style="background-image: url(images/logo-background.png);"
                             data-th-style="|background-image: url(cid:logo-background)|">
                           
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </body>
</html>


I'm able to load the pages which are templateResolver implementation . but Its not working for the emailTemplateResolver. Also I need to do with the Context not with the WebContext because I don't have the request and response objects on the implementation location.

Any help would be appreciate.
Thanks

PS: Folder Structure

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Spring mail context error with XML configuration

Metroids
I think it's because you're trying to resolve email templates from the same directory that you resolve regular templates from.  In my experience, normal templates are resolved in the WEB-INF/templates/*files* directory, but email templates have to be on the classpath somewhere (I have them in src/resources/email/*files*).

That being said, I don't really know if you're build packages those files up in a WEB-INF, because you're project structure looks a little different than I'd expect.

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Spring mail context error with XML configuration

ckulathunga
Thanks for the reply @Metroids. That's correct it needs to be the class path to that files. After below change I was able to resolve the issue.


<bean id="emailTemplateResolver" class="org.thymeleaf.templateresolver.ClassLoaderTemplateResolver">
        <property name="prefix" value="com/link/interfaces/business/secure/mail/resources/" />
        <property name="suffix" value=".html" />
        <property name="templateMode" value="HTML5"/>
        <property name="cacheable" value="false" />
        <property name="order" value="2"/>
</bean>

Thanks for the help.
Loading...