【问题标题】:javax.mail.MessagingException: Could not connect to SMTP host [duplicate]javax.mail.MessagingException:无法连接到 SMTP 主机 [重复]
【发布时间】:2012-01-16 21:56:58
【问题描述】:

可能重复:
javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 25;

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

javax.mail.MessagingException:无法连接到 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();
    }
%>

谁能告诉我是什么导致了这个错误?

【问题讨论】:

标签: java jakarta-mail


【解决方案1】:

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

所以改变

String host = "www.gmail.com";

String host = "smtp.gmail.com";

编辑:您还需要:String port = "587"; 而不是 ...port = "25"

【讨论】:

    猜你喜欢
    • 2012-06-17
    • 1970-01-01
    • 2016-07-29
    • 2014-10-23
    • 2011-11-01
    • 2014-03-08
    • 2013-08-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多