Skip to content
Open
Show file tree
Hide file tree
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
6 changes: 3 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ java {
dependencies {

api 'org.jodd:jodd-util:6.0.+'
implementation 'jakarta.mail:jakarta.mail-api:2.1.2'
implementation 'org.eclipse.angus:jakarta.mail:2.0.2'
implementation 'jakarta.mail:jakarta.mail-api:2.1.5'
implementation 'org.eclipse.angus:jakarta.mail:2.0.5'

testImplementation 'org.junit.jupiter:junit-jupiter-api:5.9.+'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.9.+'
testImplementation 'com.icegreen:greenmail:2.0.0-alpha-3'
testImplementation 'com.icegreen:greenmail:2.1.8'
}

jar {
Expand Down
17 changes: 13 additions & 4 deletions src/main/java/jodd/mail/EmailAttachmentBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,11 @@ public EmailAttachmentBuilder embeddedMessage(final EmailMessage targetMessage)
*/
public <T extends DataSource> EmailAttachmentBuilder content(final T dataSource) {
this.dataSource = dataSource;
name(dataSource.getName());
// FileDataSource backed by a file in attachmentStorage may use an internal filename like {messageid}-{attCount}.
// So only set this public name implicitly if it was not set explicitly already.
if (name == null){
name(dataSource.getName());
}
return this;
}

Expand Down Expand Up @@ -224,14 +228,13 @@ public EmailAttachment<ByteArrayDataSource> buildByteArrayDataSource() throws Ma
* @return {@link EmailAttachment}.
* @throws MailException if issue with {@link DataSource}.
*/
public EmailAttachment<FileDataSource> buildFileDataSource(final String messageId, final File attachmentStorage) throws MailException {
public EmailAttachment<FileDataSource> buildFileDataSource(final String fileName, final File attachmentStorage) throws MailException {
try {
final FileDataSource fds;
if (dataSource instanceof FileDataSource) {
fds = (FileDataSource) dataSource;
} else {
final File file = new File(attachmentStorage, sanitizeFileName(messageId));
FileUtil.writeStream(file, dataSource.getInputStream());
final File file = writeToAttachmentStore(dataSource.getInputStream(), attachmentStorage, fileName);
fds = new FileDataSource(file);
}
checkDataSource();
Expand All @@ -241,6 +244,12 @@ public EmailAttachment<FileDataSource> buildFileDataSource(final String messageI
}
}

File writeToAttachmentStore(InputStream is, File attachmentStorage, String fileName) throws IOException {
File file = new File(attachmentStorage, sanitizeFileName(fileName));
FileUtil.writeStream(file, is);
return file;
}

/**
* Check to ensure {@link DataSource} ds is valid.
*
Expand Down
13 changes: 9 additions & 4 deletions src/main/java/jodd/mail/ReceivedEmail.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

package jodd.mail;

import jakarta.activation.FileDataSource;
import jakarta.mail.Address;
import jakarta.mail.Flags;
import jakarta.mail.Message;
Expand Down Expand Up @@ -443,12 +444,16 @@ public Date receivedDate() {
*/
private ReceivedEmail addAttachment(final Part part, final InputStream content, final File attachmentStorage) throws MessagingException, IOException {
final EmailAttachmentBuilder builder = addAttachmentInfo(part);
builder.content(content, part.getContentType());
if (attachmentStorage != null) {
final String name = sanitizeFileName(messageId) + "-" + (this.attachments().size() + 1);
return storeAttachment(builder.buildFileDataSource(name, attachmentStorage));
// If we have an attachmentStorage, we can save memory by consequently using FileDataSource.
final String attStoreFileName = sanitizeFileName(messageId) + "-" + (this.attachments().size() + 1);
final File attStoreFile = builder.writeToAttachmentStore(content, attachmentStorage, attStoreFileName);
builder.content(new FileDataSource(attStoreFile));
return storeAttachment(builder.buildFileDataSource(attStoreFileName, attachmentStorage));
} else {
builder.content(content, part.getContentType());
return storeAttachment(builder.buildByteArrayDataSource());
}
return storeAttachment(builder.buildByteArrayDataSource());
}

/**
Expand Down
7 changes: 3 additions & 4 deletions src/test/java/jodd/mail/AttachmentStorageTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,10 @@ void stopGreenMailInstance() {
}

@Test
@EnabledOnOs(value = {OS.AIX, OS.LINUX, OS.MAC, OS.SOLARIS})
@EnabledOnOs(value = {OS.AIX, OS.LINUX, OS.MAC, OS.SOLARIS, OS.WINDOWS})
void testAttachmentStorage() throws Exception {
// storing files with its message-id as file name fails on windows because value of the message-id consists of brackets
// file names with brackets are not valid on window hosts
// see https://tools.ietf.org/html/rfc5322#section-3.6.4
// Used to be deactivated for Windows because of <> brackets, but filename sanitation
// was implemented in https://github.com/oblac/jodd-mail/issues/19

final SmtpServer smtpServer = MailServer.create()
.host(LOCALHOST)
Expand Down