PDA

View Full Version : ** send mail wiht spring **


vljc2004
Sep 12th, 2007, 08:12 PM
Hi, im try send mail with spring but show this:

org.springframework.web.util.NestedServletExceptio n: Request processing failed; nested exception is java.lang.NullPointerException


I create the next class:


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

public class Mail {
private String to;
private String from;
private String content;

public String SendMail(String to,String from,String content) throws AddressException, MessagingException{
Properties correo= new Properties();
correo.setProperty("mail.smtp.host","smtp.gmail.com");
correo.setProperty("mail.smtp.starttls.enbale", "true");
correo.setProperty("mail.smtp.port", "587");
correo.setProperty("mail.smtp.user", "hi@gmail.com");
correo.setProperty("mail.smtp.auth", "true");
Session sesion=Session.getDefaultInstance(correo);
sesion.setDebug(true);

MimeMessage mensaje= new MimeMessage(sesion);
mensaje.setFrom(new InternetAddress(from));
mensaje.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
mensaje.setSubject("Hi");
mensaje.setText("Mensajito con Java Mail<br>" + "<b>de</b> los <i>buenos</i>." + "poque si","html");

Transport t= sesion.getTransport("smtp");
t.connect(null,"hi@gmail.com","1234");
t.sendMessage(mensaje,mensaje.getAllRecipients());
t.close();

return null;
}

}


And SendMailController this:


public class SendMailController implements Controller {

private Mail mail;
public ModelAndView handleRequest(HttpServletRequest resp,
HttpServletResponse arg1) throws Exception {
// TODO Auto-generated method stub

String from=resp.getParameter("from");
String to=resp.getParameter("to");
String content=resp.getParameter("content");
mail.SendMail(to, from, content);
Map date= new HashMap();
dates.put("from_k", from);
date.put("to_k", to);
date.put("content_k", content);

return new ModelAndView("test.jsp","d",date);
}

}

:confused::confused::confused:

when it enters line " mail.SendMail(to, from, content); " break and show error.

On my application not this mapping the class Mail.

Thanks!.

Jörg Heinicke
Sep 13th, 2007, 12:43 AM
Where is the Mail instance supposed to come from? You should also not have instance fields in your controller. They are supposed to be thread-safe and you break that.

Joerg

lumpynose
Sep 13th, 2007, 01:44 AM
Here's an example using Apache Commons Mail:
package jmemento.service.impl;

import jmemento.dao.db.api.IUnverifiedDao;
import jmemento.domain.user.api.IUserMeta;
import jmemento.service.api.IVerifyEmailService;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;

public final class VerifyEmailService implements IVerifyEmailService {
private final Log log = LogFactory.getLog(getClass());

private final IUnverifiedDao unverifiedDao;

public VerifyEmailService(final IUnverifiedDao _unverifiedDao) {
if (_unverifiedDao == null)
throw (new IllegalArgumentException("unverifiedDao can't be null"));

unverifiedDao = _unverifiedDao;
}

public void sendEmail(final IUserMeta userMeta) {
if (userMeta == null) {
log.error("userMeta is null");

return;
}

final String emailId = unverifiedDao.getEmailId(userMeta.getId());

log
.debug(String
.format("userMeta: %s, emailId: %s", userMeta, emailId));

final SimpleEmail email = new SimpleEmail();

email.setSubject("email address verification");

email.setHostName("smtp.aol.com");
email.setSmtpPort(587);

email.setAuthentication("jmementotest@aol.com", "????????");

final StringBuffer sb = new StringBuffer();

sb.append("This is a test of VerifyEmailService.\n");
sb.append("Your emailId is: " + emailId + "\n");

try {
email.addTo(userMeta.getEmail());
email.setFrom("jmementotest@aol.com");
email.setMsg(sb.toString());

email.send();
}
catch (final EmailException e) {
log.error("email exception", e);

return;
}

log.debug("email sent");
}
}