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

Add a test for an attachment with UTF-8 symbols #483

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;

Expand Down Expand Up @@ -198,6 +199,55 @@ public void messagesAdded(MessageCountEvent e) {
}
}

@Test
public void testAttachmentWithUTF8NameAndGreenMailApi() throws MessagingException, IOException {
System.setProperty("mail.mime.decodefilename", "true");

greenMail.setUser("to@localhost", "pwd");
final IMAPStore store = greenMail.getImap().createStore();
store.connect("to@localhost", "pwd");
try {
Folder inboxFolder = store.getFolder("INBOX");
inboxFolder.open(Folder.READ_ONLY);
Message[] messages = new Message[] { null };
MessageCountListener listener = new MessageCountListener() {
@Override
public void messagesRemoved(MessageCountEvent e) {
}

@Override
public void messagesAdded(MessageCountEvent e) {
messages[0] = e.getMessages()[0];
}
};
inboxFolder.addMessageCountListener(listener);
String fileName = "кирилица testimage_ünicöde_\uD83C\uDF36";
new Thread(() -> {
try {
Thread.sleep(100);
} catch (InterruptedException e1) {
// Ignore
}
try {
GreenMailUtil.sendAttachmentEmail(
"to@localhost", "from@localhost", "subject", "body",
new byte[]{0, 1, 2}, "image/gif", MimeUtility.encodeText(fileName),
"testimage_description", greenMail.getSmtp().getServerSetup());
} catch (UnsupportedEncodingException ex) {
assertThat(false).isTrue();
}
}).start();
((IMAPFolder) inboxFolder).idle(true);

assertThat(messages[0].getContent() != null).isTrue();
assertThat(((Multipart) messages[0].getContent()).getBodyPart(1).getFileName()).isEqualTo(fileName);

inboxFolder.close();
} finally {
store.close();
}
}

private void sendMessage(InternetAddress fromAddress, InternetAddress toAddress) throws MessagingException {
final Session session = greenMail.getSmtp().createSession();
MimeMessage message = new MimeMessage(session);
Expand Down