java - 无法为 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。但我找不到导致此问题的错误。
解决方案
推荐阅读
- javascript - 从节点 JavaScript 返回到 jQuery 的 Ajax 调用落在错误函数上
- c++ - c++11中如何检查类成员指针是类还是结构类型?
- java - java - 如何在按钮上获得鼠标按下事件
- angular - a2e datetimepicker - Datetimepicker 不会显示在输入焦点和单击图标上
- oracle - 如何读取csv数据并插入数据库
- python - Scapy - 从 pcap 中删除 Dot1q (801.2 Q)
- sql - 在打字稿中,如何像 MSSQL 的“order by”子句一样对 * 、 + - 等特殊字符进行排序
- powershell - 在 powershell 中为 ask-CLI 命令动态输入
- python - K-Means 在第一列是用户名(字符串)和第二列是 ID(整数)的数据集上进行 ID 聚类
- schema - 如何使用 dateModified 微数据