From cbc0a11fa9e85ef7bdc3186ca28f9f9b0373b568 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Sexenian?= <99925035+tomas-sexenian@users.noreply.github.com> Date: Mon, 8 Jul 2024 11:47:51 -0300 Subject: [PATCH] Close attachment streams only after they have been read Changes implemented in client generated PR #879 --- .../com/genexus/internet/SMTPSession.java | 60 +++++++++---------- 1 file changed, 29 insertions(+), 31 deletions(-) diff --git a/gxmail/src/main/java/com/genexus/internet/SMTPSession.java b/gxmail/src/main/java/com/genexus/internet/SMTPSession.java index 1ee39c2a1..bca8915c3 100644 --- a/gxmail/src/main/java/com/genexus/internet/SMTPSession.java +++ b/gxmail/src/main/java/com/genexus/internet/SMTPSession.java @@ -611,46 +611,44 @@ private String getNextMessage(String sTime, String sPrefix, boolean end) return "--" + getStartMessage(sTime, sPrefix) + (end?"--":""); } - private void sendAttachment(String sTime, String fileNamePath, String attachmentPath) throws GXMailException, IOException - { + private void sendAttachment(String sTime, String fileNamePath, String attachmentPath) throws GXMailException, IOException { InputStream is = null; String fileName = fileNamePath; FileInputStream fis = null; - if (fileNamePath.lastIndexOf(File.separator) != -1) + if (fileNamePath.lastIndexOf(File.separator) != -1) fileName = fileNamePath.substring(fileNamePath.lastIndexOf(File.separator) + 1); - try { - fis = new FileInputStream(attachmentPath + fileNamePath); - is = fis; - } - catch (FileNotFoundException e) { - log ("11 - FileNotFound " + e.getMessage()); + try { + fis = new FileInputStream(attachmentPath + fileNamePath); + is = fis; + + println(getNextMessageIdMixed(sTime, false)); + println("Content-Type: " + "application/octet-stream"); + println("Content-Transfer-Encoding: " + "base64"); + println("Content-Disposition: " + "attachment; filename=\"" + GXMailer.getEncodedString(fileName) + "\""); + println(""); + + int BUFFER_SIZE = 4096; + byte[] buffer = new byte[BUFFER_SIZE]; + OutputStream base64Output = new Base64OutputStream(outStream); + int n = is.read(buffer, 0, BUFFER_SIZE); + while (n >= 0) { + base64Output.write(buffer, 0, n); + n = is.read(buffer, 0, BUFFER_SIZE); + } + base64Output.flush(); + outStream.writeBytes(CRLF); + outStream.flush(); + } catch (FileNotFoundException e) { + log("11 - FileNotFound " + e.getMessage()); throw new GXMailException("Can't find " + attachmentPath + fileNamePath, MAIL_InvalidAttachment); } finally { - if (is != null) - is.close(); - if (fis != null) - fis.close(); + if (is != null) + is.close(); + if (fis != null) + fis.close(); } - - println(getNextMessageIdMixed(sTime, false)); - println("Content-Type: " + "application/octet-stream"); - println("Content-Transfer-Encoding: " + "base64"); - println("Content-Disposition: " + "attachment; filename=\"" + GXMailer.getEncodedString(fileName) + "\""); - println(""); - - int BUFFER_SIZE = 4096; - byte[] buffer = new byte[BUFFER_SIZE]; - OutputStream base64Output = new Base64OutputStream(outStream); - int n = is.read(buffer, 0, BUFFER_SIZE); - while (n >= 0) { - base64Output.write(buffer, 0, n); - n = is.read(buffer, 0, BUFFER_SIZE); - } - base64Output.flush(); - outStream.writeBytes(CRLF); - outStream.flush(); } private void println(String s) throws IOException