Skip to content

Commit 56474a5

Browse files
artembilangaryrussell
authored andcommitted
GH-3560 Parse mail FROM as comma-delimited header (#3562)
Fixes #3560 According RFC 5322 `FROM` and `REPLY-TO` received mail message can be as an array of addresses. * Fix `MailUtils` to present those arrays as comma-delimited strings for Spring message headers * Fix tests to deal already with several addresses for `FROM` mime header. In the future we may change the logic to map those mime headers to arrays as it states according the mentioned RFC **Cherry-pick to `5.4.x` & `5.3.x`**
1 parent d5ff74b commit 56474a5

File tree

5 files changed

+13
-21
lines changed

5 files changed

+13
-21
lines changed

spring-integration-mail/src/main/java/org/springframework/integration/mail/support/MailUtils.java

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2019 the original author or authors.
2+
* Copyright 2016-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -25,12 +25,14 @@
2525

2626
import org.springframework.integration.mail.MailHeaders;
2727
import org.springframework.messaging.MessagingException;
28-
import org.springframework.util.Assert;
28+
import org.springframework.util.StringUtils;
2929

3030
/**
3131
* Utilities for handling mail messages.
3232
*
3333
* @author Gary Russell
34+
* @author Artem Bilan
35+
*
3436
* @since 4.3
3537
*
3638
*/
@@ -47,27 +49,19 @@ private MailUtils() {
4749
* @return the map.
4850
*/
4951
public static Map<String, Object> extractStandardHeaders(Message source) {
50-
Map<String, Object> headers = new HashMap<String, Object>();
52+
Map<String, Object> headers = new HashMap<>();
5153
try {
52-
headers.put(MailHeaders.FROM, convertToString(source.getFrom()));
54+
headers.put(MailHeaders.FROM, StringUtils.arrayToCommaDelimitedString(source.getFrom()));
5355
headers.put(MailHeaders.BCC, convertToStringArray(source.getRecipients(RecipientType.BCC)));
5456
headers.put(MailHeaders.CC, convertToStringArray(source.getRecipients(RecipientType.CC)));
5557
headers.put(MailHeaders.TO, convertToStringArray(source.getRecipients(RecipientType.TO)));
56-
headers.put(MailHeaders.REPLY_TO, convertToString(source.getReplyTo()));
58+
headers.put(MailHeaders.REPLY_TO, StringUtils.arrayToCommaDelimitedString(source.getReplyTo()));
5759
headers.put(MailHeaders.SUBJECT, source.getSubject());
5860
return headers;
5961
}
60-
catch (Exception e) {
61-
throw new MessagingException("conversion of MailMessage headers failed", e);
62-
}
63-
}
64-
65-
private static String convertToString(Address[] addresses) {
66-
if (addresses == null || addresses.length == 0) {
67-
return null;
62+
catch (Exception ex) {
63+
throw new MessagingException("conversion of MailMessage headers failed", ex);
6864
}
69-
Assert.state(addresses.length == 1, "expected a single value but received an Array");
70-
return addresses[0].toString();
7165
}
7266

7367
private static String[] convertToStringArray(Address[] addresses) {

spring-integration-mail/src/test/java/org/springframework/integration/mail/ImapMailReceiverTests.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@
7272
import org.springframework.integration.handler.AbstractReplyProducingMessageHandler;
7373
import org.springframework.integration.history.MessageHistory;
7474
import org.springframework.integration.mail.support.DefaultMailHeaderMapper;
75-
import org.springframework.integration.test.condition.LongRunningTest;
7675
import org.springframework.integration.test.mail.TestMailServer;
7776
import org.springframework.integration.test.mail.TestMailServer.ImapServer;
7877
import org.springframework.integration.test.util.TestUtils;
@@ -97,7 +96,6 @@
9796
@ContextConfiguration(
9897
"classpath:org/springframework/integration/mail/config/ImapIdleChannelAdapterParserTests-context.xml")
9998
@DirtiesContext
100-
@LongRunningTest
10199
public class ImapMailReceiverTests {
102100

103101
private AtomicInteger failed;
@@ -707,7 +705,7 @@ public void testConnectionException() throws Exception {
707705

708706
@Test // see INT-1801
709707
public void testImapLifecycleForRaceCondition() throws Exception {
710-
for (int i = 0; i < 1000; i++) {
708+
for (int i = 0; i < 100; i++) {
711709
final ImapMailReceiver receiver = new ImapMailReceiver("imap://foo");
712710
Store store = mock(Store.class);
713711
Folder folder = mock(Folder.class);

spring-integration-mail/src/test/java/org/springframework/integration/mail/config/Pop3Tests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public void testPop3() throws Exception {
6767
assertThat(headers.get(MailHeaders.TO, String[].class)[0]).isEqualTo("Foo <foo@bar>");
6868
assertThat(Arrays.toString(headers.get(MailHeaders.CC, String[].class))).isEqualTo("[a@b, c@d]");
6969
assertThat(Arrays.toString(headers.get(MailHeaders.BCC, String[].class))).isEqualTo("[e@f, g@h]");
70-
assertThat(headers.get(MailHeaders.FROM)).isEqualTo("Bar <bar@baz>");
70+
assertThat(headers.get(MailHeaders.FROM)).isEqualTo("Bar <bar@baz>,Bar2 <bar2@baz>");
7171
assertThat(headers.get(MailHeaders.SUBJECT)).isEqualTo("Test Email");
7272
assertThat(message.getPayload()).isEqualTo("foo\r\n\r\n");
7373
}

spring-integration-mail/src/test/java/org/springframework/integration/mail/dsl/MailTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ public void testPop3() throws IOException {
141141
assertThat(message).isNotNull();
142142
MessageHeaders headers = message.getHeaders();
143143
assertThat(headers.get(MailHeaders.TO, String[].class)).containsExactly("Foo <foo@bar>");
144-
assertThat(headers.get(MailHeaders.FROM)).isEqualTo("Bar <bar@baz>");
144+
assertThat(headers.get(MailHeaders.FROM)).isEqualTo("Bar <bar@baz>,Bar2 <bar2@baz>");
145145
assertThat(headers.get(MailHeaders.SUBJECT)).isEqualTo("Test Email");
146146
assertThat(message.getPayload()).isEqualTo("foo\r\n\r\n");
147147
assertThat(message.getHeaders().containsKey(IntegrationMessageHeaderAccessor.CLOSEABLE_RESOURCE)).isTrue();

spring-integration-test-support/src/main/java/org/springframework/integration/test/mail/TestMailServer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ public abstract class MailHandler implements Runnable {
489489
"To: Foo <foo@bar>\r\n"
490490
+ "cc: a@b, c@d\r\n"
491491
+ "bcc: e@f, g@h\r\n"
492-
+ "From: Bar <bar@baz>\r\n"
492+
+ "From: Bar <bar@baz>, Bar2 <bar2@baz>\r\n"
493493
+ "Subject: Test Email\r\n"
494494
+ "\r\n" + BODY;
495495

0 commit comments

Comments
 (0)