I am trying to send an JavaMail from servlet. The code for servlet looks like this:
package com.lsp.web;
import com.lsp.service.Mailer;
import org.springframework.beans.factory.annotation.Autowired;
import javax.mail.MessagingException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet(name = "contact", urlPatterns = {"/contact"})
public class ContactServlet extends SpringInjectedServlet {
@Autowired
private Mailer mailer;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.getRequestDispatcher("contact.jsp").forward(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String customerEmail = req.getParameter("email");
String subject = req.getParameter("subject");
String body = req.getParameter("message");
String error = null;
String succMess = null;
try {
javax.mail.internet.InternetAddress ia = new javax.mail.internet.InternetAddress(customerEmail);
ia.validate();
mailer.send(customerEmail, subject, body); //Maler class send method
req.setAttribute("succMessage", succMess);
req.getRequestDispatcher("sent.jsp").forward(req, resp);
} catch (javax.mail.internet.AddressException ae) {
error = "您指出的邮箱地址不存在";
req.setAttribute("errorMessage", error);
req.getRequestDispatcher("contact.jsp").forward(req, resp);
}
catch (MessagingException mex) {
error = "发送失败";
req.setAttribute("errorMessage", error);
req.getRequestDispatcher("contact.jsp").forward(req, resp);
}
}
}
Then my Mailer class is:
package com.lsp.service;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
@Component
public class Mailer {
private static final Logger log = Logger.getLogger(Mailer.class);
public Mailer() {
log.info("I'm newed.");
}
public void send(String from, String subject, String body) {
String to = "[email protected]";
String host = "localhost";
Properties properties = System.getProperties();
properties.setProperty("mail.smtp.host", host);
Session session = Session.getDefaultInstance(properties);
try{
// Create a default MimeMessage object.
MimeMessage message = new MimeMessage(session);
// Set From: header field of the header.
message.setFrom(new InternetAddress(from));
// Set To: header field of the header.
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
// Set Subject: header field
message.setSubject(subject);
// Send the actual HTML message, as big as you like
message.setText(body);
// Send message
Transport.send(message);
System.out.println("Sent message successfully....");
}catch (MessagingException mex) {
mex.printStackTrace();
}
}
}
After hitting submit(or send) button on jsp page's submit form, I got an exception saying:
com.sun.mail.util.MailConnectException: Couldn't connect to host, port: localhost, 25; timeout -1;
nested exception is:java.net.SocketException: Network is unreachable: connect
Where did the port number 25 come from? Why am I getting this exception and how can solve it?
I appreciate if someone could help me. Thank you.
The way to solve this is:
1.) Set a smtp service so your sessions can connect to something and queue your SMTP messages. Apache James is really easy to use on Windows, just download, decompress and execute run.bat. There will be a service running on localhost:25.
2.) Using getDefaultInstance
sets some default properties, while getInstance
urges you to create a set of properties so the client can connect to out SMTP service. Check which one serves you better.
3.) Read about SMTP. If you expected your code to send messages using nothing but your client, you really need to improve your knowledge about that.