I am trying to send an html email inside a Spring (version 3.0.5.RELEASE) webapp using Spring's JavaMailSenderImpl
.
The emails are sent but they are incorrect:
- they are missing the subject
- the To field is shown empty or as "Undisclosed recipients:;"
- the html body is shown as plain text
I have tried the following, without success:
MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper message = new MimeMessageHelper(mimeMessage, false, "utf-8");
message.setTo(to);
message.setFrom(from);
message.setReplyTo(from);
message.setSubject(StringUtils.trim(subject));
message.setSentDate(new Date());
mimeMessage.setContent(StringUtils.trim(messageBody), "text/html");
mailSenderImpl.send(mimeMessage);
(mailSender
is Spring's JavaMailSenderImpl retrieved from application context; to
, from
, subject
, messageBody
are String
objects).
To set the content, I also tried:
message.setText(messageBody, true);
message.setText(messageBodyPlain, messageBodyText);
I also tried using a multipart message (setting MimeMessageHelper
's second argument to true
) and with a MimeMessagePreparator
.
Looking at the delivered messages, they do not contain the Content-Type:
or Subject
headers.
For my tests I have used my company's smtp server and Gmail's smtp server.
UPDATE: problem solved
Turns out my question is a duplicate of this other one: a bunch of Maven dependencies included geronimo-javamail_1.4_spec
, and this was the cause of malformed emails. I just had to exclude this jar, editing pom.xml like this:
<dependency>
<groupId>org.apache.ws.commons.axiom</groupId>
<artifactId>axiom-api</artifactId>
<version>1.2.8</version>
<exclusions>
<exclusion>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-javamail_1.4_spec</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.ws.commons.axiom</groupId>
<artifactId>axiom-dom</artifactId>
<version>1.2.8</version>
<exclusions>
<exclusion>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-javamail_1.4_spec</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.ws.commons.axiom</groupId>
<artifactId>axiom-impl</artifactId>
<version>1.2.8</version>
<exclusions>
<exclusion>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-javamail_1.4_spec</artifactId>
</exclusion>
</exclusions>
</dependency>