简体   繁体   中英

Spring--cannot convert javax.mail.session

I am creating a mail session inside of my servlet context and then using JNDI to inject it into my spring framework design. Here's how the context looks:

<Resource name="mail/session" auth="Container"
            type="javax.mail.Session"
            mail.smtp.from="noreply@xxxx.com"
            mail.smtp.user="noreply@xxxx.com"
            mail.smtp.auth="true"
            mail.smtp.starttls.enable="true"
/>

And where I'm bringing it in:

  <bean id="smtpSession" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="java:comp/env/mail/session"/>
  </bean>

and where I'm injecting it into the spring java mail sender:

  <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
      <property name="host" ref="smtpHost"/>
      <property name="password" ref="smtpPassword"/>
      <property name="port" ref="smtpPort"/>
      <property name="username" ref="smtpFrom"/>
      <property name="session" ref="smtpSession"/>
  </bean>

Now here's the message I'm getting:

Caused by: java.lang.IllegalStateException: Cannot convert value of type [javax.
mail.Session] to required type [javax.mail.Session] for property 'session': no m
atching editors or conversion strategy found
        at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(Ty
peConverterDelegate.java:231)
        at org.springframework.beans.BeanWrapperImpl.convertIfNecessary(BeanWrap
perImpl.java:447)
        ... 51 more

Uh, what???? Why is it trying to convert it?

You most likely have two copies if javax.mail.Session on your classpath. One probably comes from the appserver's internal libraries, the other is likely packed in your app's lib directory. The two copies will clash when you try and use them like this.

Remove the one in your app's lib directory, and try again.

This is a classloading issue. Usually this is because the class is both in a jar in your server and in your application. In this case, you probably want to remove it from your application. Do you have something like mail.jar in your WEB-INF/lib or EAR?

In my case there were 2 jars- mail.jar and geronimo-javamail_1.4_mail-1.8.4.jar.

Only after removing both from my app's lib and putting mail.jar in server's lib , error was resolved.

If you are not sure what other jars could have this javax.mail.Session class present , you can use below command to find.

forfiles /S /M *.jar /C "cmd /c jar -tvf @file | findstr /C:"Session" && echo @path"

Session refers to class Session we are searching

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM