javax.mail.MessaginException:无法连接到SMTP主机

javax.mail.MessaginException:无法连接到SMTP主机,java,jakarta-mail,Java,Jakarta Mail,可能重复: 我试图通过jsp页面发送电子邮件,但显示以下错误: javax.mail.MessaginException:无法连接到SMTP主机:www.gmail.com,端口:25,响应:421 这是我用来发送电子邮件的代码: <% try { String host = "www.gmail.com"; String to = request.getParameter("to"); String from = requ

可能重复:

我试图通过jsp页面发送电子邮件,但显示以下错误:

javax.mail.MessaginException
:无法连接到SMTP主机:www.gmail.com,端口:25,响应:421

这是我用来发送电子邮件的代码:

<%
    try
    {
        String host = "www.gmail.com";
        String to = request.getParameter("to");
        String from = request.getParameter("from");
        String subject = request.getParameter("subject");
        String messageText = request.getParameter("body");
        boolean sessionDebug = false;

        Properties props = System.getProperties();
        props.put("mail.host", host);
        props.put("mail.transport.protocol", "smtp");
        Session mailSession = Session.getDefaultInstance(props, null);

        mailSession.setDebug(sessionDebug);

        Message msg = new MimeMessage(mailSession);
        msg.setFrom(new InternetAddress(from));
        InternetAddress[] address = {new InternetAddress(to)};
        msg.setRecipients(Message.RecipientType.TO, address);
        msg.setSubject(subject);
        msg.setSentDate(new Date());
        msg.setText(messageText);

        Transport.send(msg);
        out.println("Mail was sent to " + to);
        out.println(" from " + from);
        out.println(" using host " + host + ".");
    }
    catch (MessagingException mex) 
    {
        System.out.println("Error: unable to send message....");
        mex.printStackTrace();
    }
%>


有人能告诉我是什么导致了这个错误吗?

你需要指向gmail邮件服务器,而不是web服务器

所以改变

String host = "www.gmail.com";


编辑:您还需要:
stringport=“587”
而不是
…port=“25”

您需要指向gmail邮件服务器,而不是web服务器

所以改变

String host = "www.gmail.com";

编辑:您还需要:
stringport=“587”而不是
…port=“25”

421:服务不可用,关闭传输通道
421:服务不可用,关闭传输通道