无法查找java邮件会话的jndi资源无法连接到主机,端口:localhost,25;超时-1;

gudnpqoy  于 2021-10-10  发布在  Java
关注(0)|答案(0)|浏览(352)

mailservlet.java

  1. Session session = null;
  2. try {
  3. Context initCtx = new InitialContext();
  4. session = (Session) initCtx.lookup("java:comp/env/mail/Session");
  5. } catch (Exception ex) {
  6. System.out.println("lookup error");
  7. System.out.println(ex.getMessage());
  8. ex.printStackTrace();
  9. }

web inf/web.xml

  1. <web-app>
  2. <resource-ref>
  3. <description>mail Session</description>
  4. <res-ref-name>mail/Session</res-ref-name>
  5. <res-type>jakarta.mail.Session</res-type>
  6. <res-auth>Container</res-auth>
  7. </resource-ref>
  8. ......
  9. </web-app>

meta inf/context.xml

  1. <Context>
  2. <Resource name="mail/Session" auth="Container" type="jakarta.mail.Session"
  3. mail.transport.protocol="smtp"
  4. mail.smtp.auth="true"
  5. mail.smtp.host="smtp.gmail.com"
  6. mail.smtp.port="465"
  7. mail.smtp.user="xxxxxxx"
  8. password="xxxxxxxx"
  9. mail.from="xxxxxxx@gmail.com"
  10. mail.smtp.quitwait="false"
  11. mail.smtp.starttls.enable="true"
  12. mail.smtp.socketFactory.class="jakarta.net.ssl.SSLSocketFactory"
  13. mail.debug="true"/>
  14. </Context>

错误:com.sun.mail.util.mailconnectexception:无法连接到主机,端口:localhost,25;超时-1;
我试图做的是通过tomcat创建一个邮件会话示例,并从这个mailid发送电子邮件。我搜索了很多tomcat的how to's。http://tomcat.apache.org/tomcat-7.0-doc/jndi-resources-howto.html#javamail_sessions. 但我找不到导致这种情况的错误。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题