Skip to content

Commit

Permalink
Cannot parse messages without a session #710
Browse files Browse the repository at this point in the history
Signed-off-by: jmehrens [email protected]
  • Loading branch information
jmehrens committed Feb 16, 2024
1 parent 30dbfe5 commit c659248
Showing 1 changed file with 26 additions and 5 deletions.
31 changes: 26 additions & 5 deletions api/src/main/java/jakarta/mail/internet/MimeMessage.java
Original file line number Diff line number Diff line change
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);
}
}
}

0 comments on commit c659248

Please sign in to comment.