views:

307

answers:

2

I have updated my libraries, and now e-mails are sent without subject. I don't know where this happened...

Mail API is 1.4.3., Spring 2.5.6. and Spring Integration Mail 1.0.3.RELEASE.

<!-- Definitions for SMTP server -->
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
    <property name="host" value="${mail.host}" />
    <property name="username" value="${mail.username}" />
    <property name="password" value="${mail.password}" />
</bean>

<bean id="adminMailTemplate" class="org.springframework.mail.SimpleMailMessage" >
    <property name="from" value="${mail.admin.from}" />
    <property name="to" value="${mail.admin.to}" />
    <property name="cc">
        <list>
            <value>${mail.admin.cc1}</value>
        </list>
    </property>
</bean>

<!-- Mail service definition -->
<bean id="mailService" class="net.bbb.core.service.impl.MailServiceImpl">
    <property name="sender" ref="mailSender"/>
    <property name="mail" ref="adminMailTemplate"/>
</bean>

And properties mail.host,mail.username,mail.password,mail.admin.from,mail.admin.to, mail.admin.cc1.

Java class:

/** The sender. */
private MailSender sender;

/** The mail. */
private SimpleMailMessage mail;

public void sendMail() {
    this.mail.setSubject("Subject");

    this.mail.setText("msg body");          

    try {
        getSender().send(this.mail);
    } catch (MailException e) {
        log.error("Error sending mail!",e);
    }
}

public SimpleMailMessage getMail() {
    return this.mail;
}

public void setMail(SimpleMailMessage mail) {
    this.mail = mail;
}

public MailSender getSender() {
    return this.sender;
}

public void setSender(MailSender mailSender1) {
    this.sender = mailSender1;
}

Everything worked before, I am wondering if there may be any conflicts with new libraries.

+2  A: 

I expect it's something to do with the way that you're injecting a singleton SimpleMailMessage into your bean. This is not thread-safe, since every call to your sendMail method will be using the same underlying SimpleMailmessage object. It's quite possible that some implementation change in the new libraries now means this is broken.

SimpleMailMessage has a copy constructor, so you should do it like this:

<bean id="mailService" class="net.bbb.core.service.impl.MailServiceImpl">
    <property name="sender" ref="mailSender"/>
    <property name="template" ref="adminMailTemplate"/>
</bean>

and

private SimpleMailMessage template;

public void setTemplate(SimpleMailMessage template) {
   this.template = template;
}

public void sendMail() {
    SimpleMailMessage message = new SimpleMailMessage(template);
    message.setSubject("Subject");
    message.setText("msg body");          

    try {
        getSender().send(message);
    } catch (MailException e) {
        log.error("Error sending mail!",e);
    }
}
skaffman
+1 Thanks for this. But it did not remove the problem...
Trick
@Trick: There's nothing else in the information you've given us that would cause your issue.
skaffman
It must be something with libraries. I will revert everything.
Trick
@Trick: Don't revert everything. Revert one thing at a time until you nail down which specific change broke it. Scientific method, and all that.
skaffman
Yes, of course :) It is not important at the moment, so I will leave this, when I'll have time. And I will report, what happens. Thanks!
Trick
A: 

Finally - I had the time to resolve this.

In pom.xml, I have added java mail dependency and remove exclusion for geronimo javamail in apache axis transport http dependency.

Trick