简体   繁体   中英

NULL Pointer exception for sending html mail using Spring MVC

Spring-Mail.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
    <property name="host" value="smtp.gmail.com" />


    <property name="javaMailProperties">
       <props>
    </property>
</bean>

<bean id="mailMail" class="com.mkyong.common.EmailProcessor">
    <property name="mailSender" ref="mailSender" />
</bean>

</beans>

EmailProcessor.java

eBeanFactory@7e78d6c6: defining beans [mailSender,mailMail]; root of factory hierarchy java.lang.NullPointerException

private JavaMailSender javaMailSender;

You have to call the setter of this property In your case private MailSender mailSender; will be automatically injected by spring. But private JavaMailSender javaMailSender; will be null.

And you are calling javaMailSender.send(mimeMessage); resulting in null pointer.

Use (JavaMailSender)mailSender.send(mimeMessage); instead of javaMailSender.send(mimeMessage);

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