【问题标题】:Grails Error for using e-mail service使用电子邮件服务的 Grails 错误
【发布时间】:2010-10-03 10:25:09
【问题描述】:

这是控制器

类 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", “假”);

    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); } 捕捉(异常墨西哥) { mex.printStackTrace(); } }

}

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

错误

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

【问题讨论】:

    标签: grails groovy controller service email


    【解决方案1】:

    我认为您没有在控制器中使用驼峰式命名服务字段。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-23
      • 2014-04-09
      • 2013-11-13
      • 2023-02-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多