2

这是控制器

类 JavaMailerController {

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

这是服务

导入 javax.mail。; 导入 javax.mail.internet。; 导入 java.util.*;

类 JavaMailerService {

boolean transactional = false

def serviceMethod() { String  d_email = "thisemail@gmail.com",
        d_password = "thispassword",
        d_host = "smtp.gmail.com",
        d_port  = "587",
        m_to = "thisto@gmail.com",
        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");

// 以防万一,但目前没有必要,奇怪的是 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,

新的互联网地址(m_to));Transport.send(msg); } catch (Exception mex) { mex.printStackTrace(); } }

}

私有类 SMTPAuthenticator 扩展 javax.mail.Authenticator { public PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(d_email, d_password); } }

错误

错误 200:java.lang.NullPointerException:无法在空对象上调用方法 serviceMethod() Servlet:grails URI:/JavaMailer/grails/javaMailer/x.dispatch 异常消息:无法在空对象上调用方法 serviceMethod() 原因:java。 lang.NullPointerException:无法在空对象上调用方法 serviceMethod() 类:未知在行:[-1] 代码片段:

4

1 回答 1

2

我认为您没有在控制器中将您的服务字段骆驼化。

class JavaMailerController {
   JavaMailerService javaMailerService
   def x = {
      javaMailerService.serviceMethod()
   } 
}
于 2009-01-26T04:05:11.233 回答