|
29 | 29 | import jakarta.mail.Multipart; |
30 | 30 | import jakarta.mail.Session; |
31 | 31 | import jakarta.mail.util.LineOutputStream; |
| 32 | +import jakarta.mail.util.StreamProvider; |
32 | 33 |
|
33 | 34 | import java.io.BufferedInputStream; |
34 | 35 | import java.io.ByteArrayInputStream; |
|
44 | 45 | import java.util.Enumeration; |
45 | 46 | import java.util.List; |
46 | 47 | import java.util.Properties; |
| 48 | +import java.util.ServiceConfigurationError; |
| 49 | + |
47 | 50 |
|
48 | 51 | /** |
49 | 52 | * This class represents a MIME style email message. It implements |
@@ -243,7 +246,7 @@ public MimeMessage(MimeMessage source) throws MessagingException { |
243 | 246 | allowutf8 = source.allowutf8; |
244 | 247 | source.writeTo(bos); |
245 | 248 | bos.close(); |
246 | | - try (InputStream bis = getStreamProvider().inputSharedByteArray(bos.toByteArray())) { |
| 249 | + try (InputStream bis = provider().inputSharedByteArray(bos.toByteArray())) { |
247 | 250 | parse(bis); |
248 | 251 | } |
249 | 252 | saved = true; |
@@ -1406,7 +1409,7 @@ protected InputStream getContentStream() throws MessagingException { |
1406 | 1409 | if (contentStream != null) |
1407 | 1410 | return ((SharedInputStream) contentStream).newStream(0, -1); |
1408 | 1411 | if (content != null) { |
1409 | | - return getStreamProvider().inputSharedByteArray(content); |
| 1412 | + return provider().inputSharedByteArray(content); |
1410 | 1413 | } |
1411 | 1414 | throw new MessagingException("No MimeMessage content"); |
1412 | 1415 | } |
@@ -1913,7 +1916,7 @@ public void writeTo(OutputStream os, String[] ignoreList) |
1913 | 1916 | // Else, the content is untouched, so we can just output it |
1914 | 1917 | // First, write out the header |
1915 | 1918 | Enumeration<String> hdrLines = getNonMatchingHeaderLines(ignoreList); |
1916 | | - LineOutputStream los = getStreamProvider().outputLineStream(os, allowutf8); |
| 1919 | + LineOutputStream los = provider().outputLineStream(os, allowutf8); |
1917 | 1920 | while (hdrLines.hasMoreElements()) |
1918 | 1921 | los.writeln(hdrLines.nextElement()); |
1919 | 1922 |
|
@@ -2319,6 +2322,25 @@ protected MimeMessage createMimeMessage(Session session) |
2319 | 2322 | return new MimeMessage(session); |
2320 | 2323 | } |
2321 | 2324 |
|
| 2325 | + private StreamProvider provider() throws MessagingException { |
| 2326 | + try { |
| 2327 | + try { |
| 2328 | + final Session s = this.session; |
| 2329 | + if (s != null) { |
| 2330 | + return s.getStreamProvider(); |
| 2331 | + } else { |
| 2332 | + return Session.getDefaultInstance(System.getProperties(), |
| 2333 | + null).getStreamProvider(); |
| 2334 | + } |
| 2335 | + } catch (ServiceConfigurationError sce) { |
| 2336 | + throw new IllegalStateException(sce); |
| 2337 | + } |
| 2338 | + } catch (RuntimeException re) { |
| 2339 | + throw new MessagingException("Unable to get " |
| 2340 | + + StreamProvider.class.getName(), re); |
| 2341 | + } |
| 2342 | + } |
| 2343 | + |
2322 | 2344 | boolean isStrict() { |
2323 | 2345 | return strict; |
2324 | 2346 | } |
|
0 commit comments