发送JavaMail时发生MailConnectException

时间:2014-01-20 01:39:37

标签: java jsp maven servlets javamail

我正在尝试从servlet发送JavaMail。 servlet的代码如下所示:

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);
        }
    }
}

然后我的Mailer课程是:

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 = "jshao745@gmail.com";
        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();
        }
    }
}

点击jsp页面提交表单上的提交(或发送)按钮后,我得到一个例外说法:

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

端口号25来自哪里?为什么我得到这个例外以及如何解决它?

如果有人能帮助我,我感激不尽。 谢谢。

1 个答案:

答案 0 :(得分:0)

解决这个问题的方法是:

1。)设置smtp服务,以便您的会话可以连接到某些内容并对您的SMTP邮件进行排队。 Apache James在Windows上非常容易使用,只需下载,解压缩并执行run.bat即可。将在localhost上运行一项服务:25。

2.。)使用getDefaultInstance设置一些默认属性,而getInstance则敦促您创建一组属性,以便客户端可以连接到SMTP服务。检查哪一个更适合你。

3.)阅读有关SMTP的信息。如果您希望您的代码只使用您的客户端发送消息,那么您确实需要提高您对此的了解。