Expand my Community achievements bar.

AEM Email API Issue.

Avatar

Level 2

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

8 Replies

Avatar

Level 10

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

Level 4

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

Avatar

Level 3

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

Employee Advisor

veerareddyc25475686

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

Avatar

Community Advisor

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



Arun Patidar

Avatar

Level 1

Hi @arunpatidar,

 

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?