SimpleMailMessage不起作用

时间:2012-09-03 17:25:01

标签: spring sendmail

即时通讯使用spring SimpleMailMessage,每当我尝试发送邮件功能时,我都会遇到此异常:

java.lang.IllegalArgumentException: The 'original' message argument cannot be null

我的spring.xml

   <bean id="MailSender" class="com.util.MailSender">

    <property name="mailSender" ref="mailSender"/>
    <property name="templateMessage" ref="templateMessage"/>

</bean>

MailSenderClass:

  private MailSender mailSender;
    private SimpleMailMessage templateMessage;

    //getter and setter
    public void sendMail(String content, List<String> listEmail){
        SimpleMailMessage[] mailMessageArray = new SimpleMailMessage[listEmail.size()];
        Iterator<String> iterator = listEmail.iterator();

        for (int index = 0; iterator.hasNext(); index ++){

    SimpleMailMessage message = new SimpleMailMessage(this.templateMessage);
    String toAddress = iterator.next(); 
         message.setTo(toAddress);
     message.setText(content);  
         mailMessageArray[index] = message;

    }
         this.mailSender.send(mailMessageArray);
     }

Action Bean Class:

 private MailSender mailSender=new MailSender();

      public void ReSend(){
         mailSender.sendMail(listBEeans[i].getEmailContent(),listEmail);
          //able to pass the content and listEmails correctly

}

我是否错过了我的动作bean中的任何配置?谢谢

2 个答案:

答案 0 :(得分:1)

看起来您的bean正在创建对MailSender的本地引用,而不是使用注入的依赖项(假设您创建了一个)。你应该在某个地方:

AnObject
{
...
MailSender mailsender = (MailSender)context.getBean("mailSender");
...
}

然后你的豆子应该说:

private MailSender = AnObject.getMailSender();

答案 1 :(得分:0)

问题是构造函数中使用的模板是null。它不可能。

来自SimpleMailMessage的代码:

public SimpleMailMessage(SimpleMailMessage original) {
    Assert.notNull(original, "The 'original' message argument cannot be null");
...

检查你的bean templateMessage是否真的设置为某个东西,或者从MailSender中删除它并使用no-args构造函数。