From c0274d2b5a7bd5d9fac7afd485f614c756bf2fa0 Mon Sep 17 00:00:00 2001 From: Ignacio Tisnes Date: Mon, 1 Jul 2024 10:52:49 -0300 Subject: [PATCH 1/2] Fix file input stream closed when adding attachment --- .../com/genexus/internet/SMTPSession.java | 41 ++++++++++--------- 1 file changed, 21 insertions(+), 20 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..629ef6f9a 100644 --- a/gxmail/src/main/java/com/genexus/internet/SMTPSession.java +++ b/gxmail/src/main/java/com/genexus/internet/SMTPSession.java @@ -623,8 +623,27 @@ private void sendAttachment(String sTime, String fileNamePath, String attachment try { fis = new FileInputStream(attachmentPath + fileNamePath); is = fis; - } - catch (FileNotFoundException e) { + + 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(); + + base64Output.close(); + } catch (FileNotFoundException e) { log ("11 - FileNotFound " + e.getMessage()); throw new GXMailException("Can't find " + attachmentPath + fileNamePath, MAIL_InvalidAttachment); } finally { @@ -633,24 +652,6 @@ private void sendAttachment(String sTime, String fileNamePath, String attachment 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 From fa7d4bdaf9863fe3c8cf069108c91d97106975b4 Mon Sep 17 00:00:00 2001 From: Ignacio Tisnes Date: Mon, 1 Jul 2024 11:25:58 -0300 Subject: [PATCH 2/2] base64Output stream incorrectly closed (caused the socket to close) --- gxmail/src/main/java/com/genexus/internet/SMTPSession.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/gxmail/src/main/java/com/genexus/internet/SMTPSession.java b/gxmail/src/main/java/com/genexus/internet/SMTPSession.java index 629ef6f9a..2b50e0eb9 100644 --- a/gxmail/src/main/java/com/genexus/internet/SMTPSession.java +++ b/gxmail/src/main/java/com/genexus/internet/SMTPSession.java @@ -641,8 +641,6 @@ private void sendAttachment(String sTime, String fileNamePath, String attachment base64Output.flush(); outStream.writeBytes(CRLF); outStream.flush(); - - base64Output.close(); } catch (FileNotFoundException e) { log ("11 - FileNotFound " + e.getMessage()); throw new GXMailException("Can't find " + attachmentPath + fileNamePath, MAIL_InvalidAttachment);