diff --git a/api/src/main/java/jakarta/mail/internet/MimeMessage.java b/api/src/main/java/jakarta/mail/internet/MimeMessage.java index c1a71934..e832b097 100644 --- a/api/src/main/java/jakarta/mail/internet/MimeMessage.java +++ b/api/src/main/java/jakarta/mail/internet/MimeMessage.java @@ -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 @@ -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; @@ -44,6 +45,7 @@ import java.util.Enumeration; import java.util.List; import java.util.Properties; +import java.util.ServiceConfigurationError; /** @@ -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... @@ -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"); } @@ -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 hdrLines = getNonMatchingHeaderLines(ignoreList); - LineOutputStream los = session.getStreamProvider().outputLineStream(os, allowutf8); + LineOutputStream los = provider().outputLineStream(os, allowutf8); while (hdrLines.hasMoreElements()) los.writeln(hdrLines.nextElement()); @@ -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); + } + } } diff --git a/doc/release/CHANGES.txt b/doc/release/CHANGES.txt index 8d1675ee..efaf7d43 100644 --- a/doc/release/CHANGES.txt +++ b/doc/release/CHANGES.txt @@ -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 @@ -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 @@ -862,7 +863,7 @@ The following bugs have been fixed in the 1.1.2 release. fix bug in SMTP output that sometimes duplicated "." 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