We are upgrading the project to Java 17, SpringBoot 3.0.2. We are using Amazon SES for sending emails with attachments.
Email without attachments is working fine. We get cast the exception for emails with attachments.
Here are our POM.xml dependencies:
<dependency>
<groupId>jakarta.mail</groupId>
<artifactId>jakarta.mail-api</artifactId>
<version>2.1.1</version>
<exclusions>
<exclusion>
<artifactId>geronimo-javamail_1.4_mail</artifactId>
<groupId>org.apache.geronimo.javamail</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.eclipse.angus</groupId>
<artifactId>angus-mail</artifactId>
<version>2.0.1</version>
<scope>runtime</scope>
<exclusions>
<exclusion>
<artifactId>geronimo-javamail_1.4_mail</artifactId>
<groupId>org.apache.geronimo.javamail</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.sun.activation</groupId>
<artifactId>jakarta.activation</artifactId>
<version>2.0.1</version>
<exclusions>
<exclusion>
<artifactId>geronimo-javamail_1.4_mail</artifactId>
<groupId>org.apache.geronimo.javamail</groupId>
</exclusion>
</exclusions>
</dependency>
We are getting an error while writing to output stream for an attachment:
MimeMessage message = new MimeMessage(session);
// Add subject, from and to lines.
message.setSubject(subject);
message.setFrom(new InternetAddress(fromEmail));
message.setRecipients(javax.mail.Message.RecipientType.TO, InternetAddress.parse(addresses));
// Create a multipart/alternative child container.
MimeMultipart msgBody = new MimeMultipart("alternative");
// Create a wrapper for the HTML and text parts.
MimeBodyPart wrap = new MimeBodyPart();
// Define the HTML part.
MimeBodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent(replacedEmailBody, "text/html; charset=UTF-8");
// Add the HTML parts to the child container.
msgBody.addBodyPart(htmlPart);
// Add the child container to the wrapper object.
wrap.setContent(msgBody);
// Create a multipart/mixed parent container.
**MimeMultipart msg = new MimeMultipart("mixed");**
// Add the parent container to the message.
message.setContent(msg);
// Add the multipart/alternative part to the message.
msg.addBodyPart(wrap);
// Define the attachment
MimeBodyPart att = new MimeBodyPart();
DataSource fds = new FileDataSource(file);
att.setDataHandler(new DataHandler(fds));
att.setFileName(fds.getName());
msg.addBodyPart(att);
addLogo(msg);
// Send the email.
try {
**ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
message.writeTo(outputStream); // This line gives above exception **
RawMessage rawMessage = new RawMessage(ByteBuffer.wrap(outputStream.toByteArray()));
SendRawEmailRequest rawEmailRequest = new SendRawEmailRequest(rawMessage);
emailClient.sendRawEmail(rawEmailRequest);
// Display an error if something goes wrong.
} catch (Exception ex) {
logger.error(ErrorCode.FAILED_TO_SEND_EMAIL.getErrorMessage());
logger.error("Message : {}", ex);
throw new AppException(ErrorCode.FAILED_TO_SEND_EMAIL);
}
Thanks in advance.