Highlighted

AEM Email API Issue.

Avatar

Avatar

chamu0003

Avatar

chamu0003

chamu0003

14-11-2016

Hi ,

Can anyone please help me what's wrong in my email sending  code.

Node templateNode = adminSession.getNode("/etc/workflow/notification/email/demo/choosesite.txt"); log.info("admin session:: "+adminSession+".."+templateNode); final Map<String, String> parameters = new HashMap<String, String>(); parameters.put("title", "page activation"); parameters.put("name", "chamu"); parameters.put("id", "chamu@gmail.com"); parameters.put("prefix", "localhost"); parameters.put("path", "/content/demo/test.html"); */ final MailTemplate mailTemplate = MailTemplate.create(templateNode.getPath(),templateNode.getSession()); HtmlEmail  email = mailTemplate.getEmail(StrLookup.mapLookup(parameters),HtmlEmail.class); email.addTo("chamu0001@gmail.com"); //email.addTo(new ArrayList<>()); MessageGateway<HtmlEmail> messageGateway = this.messageGatewayService.getGateway(HtmlEmail.class); messageGateway.send(email);

it's always throwing below error .

Caused by: java.lang.LinkageError: loader constraint violation: when resolving method "com.day.cq.commons.mail.MailTemplate.getEmail(Lorg/apache/commons/lang/text/StrLookup;Ljava/lang/Class;)Lorg/apache/commons/mail/Email;" the class loader (instance of org/apache/felix/framework/BundleWiringImpl$BundleClassLoaderJava5) of the current class, com/demo/aem/workflow/exec/RBSpawnSubWorkflowsProcess, and the class loader (instance of org/apache/felix/framework/BundleWiringImpl$BundleClassLoaderJava5) for resolved class, com/day/cq/commons/mail/MailTemplate, have different Class objects for the type s/lang/text/StrLookup;Ljava/lang/Class;)Lorg/apache/commons/mail/Email; used in the signature

Replies

Highlighted

Avatar

Avatar

smacdonald2008

Total Posts

12.7K

Likes

1.4K

Correct Answer

2.3K

Avatar

smacdonald2008

Total Posts

12.7K

Likes

1.4K

Correct Answer

2.3K
smacdonald2008

14-11-2016

Are you following an online doc? If so - can you reference it so community can see if that is reproducible.

We have a MessageGateWay article - but that is part of a custom workflow step.

http://helpx.adobe.com/experience-manager/using/creating-custom-aem-workflow-steps.html

It does not use the email template as is shown here. 

Highlighted

Avatar

Avatar

mohanb

Avatar

mohanb

mohanb

14-11-2016

try restarting bundles, i had faced issues and it was due to resolution dependency issues. restarting components/ respective bundles resolved issue.

Highlighted

Avatar

Avatar

veerareddyc2547

Avatar

veerareddyc2547

veerareddyc2547

14-11-2016

Hi,

You can see the below servlet code. we are hitting the below servlet through action="/bin/hello" and from that servlet we are sending the email ,we are able to get the email also.

Maven dependency:

<dependency>
            <groupId>javax.mail</groupId>
            <artifactId>javax.mail-api</artifactId>
            <version>1.5.1</version>
        </dependency>

and if your system pointing  port no 465 of smtp.gmail.com ,use this code 

props.put("mail.smtp.auth", "true");
            props.put("mail.smtp.ssl.enable", "true");
        //    props.put("mail.smtp.starttls.enable", "true");
            props.put("mail.smtp.host", "smtp.gmail.com");
            props.put("mail.smtp.port", "465");

or else   if your system pointing  port no 587 of smtp.gmail.com ,use this code

props.put("mail.smtp.auth", "true");
            props.put("mail.smtp.starttls.enable", "true");
            props.put("mail.smtp.host", "smtp.gmail.com");
            props.put("mail.smtp.port", "587");

 

 

servlet code :

@SlingServlet(paths = "/bin/hello", methods = "{GET, POST}")

public class MailTest extends SlingAllMethodsServlet {

    private static final long serialVersionUID = 1L;

    @Override
    protected void doPost(SlingHttpServletRequest request,
            SlingHttpServletResponse response) throws ServletException,
            IOException {
        try {
            final String from = "aemwebsiteenquiryform@gmail.com";
            final String password = "123";

            String email = request.getParameter("email");
            String query = request.getParameter("query");
            
            
            System.out.println(email + "--" + query);

            Properties props = new Properties();
            props.put("mail.smtp.auth", "true");
            props.put("mail.smtp.starttls.enable", "true");
            props.put("mail.smtp.host", "smtp.gmail.com");
            props.put("mail.smtp.port", "587");

            Session session = Session.getInstance(props,
                    new javax.mail.Authenticator() {
                        protected PasswordAuthentication getPasswordAuthentication() {
                            return new PasswordAuthentication(from, password);
                        }
                    });

            Message nameOfStationMessages = new MimeMessage(session);
            nameOfStationMessages.setFrom(new InternetAddress(from));
            nameOfStationMessages.setRecipients(Message.RecipientType.TO,
                    InternetAddress.parse(email));
            nameOfStationMessages.setText("Suggestion/Query: " + query);
            Transport.send(nameOfStationMessages);
           System.out.println("hello");
            response.sendRedirect("/content/transport/fr.html");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Highlighted

Avatar

Avatar

jbrar

Employee

Avatar

jbrar

Employee

jbrar
Employee

07-06-2019

veerareddyc25475686

I am facing the same issue, Can you share the solution for this error?

Highlighted

Avatar

Avatar

Arun_Patidar

MVP

Total Posts

2.9K

Likes

1.0K

Correct Answer

831

Avatar

Arun_Patidar

MVP

Total Posts

2.9K

Likes

1.0K

Correct Answer

831
Arun_Patidar
MVP

09-06-2019

JaideepBrar

I tried in 6.4 with same code shared in original post,It working for me without any issue. below is the code :

aem63app-repo/SimpleEmailServlet.java at master · arunpatidar02/aem63app-repo · GitHub

Highlighted

Avatar

Avatar

jbrar

Employee

Avatar

jbrar

Employee

jbrar
Employee

10-06-2019

Thanks arunpatidar26​. That was helpful

Highlighted

Avatar

Avatar

EugeneFur

Avatar

EugeneFur

EugeneFur

14-01-2020

Hi @Arun_Patidar,

 

I've tried your code sample in my local AEM 6.4 instance. Unfortunately I'm getting an error in that line: 

HtmlEmail email = mailTemplate.getEmail(StrLookup.mapLookup(parameters), HtmlEmail.class);

 

Here's the excerpt from the error stack trace

org.apache.commons.mail.EmailException: Invalid message supplied

                at org.apache.commons.mail.HtmlEmail.setMsg(HtmlEmail.java:177) [org.apache.commons.email:1.5.0]

               at com.day.cq.commons.mail.MailTemplate.getEmail(MailTemplate.java:179) [com.day.cq.cq-commons:5.11.18]

 

 

Any idea what could cause that error?

Should it work in local in first place?

 

Highlighted

Avatar

Avatar

Arun_Patidar

MVP

Total Posts

2.9K

Likes

1.0K

Correct Answer

831

Avatar

Arun_Patidar

MVP

Total Posts

2.9K

Likes

1.0K

Correct Answer

831
Arun_Patidar
MVP

15-01-2020

Hi,

It does work in 6.4

https://aemlab.blogspot.com/2019/06/aem-custom-templated-email.html

 

Could you please check email template?