Skip to content

Commit aa1dc71

Browse files
authored
Cannot parse messages without a session #710 (#711)
Signed-off-by: jmehrens [email protected]
1 parent 30dbfe5 commit aa1dc71

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

api/src/main/java/jakarta/mail/internet/MimeMessage.java

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1997, 2023 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1997, 2024 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -29,6 +29,7 @@
2929
import jakarta.mail.Multipart;
3030
import jakarta.mail.Session;
3131
import jakarta.mail.util.LineOutputStream;
32+
import jakarta.mail.util.StreamProvider;
3233

3334
import java.io.BufferedInputStream;
3435
import java.io.ByteArrayInputStream;
@@ -44,6 +45,7 @@
4445
import java.util.Enumeration;
4546
import java.util.List;
4647
import java.util.Properties;
48+
import java.util.ServiceConfigurationError;
4749

4850

4951
/**
@@ -243,9 +245,9 @@ public MimeMessage(MimeMessage source) throws MessagingException {
243245
strict = source.strict;
244246
source.writeTo(bos);
245247
bos.close();
246-
InputStream bis = session.getStreamProvider().inputSharedByteArray(bos.toByteArray());
247-
parse(bis);
248-
bis.close();
248+
try (InputStream bis = provider().inputSharedByteArray(bos.toByteArray())) {
249+
parse(bis);
250+
}
249251
saved = true;
250252
} catch (IOException ex) {
251253
// should never happen, but just in case...
@@ -1408,7 +1410,7 @@ protected InputStream getContentStream() throws MessagingException {
14081410
if (contentStream != null)
14091411
return ((SharedInputStream) contentStream).newStream(0, -1);
14101412
if (content != null) {
1411-
return session.getStreamProvider().inputSharedByteArray(content);
1413+
return provider().inputSharedByteArray(content);
14121414
}
14131415
throw new MessagingException("No MimeMessage content");
14141416
}
@@ -1915,7 +1917,7 @@ public void writeTo(OutputStream os, String[] ignoreList)
19151917
// Else, the content is untouched, so we can just output it
19161918
// First, write out the header
19171919
Enumeration<String> hdrLines = getNonMatchingHeaderLines(ignoreList);
1918-
LineOutputStream los = session.getStreamProvider().outputLineStream(os, allowutf8);
1920+
LineOutputStream los = provider().outputLineStream(os, allowutf8);
19191921
while (hdrLines.hasMoreElements())
19201922
los.writeln(hdrLines.nextElement());
19211923

@@ -2320,4 +2322,23 @@ protected MimeMessage createMimeMessage(Session session)
23202322
throws MessagingException {
23212323
return new MimeMessage(session);
23222324
}
2325+
2326+
private StreamProvider provider() throws MessagingException {
2327+
try {
2328+
try {
2329+
final Session s = this.session;
2330+
if (s != null) {
2331+
return s.getStreamProvider();
2332+
} else {
2333+
return Session.getDefaultInstance(System.getProperties(),
2334+
null).getStreamProvider();
2335+
}
2336+
} catch (ServiceConfigurationError sce) {
2337+
throw new IllegalStateException(sce);
2338+
}
2339+
} catch (RuntimeException re) {
2340+
throw new MessagingException("Unable to get "
2341+
+ StreamProvider.class.getName(), re);
2342+
}
2343+
}
23232344
}

doc/release/CHANGES.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ longer available.
2424
E 631 Session.getService does not use proper classloader in OSGI environment
2525
E 665 Jakarta Mail erroneously assumes that classes can be loaded from Thread#getContextClassLoader
2626
E 695 SharedFileInputStream should comply with spec
27+
E 710 Cannot parse messages without a session
2728

2829

2930
CHANGES IN THE 2.1.2 RELEASE
@@ -141,7 +142,7 @@ GH 334 gimap set labels error with some non english characters
141142
The following bugs have been fixed in the 1.6.1 release.
142143

143144
GH 262 Some IMAP servers send EXPUNGE responses for unknown messages
144-
GH 278 BODYSTRUCTURE Parser fails on specific IMAP Server response
145+
GH 278 BODYSTRUCTURE Parser fails on specific IMAP Server response
145146
GH 283 clean up connections when closing IMAPStore
146147
GH 287 Allow relaxed Content-Disposition parsing
147148
GH 289 use a different IMAP tag prefix for each connection
@@ -862,7 +863,7 @@ The following bugs have been fixed in the 1.1.2 release.
862863
<no id> fix bug in SMTP output that sometimes duplicated "."
863864
<no id> close SMTP transport on I/O error
864865
4230541 don't send SMTP NOOP unnecessarily
865-
4216666 IMAP provider INTERNALDATE formatter error, causing
866+
4216666 IMAP provider INTERNALDATE formatter error, causing
866867
problems during appendMessages()
867868
4227888 IMAP provider does not honor the UID item in its FetchProfile
868869

0 commit comments

Comments
 (0)