Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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="[email protected]"
            mail.smtp.user="[email protected]"
            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?

like image 332
Thom Avatar asked Jan 11 '12 15:01

Thom


2 Answers

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.

like image 79
skaffman Avatar answered Oct 02 '22 07:10

skaffman


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?

like image 32
Jeremiah Orr Avatar answered Oct 02 '22 07:10

Jeremiah Orr