Skip to content
This repository was archived by the owner on Aug 20, 2025. It is now read-only.

Commit 3cc5d4d

Browse files
committed
Handle more cases of newlines in addresses - bug 7529
1 parent 6bf7df8 commit 3cc5d4d

File tree

3 files changed

+87
-8
lines changed

3 files changed

+87
-8
lines changed

mail/src/main/java/javax/mail/internet/MimeMessage.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
33
*
4-
* Copyright (c) 1997-2015 Oracle and/or its affiliates. All rights reserved.
4+
* Copyright (c) 1997-2016 Oracle and/or its affiliates. All rights reserved.
55
*
66
* The contents of this file are subject to the terms of either the GNU
77
* General Public License Version 2 only ("GPL") or the Common Development
@@ -393,7 +393,7 @@ public void setFrom(Address address) throws MessagingException {
393393
if (address == null)
394394
removeHeader("From");
395395
else
396-
setHeader("From", address.toString());
396+
setHeader("From", MimeUtility.fold(6, address.toString()));
397397
}
398398

399399
/**
@@ -497,7 +497,7 @@ public void setSender(Address address) throws MessagingException {
497497
if (address == null)
498498
removeHeader("Sender");
499499
else
500-
setHeader("Sender", address.toString());
500+
setHeader("Sender", MimeUtility.fold(8, address.toString()));
501501
}
502502

503503
/**

mail/src/main/java/javax/mail/internet/NewsAddress.java

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
33
*
4-
* Copyright (c) 1997-2014 Oracle and/or its affiliates. All rights reserved.
4+
* Copyright (c) 1997-2016 Oracle and/or its affiliates. All rights reserved.
55
*
66
* The contents of this file are subject to the terms of either the GNU
77
* General Public License Version 2 only ("GPL") or the Common Development
@@ -81,7 +81,9 @@ public NewsAddress(String newsgroup) {
8181
* @param host the host
8282
*/
8383
public NewsAddress(String newsgroup, String host) {
84-
this.newsgroup = newsgroup;
84+
// XXX - this method should throw an exception so we can report
85+
// illegal addresses, but for now just remove whitespace
86+
this.newsgroup = newsgroup.replaceAll("\\s+", "");
8587
this.host = host;
8688
}
8789

@@ -182,14 +184,24 @@ public static String toString(Address[] addresses) {
182184

183185
StringBuffer s =
184186
new StringBuffer(((NewsAddress)addresses[0]).toString());
185-
for (int i = 1; i < addresses.length; i++)
186-
s.append(",").append(((NewsAddress)addresses[i]).toString());
187+
int used = s.length();
188+
for (int i = 1; i < addresses.length; i++) {
189+
s.append(",");
190+
used++;
191+
String ng = ((NewsAddress)addresses[i]).toString();
192+
if (used + ng.length() > 76) {
193+
s.append("\r\n\t");
194+
used = 8;
195+
}
196+
s.append(ng);
197+
used += ng.length();
198+
}
187199

188200
return s.toString();
189201
}
190202

191203
/**
192-
* Parse the given comma separated sequence of newsgroup into
204+
* Parse the given comma separated sequence of newsgroups into
193205
* NewsAddress objects.
194206
*
195207
* @param newsgroups comma separated newsgroup string

mail/src/test/java/javax/mail/internet/MimeMessageTest.java

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050

5151
import javax.mail.*;
5252
import static javax.mail.Message.RecipientType.*;
53+
import static javax.mail.internet.MimeMessage.RecipientType.*;
5354

5455
import org.junit.*;
5556
import static org.junit.Assert.assertArrayEquals;
@@ -95,6 +96,72 @@ public void testSetRecipientStringNull() throws Exception {
9596
assertArrayEquals("To: is removed", null, m.getRecipients(TO));
9697
}
9798

99+
/**
100+
* Test that setFrom with an address containing a newline is folded
101+
* properly.
102+
* (Bug 7529)
103+
*/
104+
@Test
105+
public void testSetFromFold() throws Exception {
106+
InternetAddress addr = new InternetAddress("[email protected]", "Joe\r\nBad");
107+
MimeMessage m = new MimeMessage(s);
108+
m.setFrom(addr);
109+
assertEquals("Joe\r\n Bad <[email protected]>", m.getHeader("From", null));
110+
}
111+
112+
/**
113+
* Test that setSender with an address containing a newline is folded
114+
* properly.
115+
* (Bug 7529)
116+
*/
117+
@Test
118+
public void testSetSenderFold() throws Exception {
119+
InternetAddress addr = new InternetAddress("[email protected]", "Joe\r\nBad");
120+
MimeMessage m = new MimeMessage(s);
121+
m.setSender(addr);
122+
assertEquals("Joe\r\n Bad <[email protected]>", m.getHeader("Sender", null));
123+
}
124+
125+
/**
126+
* Test that setRecipient with a newsgroup address containing a newline is
127+
* handled properly.
128+
* (Bug 7529)
129+
*/
130+
@Test
131+
public void testSetNewsgroupWhitespace() throws Exception {
132+
NewsAddress addr = new NewsAddress("alt.\r\nbad");
133+
MimeMessage m = new MimeMessage(s);
134+
m.setRecipient(NEWSGROUPS, addr);
135+
assertEquals("alt.bad", m.getHeader("Newsgroups", null));
136+
}
137+
138+
/**
139+
* Test that setRecipients with many newsgroup addresses is folded properly.
140+
* (Bug 7529)
141+
*/
142+
@Test
143+
public void testSetNewsgroupFold() throws Exception {
144+
NewsAddress[] longng = NewsAddress.parse(
145+
"alt.loooooooooooooooooooooooooooooooooooooooooooooooooong," +
146+
"alt.verylongggggggggggggggggggggggggggggggggggggggggggggg");
147+
MimeMessage m = new MimeMessage(s);
148+
m.setRecipients(NEWSGROUPS, longng);
149+
assertTrue(m.getHeader("Newsgroups", null).indexOf("\r\n\t") > 0);
150+
}
151+
152+
/**
153+
* Test that newsgroups can be set and read back (even if folded).
154+
*/
155+
@Test
156+
public void testSetGetNewsgroups() throws Exception {
157+
NewsAddress[] longng = NewsAddress.parse(
158+
"alt.loooooooooooooooooooooooooooooooooooooooooooooooooong," +
159+
"alt.verylongggggggggggggggggggggggggggggggggggggggggggggg");
160+
MimeMessage m = new MimeMessage(s);
161+
m.setRecipients(NEWSGROUPS, longng);
162+
assertArrayEquals(longng, m.getRecipients(NEWSGROUPS));
163+
}
164+
98165
/**
99166
* Test that copying a DataHandler from one message to another
100167
* has the desired effect.

0 commit comments

Comments
 (0)