【发布时间】: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