Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Close attachment streams only after they have been read #882

Merged
merged 1 commit into from
Aug 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 29 additions & 31 deletions gxmail/src/main/java/com/genexus/internet/SMTPSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading