2

我想使用 spring 框架发送纯文本电子邮件。我正在使用 netbeans IDE,并且还在我的依赖项文件夹中添加了 mail.jar、common.jar。我的代码如下:

 @Service("emailService")
    public class EmailService {

        @Inject
        private MailSender mailSender;



         @RequestMapping(method = RequestMethod.POST)
        public void sendMail(String from, String to, String subject, String body) {
             System.out.println("=============from:"+from);
                 System.out.println("==========to:"+to);
                 System.out.println("subject: "+subject);
                 System.out.println("body: "+body);

            SimpleMailMessage message = new SimpleMailMessage();

            message.setFrom(from);
            message.setTo(to);
            message.setSubject(subject);
            message.setText(body);
            this.mailSender.send(message); 
                System.out.println("----------after sendig mail");

        }
}

我收到 this.mailSender.send(message); 的 NullPointerException

我的 applicationContext.xml 如下:

 <!-- Required for sending mail using spring framework -->
<context:component-scan base-package="net.codejava.spring" />

<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
    <property name="host" value="smtp.gmail.com" />
    <property name="port" value="587" />
    <property name="username" value="USERNAME" />
    <property name="password" value="PASSWRD" />
    <property name="javaMailProperties">
        <props>
            <prop key="mail.transport.protocol">smtp</prop>
            <prop key="mail.smtp.auth">true</prop>
            <prop key="mail.smtp.starttls.enable">true</prop>
        </props>
    </property>
</bean>

请告诉我如何克服它。

4

1 回答 1

0

只需更改@Inject@Autowired

于 2013-03-01T07:11:50.930 回答