在 OpenShift 中访问 Context.xml 邮件资源时出现问题

Problems accessing Context.xml Mail resource in OpenShift

请帮忙, 我在 OpenShift 上有一个 Java Spring MVC 应用程序 (Tomcat 7),它试图在 Context.xml 中使用资源查找。当它尝试创建会话 session = (Session) envContext.lookup("mail/Session"); 时,我不断收到 500 个错误(请参阅下面的代码)。 app-root/logs/jbossews.log 中的日志中没有异常,e.printStackTrace(); 不会在 catch 块中打印任何内容。异常是否会记录在其他地方?我习惯于在 apache/tomcat 中使用 access.log 和 error.log,jbossews.log 看不到任何信息(仅供参考,我没有使用 RHC 工具,我只是通过 ssh 跟踪日志)。

我的项目在 eclipse 中的本地系统上运行良好,没有任何问题。 请帮忙, 谢谢

Session session = null;

Context initalContext = new InitialContext();
Context envContext = (Context) initalContext.lookup("java:comp/env");


try{
    // Blows up here with a 500 error
    session = (Session) envContext.lookup("mail/Session");
}
catch (Exception e) {
    // No StackTrace printed
    e.printStackTrace();
}

这是我位于 jbossews/conf/context 的资源。xml

  <Resource
      name="mail/Session"
      type="javax.mail.Session"
      auth="Container"
      mail.smtp.host="smtp.gmail.com"
      mail.smtp.socketFactory.port="465"
      mail.smtp.socketFactory.class="javax.net.ssl.SSLSocketFactory"
      mail.smtp.auth="true"
      mail.smtp.port="465"
      mail.from="XXXXXX@mailserver.com"
      mail.user="XXXXXX@mailserver.com"
      password="XXXXXX"
      />

我尝试将以下内容添加到我的 web.xml 中,看看是否可以解决问题(没有解决)。

<resource-ref>
    <description>Resource reference to a factory for javax.mail.Session   instances that may be used for sending electronic mail messages, preconfigured to connect to the appropriate SMTP server.</description>
    <res-ref-name>mail/Session</res-ref-name>
    <res-type>javax.mail.Session</res-type>
    <res-auth>Container</res-auth>
</resource-ref>

发现我的问题是由于我的 pom.xml 中的问题(毕竟与访问 Context.xml 无关)。恼人的是,异常没有被记录在 jbossews.log 中,所以我不得不通过 Exception exception = (Exception) httpRequest.getAttribute("javax.servlet.error.exception"); exception.printStackTrace(); ( httpRequest being a HttpServletRequest Object)

显式地记录它们

我的 pom.xml 中的问题是这样的,我尝试使用:

<dependency>
    <groupId>javax.mail</groupId>
    <artifactId>javax.mail-api</artifactId>
    <version>1.5.1</version>
    <scope>provided</scope>
</dependency>

使用以下依赖项解决了问题。

<dependency>
    <groupId>com.sun.mail</groupId>
    <artifactId>javax.mail</artifactId>
    <version>1.5.2</version>
</dependency>

注意:为了让 Context.xml "mail/Session" 资源在 OpenShift 上工作,我必须在 src/main/webapp/WEB-INF/ 中创建一个 lib 文件夹并放置 javax.mail-api.jar里面。

希望这对遇到问题的其他人有所帮助。