Expand my Community achievements bar.

Regarding Mail issue in publish instance

Avatar

Level 3

Hi All,
 
We have created a custom mail service. the code is working fine in author instance but it is not working in publish instance .can any one please suggest me to solve this problem and what configuration should i do in publish instace.

here is my code.

form component:
---------------
<%@include file="/libs/foundation/global.jsp"%>

<script>
 
$(document).ready(function() {
   
    $('body').hide().fadeIn(5000);
          
$('#submit').click(function() {
    var failure = function(err) {
             alert("Unable to retrive data "+err);
   };
   var name = $("#user_name").val();
var email = $("#email").val();
 
    //Use JQuery AJAX to perform a GET to the AEM Sling Servlet that uses Sling Models
    $.ajax({
         type: 'GET',    
         url:'/bin/slingmodel',
        data:"name=" + name+"&email="+email,
         success: function(msg){
             $('#json').val(msg);   
         }
     });
  });

}); // end ready
</script>


<form method='post' action="javascript:mail();" >
<input type="text" class="input-large" id="user_name" name="name">
<input type="text" class="input-large" id="email" name="email">
<button type="submit" class="btn btn-primary" id="submit">Submit</button>
</form>


and java code to send mail:
---------------------------
package com.rajasthan.itportal.sightly;

import java.io.IOException;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.ServletException;

import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.sling.SlingServlet;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
 
 
 
@SlingServlet(paths="/bin/slingmodel", methods="GET")
public class SlingModels extends SlingAllMethodsServlet{
 
    private static final long serialVersionUID = 1L;
    Logger logger = LoggerFactory.getLogger(this.getClass());
    @Reference
    ResourceResolverFactory resourceResolverFactory;    
    ResourceResolver resourceResolver;
    public void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response)throws ServletException,IOException{
        logger.info("inside sling model test servlet");
        response.setContentType("text/html");
        try {
            final String from = "aemwebsiteenquiryform@gmail.com";
            final String password = "Robotics@123";

            String name = request.getParameter("name");
            String email = request.getParameter("email");
            System.out.println(name);
            System.out.println(email);
            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");
            System.out.println("2");
            Session session = Session.getInstance(props,
                    new javax.mail.Authenticator() {
                        protected PasswordAuthentication getPasswordAuthentication() {
                            return new PasswordAuthentication(from, password);
                        }
                    });
            System.out.println("3");
            Message nameOfStationMessages = new MimeMessage(session);
            
            nameOfStationMessages.setFrom(new InternetAddress(from));
            System.out.println("5");
            nameOfStationMessages.setRecipients(Message.RecipientType.TO,
                    InternetAddress.parse(email));
            nameOfStationMessages.setText("Suggestion/Query: " + name);
            System.out.println("6");
            
            
            System.out.println("7");
            Transport.send(nameOfStationMessages);
            
            System.out.println("hit");
            
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        finally{
            if(resourceResolver.isLive())
                resourceResolver.close();
        }
         
     
    }
 
}

3 Replies

Avatar

Level 9

Hi,

Not pretty sure, but one thing that you can check is the path provided for the servlet '/bin/slingmodel'. 

May be try with /content/slingmodel or some path, because if the /bin etc location is whitelisted, could contribute to the issue.

Avatar

Administrator

Is there some error coming up in publish instance ?

I hope everything is replicated to publish instance (OSGI service, Component, JSP,  JS etc.)

~kautuk



Kautuk Sahni

Avatar

Administrator

Check on your publish on localhost:4503/sytem/console/requests the trace of the request, which does not work.

Also Check in crx-quickstart/logs/error.log

Reference Link:- https://forums.adobe.com/message/4263826#4263826

~kautuk



Kautuk Sahni