From 0a6532f5bc46fa1c0499ff776d950cd341bfc49d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Sexenian?= <99925035+tomas-sexenian@users.noreply.github.com> Date: Wed, 8 May 2024 17:28:12 -0300 Subject: [PATCH] Fix attachment download and empty subject when working with POP3 --- .../genexus/internet/JapaneseMimeDecoder.java | 3 ++ .../genexus/internet/POP3SessionJavaMail.java | 40 +++++++++---------- 2 files changed, 22 insertions(+), 21 deletions(-) diff --git a/common/src/main/java/com/genexus/internet/JapaneseMimeDecoder.java b/common/src/main/java/com/genexus/internet/JapaneseMimeDecoder.java index 2b13db14b..e0aca35f4 100644 --- a/common/src/main/java/com/genexus/internet/JapaneseMimeDecoder.java +++ b/common/src/main/java/com/genexus/internet/JapaneseMimeDecoder.java @@ -8,6 +8,9 @@ public class JapaneseMimeDecoder public static String decode(String encoded) { + if (encoded == null) + return ""; + int strLen = encoded.length(); int currentIndex = 0; diff --git a/gxmail/src/main/java/com/genexus/internet/POP3SessionJavaMail.java b/gxmail/src/main/java/com/genexus/internet/POP3SessionJavaMail.java index 11b567451..791f9788d 100644 --- a/gxmail/src/main/java/com/genexus/internet/POP3SessionJavaMail.java +++ b/gxmail/src/main/java/com/genexus/internet/POP3SessionJavaMail.java @@ -1,11 +1,8 @@ package com.genexus.internet; -import java.io.BufferedInputStream; -import java.io.BufferedOutputStream; -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; +import java.io.*; +import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; import java.util.Enumeration; import java.util.Hashtable; import java.util.Properties; @@ -357,21 +354,22 @@ private String getAttachmentContentId(Part part) throws MessagingException private void saveFile(String filename, InputStream input) throws IOException { - File file = new File(attachmentsPath + filename); - BufferedOutputStream bos; - try (FileOutputStream fos = new FileOutputStream(file)) { - bos = new BufferedOutputStream(fos); - } - - BufferedInputStream bis = new BufferedInputStream(input); - int aByte; - while ((aByte = bis.read()) != -1) - { - bos.write(aByte); - } - bos.flush(); - bos.close(); - bis.close(); + try { + String encodedFilename = URLEncoder.encode(filename, StandardCharsets.UTF_8.toString()); + encodedFilename = encodedFilename.replace("+", "_"); + File file = new File(attachmentsPath + encodedFilename); + try (FileOutputStream fos = new FileOutputStream(file); + BufferedOutputStream bos = new BufferedOutputStream(fos); + BufferedInputStream bis = new BufferedInputStream(input)) { + int aByte; + while ((aByte = bis.read()) != -1) { + bos.write(aByte); + } + bos.flush(); + } + } catch (UnsupportedEncodingException e) { + throw new IOException("Error encoding the filename", e); + } } public String getNextUID() throws GXMailException