views:

620

answers:

2

This is the controller

class JavaMailerController {

JavaMailerService javamailerservice
def x = {javamailerservice.serviceMethod()} }

This is the Service

import javax.mail.; import javax.mail.internet.; import java.util.*;

class JavaMailerService {

boolean transactional = false

def serviceMethod() { String  d_email = "[email protected]",
        d_password = "thispassword",
        d_host = "smtp.gmail.com",
        d_port  = "587",
        m_to = "[email protected]",
        m_subject = "Testing",
        m_text = "Hey, this is the testing email.";

    Properties props = new Properties();
    props.put("mail.smtp.user", d_email);
    props.put("mail.smtp.host", d_host);
    props.put("mail.smtp.port", d_port);
    props.put("mail.smtp.starttls.enable","true");

// just in case, but not currently necessary, oddly enough props.put("mail.smtp.auth", "true"); //props.put("mail.smtp.debug", "true"); props.put("mail.smtp.socketFactory.port", d_port); props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); props.put("mail.smtp.socketFactory.fallback", "false");

    SecurityManager security = System.getSecurityManager();

    try
    {
        Authenticator auth = new SMTPAuthenticator();
        Session session = Session.getInstance(props, auth);
        //session.setDebug(true);

        MimeMessage msg = new MimeMessage(session);
        msg.setText(m_text);
        msg.setSubject(m_subject);
        msg.setFrom(new InternetAddress(d_email));
        msg.addRecipient(Message.RecipientType.TO,

new InternetAddress(m_to)); Transport.send(msg); } catch (Exception mex) { mex.printStackTrace(); } }

}

private class SMTPAuthenticator extends javax.mail.Authenticator { public PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(d_email, d_password); } }

The Error

Error 200: java.lang.NullPointerException: Cannot invoke method serviceMethod() on null object Servlet: grails URI: /JavaMailer/grails/javaMailer/x.dispatch Exception Message: Cannot invoke method serviceMethod() on null object Caused by: java.lang.NullPointerException: Cannot invoke method serviceMethod() on null object Class: Unknown At Line: [-1] Code Snippet:

+1  A: 

I think you didn't camelCase your service field in the controller.

class JavaMailerController {
   JavaMailerService javaMailerService
   def x = {
      javaMailerService.serviceMethod()
   } 
}
Chii
A: 

Thanks, it worked!!!! how can I work out without this convention?? as in

JavaMailerService javamailerservice def x = {javamailerservice.serviceMethod()} }

without camelCase?

can you please give out some details about what happens under the hood with spring in this context, i need to understand :)

tranced_UT3