首页 > 解决方案 > 无法为 java 邮件会话查找 JNDI 资源。无法连接到主机,端口:localhost,25;超时-1;

问题描述

MailServlet.java

Session session = null;
        try {
            Context initCtx = new InitialContext();
            session = (Session) initCtx.lookup("java:comp/env/mail/Session");
        } catch (Exception ex) {
            System.out.println("lookup error");
            System.out.println(ex.getMessage());
            ex.printStackTrace();
        }

WEB-INF/web.xml

<web-app>

    <resource-ref>
        <description>mail Session</description>
        <res-ref-name>mail/Session</res-ref-name>
        <res-type>jakarta.mail.Session</res-type>
        <res-auth>Container</res-auth>
    </resource-ref>
......
</web-app>

META-INF/context.xml

<Context>
    <Resource name="mail/Session" auth="Container" type="jakarta.mail.Session"
    mail.transport.protocol="smtp"
    mail.smtp.auth="true"
    mail.smtp.host="smtp.gmail.com"
    mail.smtp.port="465"
    mail.smtp.user="xxxxxxx"
    password="xxxxxxxx"
    mail.from="xxxxxxx@gmail.com"
    mail.smtp.quitwait="false"
    mail.smtp.starttls.enable="true"
    mail.smtp.socketFactory.class="jakarta.net.ssl.SSLSocketFactory"
    mail.debug="true"/>
</Context>

错误:com.sun.mail.util.MailConnectException:无法连接到主机,端口:localhost,25;超时-1;

我要做的是通过tomcat创建一个邮件会话实例并从此mailID发送电子邮件。我搜索了很多tomcat how to。http://tomcat.apache.org/tomcat-7.0-doc/jndi-resources-howto.html#JavaMail_Sessions。但我找不到导致此问题的错误。

标签: javatomcatservletsjakarta-mailjndi

解决方案


推荐阅读