Skip to content

Commit

Permalink
Add a test for an attachment with UTF-8 symbols
Browse files Browse the repository at this point in the history
  • Loading branch information
boris-petrov committed Dec 26, 2022
1 parent c206572 commit 4d3f514
Showing 1 changed file with 47 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,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 @@ -189,6 +190,52 @@ public void messagesAdded(MessageCountEvent e) {
}
}

@Test
public void testAttachmentWithUTF8NameAndGreenMailApi() throws MessagingException, IOException {
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);
new Thread(() -> {
try {
Thread.sleep(100);
} catch (InterruptedException e1) {
// Ignore
}
try {
String fileName = MimeUtility.encodeText("кирилица testimage_ünicöde_\uD83C\uDF36");
GreenMailUtil.sendAttachmentEmail(
"to@localhost", "from@localhost", "subject", "body",
new byte[]{0, 1, 2}, "image/gif", fileName,
"testimage_description", greenMail.getSmtp().getServerSetup());
} catch (UnsupportedEncodingException ex) {
assertThat(false).isTrue();
}
}).start();
((IMAPFolder) inboxFolder).idle(true);

assertThat(messages[0].getContent() != null).isTrue();

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

0 comments on commit 4d3f514

Please sign in to comment.