Expand my Community achievements bar.

Enhance your AEM Assets & Boost Your Development: [AEM Gems | June 19, 2024] Improving the Developer Experience with New APIs and Events

Error in using custom template for mail service

Avatar

Level 2

 private Email send(ResourceResolver resolver, Map<String, String> emailParams,
            String recipientEmail) throws Exception{

        MailTemplate mailTemplate = MailTemplate.create(TEMPLATE_PATH, resolver.adaptTo(Session.class));
        
        if (mailTemplate == null) {
         throw new Exception("Template missing - " + TEMPLATE_PATH);
        }
        
        Email email = mailTemplate.getEmail(StrLookup.mapLookup(emailParams), HtmlEmail.class); // Here we are getting compilation error. as StrLookup is not available in apache commons long3
        
        email.setTo(Collections.singleton(new InternetAddress(recipientEmail)));
        email.setFrom(SENDER_EMAIL);
        
        MessageGateway<Email> messageGateway = messageGatewayService.getGateway(email.getClass());
        
        messageGateway.send(email);
        
        return email;
    }

Getting compilation error, Though by installing same into AEM, getting below error

Error Log: 

04.01.2017 07:56:59.041 *ERROR* [FelixStartLevel] fca.workflow [com.fca.core.workflow.SendEmailMaintenanceWorkflow(2177)] Error during instantiation of the implementation object (java.lang.Error: Unresolved compilation problems: 
    The type org.apache.commons.lang.text.StrLookup cannot be resolved. It is indirectly referenced from required .class files
    The import org.apache.commons.lang cannot be resolved
    StrLookup cannot be resolved
    SENDER_EMAIL cannot be resolved to a variable
)
java.lang.Error: Unresolved compilation problems: 
    The type org.apache.commons.lang.text.StrLookup cannot be resolved. It is indirectly referenced from required .class files
    The import org.apache.commons.lang cannot be resolved
    StrLookup cannot be resolved
    SENDER_EMAIL cannot be resolved to a variable

    at com.fca.core.workflow.SendEmailMaintenanceWorkflow.<init>(SendEmailMaintenanceWorkflow.java:1)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at java.lang.Class.newInstance(Unknown Source)
    at org.apache.felix.scr.impl.manager.SingleComponentManager.createImplementationObject(SingleComponentManager.java:241)
    at org.apache.felix.scr.impl.manager.SingleComponentManager.createComponent(SingleComponentManager.java:113)
    at org.apache.felix.scr.impl.manager.SingleComponentManager.getService(SingleComponentManager.java:832)
    at org.apache.felix.scr.impl.manager.SingleComponentManager.getServiceInternal(SingleComponentManager.java:799)
    at org.apache.felix.scr.impl.manager.SingleComponentManager.getService(SingleComponentManager.java:749)
    at org.apache.felix.framework.ServiceRegistrationImpl.getFactoryUnchecked(ServiceRegistrationImpl.java:343)
    at org.apache.felix.framework.ServiceRegistrationImpl.getService(ServiceRegistrationImpl.java:243)
    at org.apache.felix.framework.ServiceRegistry.getService(ServiceRegistry.java:357)
    at org.apache.felix.framework.Felix.getService(Felix.java:3671)
    at org.apache.felix.framework.BundleContextImpl.getService(BundleContextImpl.java:470)
    at org.apache.felix.scr.impl.manager.SingleRefPair.getServiceObject(SingleRefPair.java:72)
    at org.apache.felix.scr.impl.helper.BindMethod.getServiceObject(BindMethod.java:576)
    at org.apache.felix.scr.impl.manager.DependencyManager.getServiceObject(DependencyManager.java:2037)
    at org.apache.felix.scr.impl.manager.DependencyManager.doInvokeBindMethod(DependencyManager.java:1601)
    at org.apache.felix.scr.impl.manager.DependencyManager.invokeBindMethod(DependencyManager.java:1586)
    at org.apache.felix.scr.impl.manager.SingleComponentManager.invokeBindMethod(SingleComponentManager.java:370)
    at org.apache.felix.scr.impl.manager.DependencyManager$MultipleDynamicCustomizer.addedService(DependencyManager.java:327)
    at org.apache.felix.scr.impl.manager.DependencyManager$MultipleDynamicCustomizer.addedService(DependencyManager.java:303)
    at org.apache.felix.scr.impl.manager.ServiceTracker$Tracked.customizerAdded(ServiceTracker.java:1492)
    at org.apache.felix.scr.impl.manager.ServiceTracker$Tracked.customizerAdded(ServiceTracker.java:1413)
    at org.apache.felix.scr.impl.manager.ServiceTracker$AbstractTracked.trackAdding(ServiceTracker.java:1222)
    at org.apache.felix.scr.impl.manager.ServiceTracker$AbstractTracked.track(ServiceTracker.java:1158)
    at org.apache.felix.scr.impl.manager.ServiceTracker$Tracked.serviceChanged(ServiceTracker.java:1444)
    at org.apache.felix.framework.util.EventDispatcher.invokeServiceListenerCallback(EventDispatcher.java:987)
    at org.apache.felix.framework.util.EventDispatcher.fireEventImmediately(EventDispatcher.java:838)
    at org.apache.felix.framework.util.EventDispatcher.fireServiceEvent(EventDispatcher.java:545)
    at org.apache.felix.framework.Felix.fireServiceEvent(Felix.java:4547)
    at org.apache.felix.framework.Felix.registerService(Felix.java:3521)
    at org.apache.felix.framework.BundleContextImpl.registerService(BundleContextImpl.java:348)
    at org.apache.felix.scr.impl.manager.AbstractComponentManager$3.register(AbstractComponentManager.java:869)
    at org.apache.felix.scr.impl.manager.AbstractComponentManager$3.register(AbstractComponentManager.java:857)
    at org.apache.felix.scr.impl.manager.RegistrationManager.changeRegistration(RegistrationManager.java:133)
    at org.apache.felix.scr.impl.manager.AbstractComponentManager.registerService(AbstractComponentManager.java:915)
    at org.apache.felix.scr.impl.manager.AbstractComponentManager.activateInternal(AbstractComponentManager.java:715)
    at org.apache.felix.scr.impl.manager.AbstractComponentManager.enable(AbstractComponentManager.java:399)
    at org.apache.felix.scr.impl.config.ConfigurableComponentHolder.enableComponents(ConfigurableComponentHolder.java:669)
    at org.apache.felix.scr.impl.BundleComponentActivator.initialEnable(BundleComponentActivator.java:184)
    at org.apache.felix.scr.impl.Activator.loadComponents(Activator.java:332)
    at org.apache.felix.scr.impl.Activator.access$000(Activator.java:49)
    at org.apache.felix.scr.impl.Activator$ScrExtension.start(Activator.java:257)
    at org.apache.felix.utils.extender.AbstractExtender.createExtension(AbstractExtender.java:259)
    at org.apache.felix.utils.extender.AbstractExtender.modifiedBundle(AbstractExtender.java:232)
    at org.osgi.util.tracker.BundleTracker$Tracked.customizerModified(BundleTracker.java:482)
    at org.osgi.util.tracker.BundleTracker$Tracked.customizerModified(BundleTracker.java:415)
    at org.osgi.util.tracker.AbstractTracked.track(AbstractTracked.java:232)
    at org.osgi.util.tracker.BundleTracker$Tracked.bundleChanged(BundleTracker.java:444)
    at org.apache.felix.framework.util.EventDispatcher.invokeBundleListenerCallback(EventDispatcher.java:913)
    at org.apache.felix.framework.util.EventDispatcher.fireEventImmediately(EventDispatcher.java:834)
    at org.apache.felix.framework.util.EventDispatcher.fireBundleEvent(EventDispatcher.java:516)
    at org.apache.felix.framework.Felix.fireBundleEvent(Felix.java:4531)
    at org.apache.felix.framework.Felix.startBundle(Felix.java:2169)
    at org.apache.felix.framework.Felix.setActiveStartLevel(Felix.java:1368)
    at org.apache.felix.framework.FrameworkStartLevelImpl.run(FrameworkStartLevelImpl.java:308)
    at java.lang.Thread.run(Unknown Source)
04.01.2017 07:56:59.041 *ERROR* [FelixStartLevel] fca.workflow [com.fca.core.workflow.SendEmailMaintenanceWorkflow(2177)] Failed creating the component instance; see log for reason 

5 Replies

Avatar

Employee

Can you check on your instance if this package is available.

You can do this via: /system/console/depfinder

Avatar

Level 2

Issue here is:  Email email = mailTemplate.getEmail(StrLookup.mapLookup(emailParams), HtmlEmail.class); 

Requires a class StrLookup of apache commons long 2.X version. but AEM6.1 supoorts apache commons long3. 

So its not able to find apache commons Long 2.x.

 

Is there a way add apache commons Long 2.x to osgi

Avatar

Employee

I would first see it can't rewrite your code to use the versions provided by AEM.

Avatar

Level 10

To learn what dependencies you need to work with custom templates - as well as the required Java logic -- see  Creating Adobe Experience Manager HTL Components that send Email messages based on templates