Trying to send email with an excel attachment with some body text. For whatever reason, the email that I receive is always an email with the email I want to send as attachment, then the excel itself is attached to the attached email...
Is there some property to disable this behavior?
public void sendEmailWithAttachment(EmailProperties properties) {
MimeMessage message = javaMailSender.createMimeMessage();
try {
message.setFrom(properties.getFrom());
message.setHeader("X-Mailer", properties.getHeader());
if (properties.getPriority() != null) {
message.setHeader("X-Priority", properties.getPriority().getValue());
}
InternetAddress[] toEmailArray = properties.getToAddresses().toArray(new InternetAddress[properties.getToAddresses().size()]);
message.setRecipients(javax.mail.Message.RecipientType.TO, toEmailArray);
message.setSubject(properties.getSubject());
message.setSentDate(new Date());
Multipart multipart = new MimeMultipart();
if (StringUtils.isNotBlank(properties.getMessageText())) {
MimeBodyPart mimeBodyPart = new MimeBodyPart();
mimeBodyPart.setText(properties.getMessageText());
multipart.addBodyPart(mimeBodyPart);
}
MimeBodyPart mimeBodyPart = new MimeBodyPart();
mimeBodyPart.setDataHandler(new DataHandler(new ByteArrayDataSource(properties.getContents(), properties.getContentType())));
mimeBodyPart.setFileName(properties.getFileName());
multipart.addBodyPart(mimeBodyPart);
message.setContent(multipart);
send(message);
} catch (Exception exc) {
logger.error("Error attempting to send email!", exc);
}
}
I've figured it out, if the email contain no body text, the email sent has the email as attachment. Once there is body text, the email looked normal where it shows the body text then the excel as attachment... Just why...