(geronimo-mail) 01/01: GERONIMO-6884 - Try to reproduce
[email protected] Thu, 06 Nov 2025 09:46:03 +0000
| Newsgroups | gmane.comp.java.geronimo.cvs |
|---|---|
| Message-ID | <[email protected]> |
This is an automated email from the ASF dual-hosted git repository. rzo1 pushed a commit to branch GERONIMO6884 in repository https://gitbox.apache.org/repos/asf/geronimo-mail.git commit 91f95b57a96061e9f5f57f24be88716ffdc41c29 Author: Richard Zowalla <[email protected]> AuthorDate: Thu Nov 6 10:45:49 2025 +0100 GERONIMO-6884 - Try to reproduce --- .../geronimo/mail/issues/GERONIMO6884Test.java | 112 +++++++++++++++++++++ .../src/test/resources/test-img.png | Bin 0 -> 22642 bytes 2 files changed, 112 insertions(+) diff --git a/geronimo-mail_2.1_impl/geronimo-mail_2.1_provider/src/test/java/org/apache/geronimo/mail/issues/GERONIMO6884Test.java b/geronimo-mail_2.1_impl/geronimo-mail_2.1_provider/src/test/java/org/apache/geronimo/mail/issues/GERONIMO6884Test.java new file mode 100644 index 0000000..645f4d2 --- /dev/null +++ b/geronimo-mail_2.1_impl/geronimo-mail_2.1_provider/src/test/java/org/apache/geronimo/mail/issues/GERONIMO6884Test.java @@ -0,0 +1,112 @@ +package org.apache.geronimo.mail.issues; + +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import jakarta.activation.DataHandler; +import jakarta.mail.BodyPart; +import jakarta.mail.Folder; +import jakarta.mail.Message; +import jakarta.mail.Multipart; +import jakarta.mail.Session; +import jakarta.mail.Store; +import jakarta.mail.Transport; +import jakarta.mail.internet.InternetAddress; +import jakarta.mail.internet.MimeBodyPart; +import jakarta.mail.internet.MimeMessage; +import jakarta.mail.internet.MimeMultipart; +import jakarta.mail.util.ByteArrayDataSource; +import org.apache.geronimo.mail.testserver.AbstractProtocolTest; + +import java.io.ByteArrayOutputStream; +import java.io.InputStream; +import java.util.Properties; + +import static org.junit.Assert.assertArrayEquals; + +public class GERONIMO6884Test extends AbstractProtocolTest { + public void testGERONIMO6884() throws Exception { + + final MimeBodyPart messageBodyPart = new MimeBodyPart(); + messageBodyPart.setText("Text body!"); + + byte[] bytes = new byte[47100]; + try (InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("test-img.png")) { + is.read(bytes); + } + + final MimeBodyPart attachmentPart = new MimeBodyPart(); + attachmentPart.setDataHandler(new DataHandler(new ByteArrayDataSource(bytes, "image/png"))); + attachmentPart.setHeader("Content-Transfer-Encoding", "base64"); // crucial + + final Multipart multipart = new MimeMultipart(); + multipart.addBodyPart(messageBodyPart); + multipart.addBodyPart(attachmentPart); + + final Message message = sendAndGetMessage(multipart); + + final MimeMultipart content = (MimeMultipart) message.getContent(); + final BodyPart attachment = content.getBodyPart(1); + + final InputStream is = (InputStream) attachment.getContent(); + byte[] attachmentBytes; + try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) { + byte[] buffer = new byte[8192]; + int bytesRead; + while ((bytesRead = is.read(buffer)) != -1) { + baos.write(buffer, 0, bytesRead); + } + attachmentBytes = baos.toByteArray(); + } + assertArrayEquals(bytes, attachmentBytes); + } + + private Message sendAndGetMessage(Multipart multipart) throws Exception { + + start(); + Properties props = new Properties(); + props.setProperty("mail.transport.protocol", "smtp"); + props.setProperty("mail.store.protocol", "imap"); + props.setProperty("mail.imap.port", String.valueOf(imapConf.getListenerPort())); + props.setProperty("mail.smtp.port", String.valueOf(smtpConf.getListenerPort())); + //props.setProperty("mail.debug", "true"); + Session session = Session.getInstance(props); + + Message message = new MimeMessage(session); + message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("[email protected]")); + message.setSubject("Test attachment content-type"); + message.setContent(multipart); + + Transport.send(message); + + return getAttachmentContentType(session); + } + + private Message getAttachmentContentType(Session session) throws Exception { + final Store store = session.getStore(); + store.connect("127.0.0.1", "serveruser", "serverpass"); + + Folder folder = store.getDefaultFolder(); + folder = folder.getFolder("inbox"); + folder.open(Folder.READ_ONLY); + + server.ensureMsgCount(1); + + return folder.getMessage(1); + } + +} diff --git a/geronimo-mail_2.1_impl/geronimo-mail_2.1_provider/src/test/resources/test-img.png b/geronimo-mail_2.1_impl/geronimo-mail_2.1_provider/src/test/resources/test-img.png new file mode 100644 index 0000000..35ab855 Binary files /dev/null and b/geronimo-mail_2.1_impl/geronimo-mail_2.1_provider/src/test/resources/test-img.png differ