Grails使用电子邮件服务时出错

时间:2009-01-25 16:02:57

标签: grails groovy controller service email

这是控制器

  

class JavaMailerController {

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

这是服务

  

import javax.mail。;进口   。javax.mail.internet ;进口   java.util中。*;

     

class 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端口);           props.put( “mail.smtp.socketFactory.class”   “javax.net.ssl.SSLSocketFactory”);           props.put( “mail.smtp.socketFactory.fallback”   “假”);

    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,
     

新的InternetAddress(m_to));               Transport.send(MSG);           }           catch(例外mex)           {               mex.printStackTrace();           }       }

     

}

     

私有类SMTPAuthenticator   扩展javax.mail.Authenticator       {           public PasswordAuthentication getPasswordAuthentication()           {               返回新的PasswordAuthentication(d_email,   d_password);           }       }

错误

错误200:java.lang.NullPointerException:无法在null对象上调用方法serviceMethod() Servlet:Grails URI:/JavaMailer/grails/javaMailer/x.dispatch 异常消息:无法在null对象上调用方法serviceMethod() 引起:java.lang.NullPointerException:无法在null对象上调用方法serviceMethod() 分类:未知 在线:[ - 1] 代码段:

1 个答案:

答案 0 :(得分:2)

我认为你没有来控制你的服务领域。

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