legacy-knowledge-base
公開されました Sep. 10, 2025

java.lang.ClassCastException: class javax.mail.Session cannot be cast to class javax.mail.Session error when configuring email delivery using JNDI

written-by

Jorge Diaz

How To articles are not official guidelines or officially supported documentation. They are community-contributed content and may not always reflect the latest updates to Liferay DXP. We welcome your feedback to improve How To articles!

While we make every effort to ensure this Knowledge Base is accurate, it may not always reflect the most recent updates or official guidelines.We appreciate your understanding and encourage you to reach out with any feedback or concerns.

legacy-article

learn-legacy-article-disclaimer-text

問題

portal-ext.propertiesファイルに以下のプロパティを追加し、アプリケーションサーバーのJNDIメールリソースを使用したメール配信を設定しました。

mail.session.jndi.name=mail/Session 

このLiferayのドキュメントにしたがって設定しました:

電子メールを送信しようとすると、次のような例外が発生します:

2023-03-14 16:03:14.394 ERROR [liferay/mail-2][InfrastructureUtil:90] Unable to lookup mail/Session
java.lang.ClassCastException: class javax.mail.Session cannot be cast to class javax.mail.Session (javax.mail.Session is in unnamed module of loader 'javax.mail.api@1.6.5.redhat-00001' @35cb51f9; javax.mail.Session is in unnamed module of loader com.liferay.shielded.container.internal.ShieldedContainerClassLoader @345def0f)
at com.liferay.portal.kernel.util.InfrastructureUtil._createMailSession(InfrastructureUtil.java:87) [portal-kernel.jar:?]
at com.liferay.portal.kernel.util.InfrastructureUtil.getMailSession(InfrastructureUtil.java:51) [portal-kernel.jar:?]
at com.liferay.mail.service.impl.MailServiceImpl.getSession(MailServiceImpl.java:193) [portal-impl.jar:?]
at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:?]
at jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:?]
at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:?]
at java.lang.reflect.Method.invoke(Method.java:566) ~[?:?]
at com.liferay.portal.spring.aop.AopMethodInvocationImpl.proceed(AopMethodInvocationImpl.java:50) [portal-impl.jar:?]
at com.liferay.portal.spring.transaction.TransactionInterceptor.invoke(TransactionInterceptor.java:69) [portal-impl.jar:?]
at com.liferay.portal.spring.aop.AopMethodInvocationImpl.proceed(AopMethodInvocationImpl.java:57) [portal-impl.jar:?]
at com.liferay.change.tracking.internal.aop.CTTransactionAdvice.invoke(CTTransactionAdvice.java:79) [bundleFile:?]
at com.liferay.portal.spring.aop.AopMethodInvocationImpl.proceed(AopMethodInvocationImpl.java:57) [portal-impl.jar:?]
at com.liferay.portal.spring.aop.AopInvocationHandler.invoke(AopInvocationHandler.java:49) [portal-impl.jar:?]
at com.sun.proxy.$Proxy122.getSession(Unknown Source) [?:?]
at com.liferay.mail.kernel.service.MailServiceUtil.getSession(MailServiceUtil.java:81) [portal-kernel.jar:?]
at com.liferay.petra.mail.MailEngine.getSession(MailEngine.java:83) [com.liferay.petra.mail.jar:?]
at com.liferay.petra.mail.MailEngine.send(MailEngine.java:186) [com.liferay.petra.mail.jar:?]
at com.liferay.petra.mail.MailEngine.send(MailEngine.java:336) [com.liferay.petra.mail.jar:?]
at com.liferay.mail.messaging.MailMessageListener.doMailMessage(MailMessageListener.java:100) [portal-impl.jar:?]
at com.liferay.mail.messaging.MailMessageListener.doReceive(MailMessageListener.java:115) [portal-impl.jar:?]
at com.liferay.portal.kernel.messaging.BaseMessageListener.doReceive(BaseMessageListener.java:48) [portal-kernel.jar:?]
at com.liferay.portal.kernel.messaging.BaseMessageListener.receive(BaseMessageListener.java:34) [portal-kernel.jar:?]
at com.liferay.portal.kernel.messaging.InvokerMessageListener.receive(InvokerMessageListener.java:74) [portal-kernel.jar:?]
at com.liferay.portal.messaging.internal.ParallelDestination$1.run(ParallelDestination.java:56) [bundleFile:?]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) [?:?]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) [?:?]
at java.lang.Thread.run(Thread.java:834) [?:?]

Environment

  • JNDIメール設定によるLiferay DXP 7.4インストール。

解決策

この問題の根本的な原因は、 javax.mail.Session クラスが、2つのjavaライブラリから2つの異なるクラスローダーによってロードされていることです。

  • 最初のクラスは、アプリケーションサーバーのmail.jarライブラリからロードされます。 (ローダー 'javax.mail.api@1.6.5.redhat-00001')
  • 2番目のクラスは、Liferayアプリケーションのmail.jarライブラリからロードされます。 (ローダー com.liferay.shielded.container.internal.ShieldedContainerClassLoader @345def0f)).

こちらのtomcat JDNIインストールドキュメントによると、 https://tomcat.apache.org/tomcat-9.0-doc/jndi-resources-howto.html#JavaMail_Sessions

の「4. JavaMailライブラリのインストール」 の項目で、言われています:

配布物を解凍し、mail.jarを $CATALINA_HOME/libに配置し、mail Session Resourceの初期化時にTomcatが利用できるようにします。 注意: このjarを $CATALINA_HOME/libとWebアプリケーションのlibフォルダの両方に置くとエラーが発生します。 そのため、 $CATALINA_HOME/libの場所だけに置くようにしてください。

(この ドキュメント Tomcat 用 問題 JBoss 用 相当となる) 。

だから私はn 順序 エラー ソリューション メールjar library から Liferay web アプリケーション そう javax.mailSession class should always be resolved using the classloader that is at the application server level.

この mail.jar ライブラリをアプリケーションサーバーから削除するには、次のようにします:

  1. アプリケーションサーバーをシャットダウンする
  2. [LIFERAY_HOME]/tomcat-9.x.x/webapps/ROOT/WEB-INF/shielded-container-lib/mail.jar のバックアップを作成し、万が一復元が必要になった場合に備えておく。
  3. mail.jarshielded-container-lib フォルダから削除する。
  4. アプリケーションサーバーを起動する

追加情報

did-this-article-resolve-your-issue

legacy-knowledge-base