Skip to content

spring-integration-mail minor changes #9948

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2024 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -48,7 +48,7 @@
* @author Mark Fisher
* @author Oleg Zhurakousky
* @author Artem Bilan
*
* @author Ma Jiandong
* @see MailHeaders
*/
public class MailSendingMessageHandler extends AbstractMessageHandler {
Expand All @@ -73,14 +73,14 @@ public String getComponentType() {
@Override
protected final void handleMessageInternal(Message<?> message) {
MailMessage mailMessage = convertMessageToMailMessage(message);
if (mailMessage instanceof SimpleMailMessage) {
this.mailSender.send((SimpleMailMessage) mailMessage);
if (mailMessage instanceof SimpleMailMessage simpleMailMessage) {
this.mailSender.send(simpleMailMessage);
}
else if (mailMessage instanceof MimeMailMessage) {
else if (mailMessage instanceof MimeMailMessage mimeMailMessage) {
Assert.state(this.mailSender instanceof JavaMailSender,
"this adapter requires a 'JavaMailSender' to send a 'MimeMailMessage'");

((JavaMailSender) this.mailSender).send(((MimeMailMessage) mailMessage).getMimeMessage());
((JavaMailSender) this.mailSender).send(mimeMailMessage.getMimeMessage());
}
else {
throw new IllegalArgumentException(
Expand All @@ -92,11 +92,11 @@ else if (mailMessage instanceof MimeMailMessage) {
private MailMessage convertMessageToMailMessage(Message<?> message) {
MailMessage mailMessage;
Object payload = message.getPayload();
if (payload instanceof MimeMessage) {
mailMessage = new MimeMailMessage((MimeMessage) payload);
if (payload instanceof MimeMessage mimeMessage) {
mailMessage = new MimeMailMessage(mimeMessage);
}
else if (payload instanceof MailMessage) {
mailMessage = (MailMessage) payload;
else if (payload instanceof MailMessage mailMsg) {
mailMessage = mailMsg;
}
else if (payload instanceof byte[]) {
mailMessage = createMailMessageFromByteArrayMessage((Message<byte[]>) message);
Expand Down Expand Up @@ -168,8 +168,8 @@ private void applyHeadersToMailMessage(MailMessage mailMessage, MessageHeaders h
if (to != null) {
mailMessage.setTo(to);
}
if (mailMessage instanceof SimpleMailMessage) {
Assert.state(!ObjectUtils.isEmpty(((SimpleMailMessage) mailMessage).getTo()),
if (mailMessage instanceof SimpleMailMessage simpleMailMessage) {
Assert.state(!ObjectUtils.isEmpty(simpleMailMessage.getTo()),
"No recipient has been provided on the MailMessage or the 'MailHeaders.TO' header.");
}
String[] cc = retrieveHeaderValueAsStringArray(headers, MailHeaders.CC);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,9 +244,9 @@ private void verifyProtocol() {

@Override
public void destroy() {
if (this.receiver != null && this.receiver instanceof DisposableBean) {
if (this.receiver != null && this.receiver instanceof DisposableBean disposableReceiver) {
try {
((DisposableBean) this.receiver).destroy();
disposableReceiver.destroy();
}
catch (Exception e) {
throw new IllegalStateException(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ public Map<String, Object> toHeaders(MimeMessage source) {
MultiValueMap<String, String> rawHeaders = new LinkedMultiValueMap<String, String>();
while (allHeaders.hasMoreElements()) {
Object headerInstance = allHeaders.nextElement();
if (headerInstance instanceof Header) {
Header header = (Header) headerInstance;
if (headerInstance instanceof Header header) {
rawHeaders.add(header.getName(), header.getValue());
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2024 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
/**
* @author Marius Bogoevici
* @author Artem Bilan
* @author Ma Jiandong
*/
@SpringJUnitConfig
public class MailSendingMessageHandlerContextTests {
Expand Down Expand Up @@ -77,7 +78,7 @@ public void stringMessagesWithConfiguration() {
.isEqualTo(0);
assertThat(this.mailSender.getSentSimpleMailMessages().size()).as("only one simple message must be sent")
.isEqualTo(1);
assertThat(this.mailSender.getSentSimpleMailMessages().get(0)).as("message content different from expected")
assertThat(this.mailSender.getSentSimpleMailMessages().get(0)).as("message content same as expected")
.isEqualTo(mailMessage);
}

Expand All @@ -90,19 +91,19 @@ public void byteArrayMessage() throws Exception {
.setHeader(MailHeaders.TO, MailTestsHelper.TO)
.build();
this.handler.handleMessage(message);
assertThat(this.mailSender.getSentMimeMessages().size()).as("no mime message should have been sent")
assertThat(this.mailSender.getSentMimeMessages().size()).as("only one mime message should have been sent")
.isEqualTo(1);
assertThat(this.mailSender.getSentSimpleMailMessages().size()).as("only one simple message must be sent")
assertThat(this.mailSender.getSentSimpleMailMessages().size()).as("no simple message must be sent")
.isEqualTo(0);
byte[] buffer = new byte[1024];
MimeMessage mimeMessage = this.mailSender.getSentMimeMessages().get(0);
assertThat(mimeMessage.getContent() instanceof Multipart).as("message must be multipart").isTrue();
int size = new DataInputStream(((Multipart) mimeMessage.getContent()).getBodyPart(0).getInputStream())
.read(buffer);
assertThat(size).as("buffer size does not match").isEqualTo(payload.length);
assertThat(size).as("buffer size does match").isEqualTo(payload.length);
byte[] messageContent = new byte[size];
System.arraycopy(buffer, 0, messageContent, 0, payload.length);
assertThat(messageContent).as("buffer content does not match").isEqualTo(payload);
assertThat(messageContent).as("buffer content does match").isEqualTo(payload);
assertThat(MailTestsHelper.TO.length).isEqualTo(mimeMessage.getRecipients(Message.RecipientType.TO).length);
}

Expand All @@ -125,7 +126,7 @@ public void mailOutboundChannelAdapterWithinChain() {
.isEqualTo(0);
assertThat(this.mailSender.getSentSimpleMailMessages().size()).as("only one simple message must be sent")
.isEqualTo(1);
assertThat(this.mailSender.getSentSimpleMailMessages().get(0)).as("message content different from expected")
assertThat(this.mailSender.getSentSimpleMailMessages().get(0)).as("message content same as expected")
.isEqualTo(mailMessage);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2024 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -35,6 +35,7 @@
* @author Marius Bogoevici
* @author Oleg Zhurakousky
* @author Artem Bilan
* @author Ma Jiandong
*/
public class MailSendingMessageHandlerTests {

Expand All @@ -60,7 +61,7 @@ public void textMessage() {
assertThat(mailSender.getSentMimeMessages().size()).as("no mime message should have been sent").isEqualTo(0);
assertThat(mailSender.getSentSimpleMailMessages().size()).as("only one simple message must be sent")
.isEqualTo(1);
assertThat(mailSender.getSentSimpleMailMessages().get(0)).as("message content different from expected")
assertThat(mailSender.getSentSimpleMailMessages().get(0)).as("message content same as expected")
.isEqualTo(mailMessage);
}

Expand All @@ -78,10 +79,10 @@ public void byteArrayMessage() throws Exception {
assertThat(mimeMessage.getContent() instanceof Multipart).as("message must be multipart").isTrue();
int size = new DataInputStream(((Multipart) mimeMessage.getContent()).getBodyPart(0).getInputStream())
.read(buffer);
assertThat(size).as("buffer size does not match").isEqualTo(payload.length);
assertThat(size).as("buffer size does match").isEqualTo(payload.length);
byte[] messageContent = new byte[size];
System.arraycopy(buffer, 0, messageContent, 0, payload.length);
assertThat(messageContent).as("buffer content does not match").isEqualTo(payload);
assertThat(messageContent).as("buffer content does match").isEqualTo(payload);
assertThat(MailTestsHelper.TO.length).isEqualTo(mimeMessage.getRecipients(Message.RecipientType.TO).length);
}

Expand All @@ -92,7 +93,7 @@ public void mailHeaders() {
assertThat(mailSender.getSentMimeMessages().size()).as("no mime message should have been sent").isEqualTo(0);
assertThat(mailSender.getSentSimpleMailMessages().size()).as("only one simple message must be sent")
.isEqualTo(1);
assertThat(mailSender.getSentSimpleMailMessages().get(0)).as("message content different from expected")
assertThat(mailSender.getSentSimpleMailMessages().get(0)).as("message content same as expected")
.isEqualTo(mailMessage);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -22,19 +22,14 @@

import jakarta.mail.Session;
import jakarta.mail.internet.MimeMessage;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.mail.MailHeaders;
import org.springframework.integration.mail.MailSendingMessageHandler;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.support.GenericMessage;
import org.springframework.util.FileCopyUtils;

import static org.assertj.core.api.Assertions.assertThat;
Expand All @@ -48,23 +43,10 @@
* @author Oleg Zhurakousky
* @author Gary Russell
* @author Artem Bilan
*
* @author Ma Jiandong
*/
public class MessageWithContentTypeTests {

@Test
@Disabled
public void testSendEmail() throws Exception {
ConfigurableApplicationContext ac = new ClassPathXmlApplicationContext(
"MessageWithContentTypeTests-context.xml", this.getClass());
MessageChannel inputChannel = ac.getBean("inputChannel", MessageChannel.class);
StringWriter writer = new StringWriter();
FileReader reader = new FileReader("src/test/java/org/springframework/integration/mail/config/test.html");
FileCopyUtils.copy(reader, writer);
inputChannel.send(new GenericMessage<>(writer.getBuffer().toString()));
ac.close();
}

@Test
public void testMessageConversionWithHtmlAndContentType() throws Exception {
JavaMailSender sender = mock(JavaMailSender.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2024 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,15 +17,19 @@
package org.springframework.integration.mail.config;

import java.util.Properties;
import java.util.stream.Stream;

import jakarta.mail.URLName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import org.springframework.beans.DirectFieldAccessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.integration.endpoint.SourcePollingChannelAdapter;
import org.springframework.integration.mail.ImapMailReceiver;
import org.springframework.integration.mail.MailReceiver;
import org.springframework.integration.mail.MailReceivingMessageSource;
import org.springframework.integration.mail.Pop3MailReceiver;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
Expand All @@ -36,35 +40,25 @@
* @author Jonas Partner
* @author Mark Fisher
* @author Artem Bilan
* @author Ma Jiandong
*/
@SpringJUnitConfig
public class PollingMailSourceParserTests {

@Autowired
private ApplicationContext context;

@Test
public void imapAdapter() {
Object adapter = context.getBean("imapAdapter");
assertThat(adapter.getClass()).isEqualTo(SourcePollingChannelAdapter.class);
DirectFieldAccessor adapterAccessor = new DirectFieldAccessor(adapter);
assertThat(adapterAccessor.getPropertyValue("autoStartup")).isEqualTo(Boolean.FALSE);
Object channel = context.getBean("channel");
assertThat(adapterAccessor.getPropertyValue("outputChannel")).isEqualTo(channel);
Object source = adapterAccessor.getPropertyValue("source");
assertThat(source.getClass()).isEqualTo(MailReceivingMessageSource.class);
Object receiver = new DirectFieldAccessor(source).getPropertyValue("mailReceiver");
assertThat(receiver.getClass()).isEqualTo(ImapMailReceiver.class);
DirectFieldAccessor receiverAccessor = new DirectFieldAccessor(receiver);
Object url = receiverAccessor.getPropertyValue("url");
assertThat(url).isEqualTo(new URLName("imap:foo"));
Properties properties = (Properties) receiverAccessor.getPropertyValue("javaMailProperties");
assertThat(properties.getProperty("foo")).isEqualTo("bar");
static Stream<Arguments> methodArguments() {
return Stream.of(
Arguments.arguments("imapAdapter", ImapMailReceiver.class, "imap:foo"),
Arguments.arguments("pop3Adapter", Pop3MailReceiver.class, "pop3:bar")
);
}

@Test
public void pop3Adapter() {
Object adapter = context.getBean("pop3Adapter");
@ParameterizedTest
@MethodSource("methodArguments")
public void inboundChannelAdaptorTest(String adapterName, Class<MailReceiver> mailReceiverClass, String storeUri) {
Object adapter = context.getBean(adapterName);
assertThat(adapter.getClass()).isEqualTo(SourcePollingChannelAdapter.class);
DirectFieldAccessor adapterAccessor = new DirectFieldAccessor(adapter);
assertThat(adapterAccessor.getPropertyValue("autoStartup")).isEqualTo(Boolean.FALSE);
Expand All @@ -73,10 +67,10 @@ public void pop3Adapter() {
Object source = adapterAccessor.getPropertyValue("source");
assertThat(source.getClass()).isEqualTo(MailReceivingMessageSource.class);
Object receiver = new DirectFieldAccessor(source).getPropertyValue("mailReceiver");
assertThat(receiver.getClass()).isEqualTo(Pop3MailReceiver.class);
assertThat(receiver.getClass()).isEqualTo(mailReceiverClass);
DirectFieldAccessor receiverAccessor = new DirectFieldAccessor(receiver);
Object url = receiverAccessor.getPropertyValue("url");
assertThat(url).isEqualTo(new URLName("pop3:bar"));
assertThat(url).isEqualTo(new URLName(storeUri));
Properties properties = (Properties) receiverAccessor.getPropertyValue("javaMailProperties");
assertThat(properties.getProperty("foo")).isEqualTo("bar");
}
Expand Down