AEM Email API Issue.

Avatar

Avatar
Ignite 5
Level 2
chamu0003
Level 2

Likes

0 likes

Total Posts

10 posts

Correct reply

1 solution
Top badges earned
Ignite 5
Ignite 3
Ignite 1
Validate 1
Affirm 1
View profile

Avatar
Ignite 5
Level 2
chamu0003
Level 2

Likes

0 likes

Total Posts

10 posts

Correct reply

1 solution
Top badges earned
Ignite 5
Ignite 3
Ignite 1
Validate 1
Affirm 1
View profile
chamu0003
Level 2

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

Avatar

Avatar
Validate 25
Level 10
smacdonald2008
Level 10

Likes

1,409 likes

Total Posts

12,671 posts

Correct reply

2,278 solutions
Top badges earned
Validate 25
Validate 10
Validate 1
Give back 900
Give back 600
View profile

Avatar
Validate 25
Level 10
smacdonald2008
Level 10

Likes

1,409 likes

Total Posts

12,671 posts

Correct reply

2,278 solutions
Top badges earned
Validate 25
Validate 10
Validate 1
Give back 900
Give back 600
View profile
smacdonald2008
Level 10

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. 

Avatar

Avatar
Validate 10
Level 3
mohanb
Level 3

Likes

17 likes

Total Posts

76 posts

Correct reply

1 solution
Top badges earned
Validate 10
Validate 1
Boost 5
Boost 3
Boost 10
View profile

Avatar
Validate 10
Level 3
mohanb
Level 3

Likes

17 likes

Total Posts

76 posts

Correct reply

1 solution
Top badges earned
Validate 10
Validate 1
Boost 5
Boost 3
Boost 10
View profile
mohanb
Level 3

14-11-2016

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

Avatar

Avatar
Validate 10
Level 2
veerareddyc2547
Level 2

Likes

5 likes

Total Posts

36 posts

Correct reply

1 solution
Top badges earned
Validate 10
Validate 1
Boost 5
Boost 3
Boost 1
View profile

Avatar
Validate 10
Level 2
veerareddyc2547
Level 2

Likes

5 likes

Total Posts

36 posts

Correct reply

1 solution
Top badges earned
Validate 10
Validate 1
Boost 5
Boost 3
Boost 1
View profile
veerareddyc2547
Level 2

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();
        }
    }
}

Avatar

Avatar
Coach
Employee
jbrar
Employee

Likes

389 likes

Total Posts

869 posts

Correct reply

283 solutions
Top badges earned
Coach
Establish
Give Back 50
Give Back 5
Give Back 3
View profile

Avatar
Coach
Employee
jbrar
Employee

Likes

389 likes

Total Posts

869 posts

Correct reply

283 solutions
Top badges earned
Coach
Establish
Give Back 50
Give Back 5
Give Back 3
View profile
jbrar
Employee

07-06-2019

veerareddyc25475686

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

Avatar

Avatar
Coach
MVP
Arun_Patidar
MVP

Likes

1,442 likes

Total Posts

3,314 posts

Correct reply

941 solutions
Top badges earned
Coach
Contributor 2
Ignite 10
Give Back 700
Boost 1000
View profile

Avatar
Coach
MVP
Arun_Patidar
MVP

Likes

1,442 likes

Total Posts

3,314 posts

Correct reply

941 solutions
Top badges earned
Coach
Contributor 2
Ignite 10
Give Back 700
Boost 1000
View profile
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

Avatar

Avatar
Coach
Employee
jbrar
Employee

Likes

389 likes

Total Posts

869 posts

Correct reply

283 solutions
Top badges earned
Coach
Establish
Give Back 50
Give Back 5
Give Back 3
View profile

Avatar
Coach
Employee
jbrar
Employee

Likes

389 likes

Total Posts

869 posts

Correct reply

283 solutions
Top badges earned
Coach
Establish
Give Back 50
Give Back 5
Give Back 3
View profile
jbrar
Employee

10-06-2019

Thanks arunpatidar26​. That was helpful

Avatar

Avatar
Give Back
Level 1
EugeneFur
Level 1

Likes

0 likes

Total Posts

1 post

Correct reply

0 solutions
Top badges earned
Give Back
View profile

Avatar
Give Back
Level 1
EugeneFur
Level 1

Likes

0 likes

Total Posts

1 post

Correct reply

0 solutions
Top badges earned
Give Back
View profile
EugeneFur
Level 1

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?

 

Avatar

Avatar
Coach
MVP
Arun_Patidar
MVP

Likes

1,442 likes

Total Posts

3,314 posts

Correct reply

941 solutions
Top badges earned
Coach
Contributor 2
Ignite 10
Give Back 700
Boost 1000
View profile

Avatar
Coach
MVP
Arun_Patidar
MVP

Likes

1,442 likes

Total Posts

3,314 posts

Correct reply

941 solutions
Top badges earned
Coach
Contributor 2
Ignite 10
Give Back 700
Boost 1000
View profile
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?