Skip to content

Commit

Permalink
Cannot parse messages without a session #710 (#711)
Browse files Browse the repository at this point in the history
Signed-off-by: jmehrens [email protected]
  • Loading branch information
jmehrens authored Feb 16, 2024
1 parent 30dbfe5 commit aa1dc71
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
33 changes: 27 additions & 6 deletions api/src/main/java/jakarta/mail/internet/MimeMessage.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2023 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2024 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -29,6 +29,7 @@
import jakarta.mail.Multipart;
import jakarta.mail.Session;
import jakarta.mail.util.LineOutputStream;
import jakarta.mail.util.StreamProvider;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
Expand All @@ -44,6 +45,7 @@
import java.util.Enumeration;
import java.util.List;
import java.util.Properties;
import java.util.ServiceConfigurationError;


/**
Expand Down Expand Up @@ -243,9 +245,9 @@ public MimeMessage(MimeMessage source) throws MessagingException {
strict = source.strict;
source.writeTo(bos);
bos.close();
InputStream bis = session.getStreamProvider().inputSharedByteArray(bos.toByteArray());
parse(bis);
bis.close();
try (InputStream bis = provider().inputSharedByteArray(bos.toByteArray())) {
parse(bis);
}
saved = true;
} catch (IOException ex) {
// should never happen, but just in case...
Expand Down Expand Up @@ -1408,7 +1410,7 @@ protected InputStream getContentStream() throws MessagingException {
if (contentStream != null)
return ((SharedInputStream) contentStream).newStream(0, -1);
if (content != null) {
return session.getStreamProvider().inputSharedByteArray(content);
return provider().inputSharedByteArray(content);
}
throw new MessagingException("No MimeMessage content");
}
Expand Down Expand Up @@ -1915,7 +1917,7 @@ public void writeTo(OutputStream os, String[] ignoreList)
// Else, the content is untouched, so we can just output it
// First, write out the header
Enumeration<String> hdrLines = getNonMatchingHeaderLines(ignoreList);
LineOutputStream los = session.getStreamProvider().outputLineStream(os, allowutf8);
LineOutputStream los = provider().outputLineStream(os, allowutf8);
while (hdrLines.hasMoreElements())
los.writeln(hdrLines.nextElement());

Expand Down Expand Up @@ -2320,4 +2322,23 @@ protected MimeMessage createMimeMessage(Session session)
throws MessagingException {
return new MimeMessage(session);
}

private StreamProvider provider() throws MessagingException {
try {
try {
final Session s = this.session;
if (s != null) {
return s.getStreamProvider();
} else {
return Session.getDefaultInstance(System.getProperties(),
null).getStreamProvider();
}
} catch (ServiceConfigurationError sce) {
throw new IllegalStateException(sce);
}
} catch (RuntimeException re) {
throw new MessagingException("Unable to get "
+ StreamProvider.class.getName(), re);
}
}
}
5 changes: 3 additions & 2 deletions doc/release/CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ longer available.
E 631 Session.getService does not use proper classloader in OSGI environment
E 665 Jakarta Mail erroneously assumes that classes can be loaded from Thread#getContextClassLoader
E 695 SharedFileInputStream should comply with spec
E 710 Cannot parse messages without a session


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

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

Expand Down

0 comments on commit aa1dc71

Please sign in to comment.