Search in sources :

Example 11 with SharedByteArrayInputStream

use of javax.mail.util.SharedByteArrayInputStream in project zm-mailbox by Zimbra.

the class TestContentTransferEncoding method testMultipartMimeMessage.

/*
     * This tests the CTE header of a forwarded message being inferred from the existing message when the message is a multipart message.
     */
@Ignore("disabled until bug 98015 is fixed")
@Test
public void testMultipartMimeMessage() throws Exception {
    Mailbox mbox = MailboxManager.getInstance().getMailboxByAccountId(MockProvisioning.DEFAULT_ACCOUNT_ID);
    MimeMessage mimeMsg = new ZMimeMessage(JMSession.getSession(), new SharedByteArrayInputStream(getMultipartMimeString().getBytes()));
    ParsedMessage pm = new ParsedMessage(mimeMsg, true);
    DeliveryOptions dopt = new DeliveryOptions().setFolderId(Mailbox.ID_FOLDER_INBOX);
    Message msg = mbox.addMessage(null, pm, dopt, null);
    MsgToSend msgToSend = new MsgToSend();
    msgToSend.setOrigId(String.valueOf(msg.getId()));
    msgToSend.setReplyType("w");
    msgToSend.setSubject("Fwd: Multipart Test");
    MimePartInfo mpi = new MimePartInfo();
    mpi.setContentType("multipart/alternative");
    List<MimePartInfo> mimeParts = new LinkedList<MimePartInfo>();
    mimeParts.add(MimePartInfo.createForContentTypeAndContent("text/plain", "multipart test"));
    mimeParts.add(MimePartInfo.createForContentTypeAndContent("text/html", "multipart test"));
    mpi.setMimeParts(mimeParts);
    msgToSend.setMimePart(mpi);
    SendMsgRequest req = new SendMsgRequest();
    req.setMsg(msgToSend);
    MimeMessage parsed = sendForwardedMessage(req, msg);
    ZMimeMultipart mmp = (ZMimeMultipart) parsed.getContent();
    assertEquals("test1", mmp.getBodyPart(0).getHeader("Content-Transfer-Encoding")[0]);
    assertEquals("test2", mmp.getBodyPart(1).getHeader("Content-Transfer-Encoding")[0]);
}
Also used : ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) Message(com.zimbra.cs.mailbox.Message) MimeMessage(javax.mail.internet.MimeMessage) ParseMimeMessage(com.zimbra.cs.service.mail.ParseMimeMessage) SendMsgRequest(com.zimbra.soap.mail.message.SendMsgRequest) LinkedList(java.util.LinkedList) SharedByteArrayInputStream(javax.mail.util.SharedByteArrayInputStream) Mailbox(com.zimbra.cs.mailbox.Mailbox) ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) MimeMessage(javax.mail.internet.MimeMessage) ParseMimeMessage(com.zimbra.cs.service.mail.ParseMimeMessage) ZMimeMultipart(com.zimbra.common.zmime.ZMimeMultipart) MsgToSend(com.zimbra.soap.mail.type.MsgToSend) MimePartInfo(com.zimbra.soap.mail.type.MimePartInfo) DeliveryOptions(com.zimbra.cs.mailbox.DeliveryOptions) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 12 with SharedByteArrayInputStream

use of javax.mail.util.SharedByteArrayInputStream in project zm-mailbox by Zimbra.

the class FixedMimeMessageTest method contentTransferEncoding.

@Test
public void contentTransferEncoding() throws Exception {
    String raw = "From: sender@zimbra.com\n" + "To: recipient@zimbra.com\n" + "Subject: test\n" + "Content-Type: text/plain; charset=ISO-2022-JP\n" + "\n" + "あああいいいうううえええおおお";
    MimeMessage message = new FixedMimeMessage(JMSession.getSession(), new SharedByteArrayInputStream(raw.getBytes(Charsets.UTF_8)));
    Assert.assertNull(message.getEncoding());
    message.setHeader("X-TEST", "test");
    message.saveChanges();
    //        Assert.assertNull(message.getEncoding());
    message = new FixedMimeMessage(JMSession.getSession());
    message.setHeader("X-TEST", "test");
    message.setText("あああいいいうううえええおおお", "ISO-2022-JP");
    message.saveChanges();
    Assert.assertEquals("7bit", message.getEncoding());
    message = new FixedMimeMessage(JMSession.getSession());
    message.setHeader("X-TEST", "test");
    message.setText("あああいいいうううえええおおお", "UTF-8");
    message.saveChanges();
    Assert.assertEquals("8bit", message.getEncoding());
}
Also used : FixedMimeMessage(com.zimbra.cs.mime.Mime.FixedMimeMessage) MimeMessage(javax.mail.internet.MimeMessage) FixedMimeMessage(com.zimbra.cs.mime.Mime.FixedMimeMessage) SharedByteArrayInputStream(javax.mail.util.SharedByteArrayInputStream) Test(org.junit.Test)

Example 13 with SharedByteArrayInputStream

use of javax.mail.util.SharedByteArrayInputStream in project zm-mailbox by Zimbra.

the class MimeTest method textCid.

@Test
public void textCid() throws Exception {
    String content = baseMpMixedContent + "------------1111971890AC3BB91\r\n" + "Content-Type: text/html; charset=windows-1250\r\n" + "Content-Transfer-Encoding: quoted-printable\r\n" + //barely valid, but we shouldn't break if a bad agent sends like this
    "Content-ID: <text_testemail>\r\n\r\n" + "<html>Email with img<img src=\"cid:12345_testemail\"/></html>\r\n" + "------------1111971890AC3BB91\r\n" + "Content-Type: image/jpeg;\r\n" + "name=\"img.jpg\"\r\n" + "Content-Transfer-Encoding: base64\r\n" + "Content-ID: <12345_testemail>\r\n\r\n" + //obviously not a real image
    "R0a1231312ad124svsdsal==";
    MimeMessage mm = new Mime.FixedMimeMessage(JMSession.getSession(), new SharedByteArrayInputStream(content.getBytes()));
    List<MPartInfo> parts = Mime.getParts(mm);
    Assert.assertNotNull(parts);
    Assert.assertEquals(3, parts.size());
    MPartInfo mpart = parts.get(0);
    Assert.assertEquals("multipart/mixed", mpart.getContentType());
    List<MPartInfo> children = mpart.getChildren();
    Assert.assertEquals(2, children.size());
    Set<MPartInfo> bodies = Mime.getBody(parts, false);
    Assert.assertEquals(1, bodies.size());
    MPartInfo body = bodies.iterator().next();
    Assert.assertEquals("text/html", body.getContentType());
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) SharedByteArrayInputStream(javax.mail.util.SharedByteArrayInputStream) Test(org.junit.Test)

Example 14 with SharedByteArrayInputStream

use of javax.mail.util.SharedByteArrayInputStream in project zm-mailbox by Zimbra.

the class MimeTest method fileAsStream.

private void fileAsStream(String extension, boolean expectText) throws MessagingException, IOException {
    if (extension.charAt(0) == '.') {
        extension = extension.substring(1);
    }
    String content = "From: user1@example.com\r\n" + "To: user2@example.com\r\n" + "Subject: test\r\n" + "Content-Type: application/octet-stream;name=\"test." + extension + "\"\r\n" + "Content-Transfer-Encoding: base64\r\n\r\n" + //obviously not a real file
    "R0a1231312ad124svsdsal==";
    MimeMessage mm = new Mime.FixedMimeMessage(JMSession.getSession(), new SharedByteArrayInputStream(content.getBytes()));
    MimePart part = Mime.getMimePart(mm, "1");
    String expectedType = expectText ? "text/plain" : "application/octet-stream";
    Assert.assertEquals(expectedType, Mime.getContentType(part.getContentType()));
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) MimePart(javax.mail.internet.MimePart) SharedByteArrayInputStream(javax.mail.util.SharedByteArrayInputStream)

Example 15 with SharedByteArrayInputStream

use of javax.mail.util.SharedByteArrayInputStream in project zm-mailbox by Zimbra.

the class MimeTest method multiTextBody.

@Test
public void multiTextBody() throws Exception {
    StringBuilder content = new StringBuilder(baseMpMixedContent);
    int count = 42;
    for (int i = 0; i < count; i++) {
        content.append("------------1111971890AC3BB91\r\n").append("Content-Type: text/html; charset=windows-1250\r\n").append("Content-Transfer-Encoding: quoted-printable\r\n\r\n").append("<html>Body ").append(i).append("</html>\r\n");
    }
    MimeMessage mm = new Mime.FixedMimeMessage(JMSession.getSession(), new SharedByteArrayInputStream(content.toString().getBytes()));
    List<MPartInfo> parts = Mime.getParts(mm);
    Assert.assertNotNull(parts);
    Assert.assertEquals(count + 1, parts.size());
    MPartInfo mpart = parts.get(0);
    Assert.assertEquals("multipart/mixed", mpart.getContentType());
    List<MPartInfo> children = mpart.getChildren();
    Assert.assertEquals(count, children.size());
    Set<MPartInfo> bodies = Mime.getBody(parts, false);
    Assert.assertEquals(count, bodies.size());
    for (MPartInfo body : bodies) {
        Assert.assertEquals("text/html", body.getContentType());
        Object mimeContent = body.getMimePart().getContent();
        Assert.assertTrue(mimeContent instanceof String);
        String string = (String) mimeContent;
        int idx = string.indexOf("" + (body.getPartNum() - 1));
        //body 0 is part 1, b1 is p2, and so on
        Assert.assertTrue(idx > 0);
    }
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) SharedByteArrayInputStream(javax.mail.util.SharedByteArrayInputStream) Test(org.junit.Test)

Aggregations

SharedByteArrayInputStream (javax.mail.util.SharedByteArrayInputStream)61 MimeMessage (javax.mail.internet.MimeMessage)53 Test (org.junit.Test)47 ZMimeMessage (com.zimbra.common.zmime.ZMimeMessage)31 Session (javax.mail.Session)15 JMSession (com.zimbra.cs.util.JMSession)14 Transport (javax.mail.Transport)14 InputStream (java.io.InputStream)9 ParsedMessage (com.zimbra.cs.mime.ParsedMessage)7 ByteBuilder (com.zimbra.common.zmime.ZMimeUtility.ByteBuilder)6 Mailbox (com.zimbra.cs.mailbox.Mailbox)6 FixedMimeMessage (com.zimbra.cs.mime.Mime.FixedMimeMessage)6 MessagingException (javax.mail.MessagingException)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 MimeMultipart (javax.mail.internet.MimeMultipart)5 ZMimeMultipart (com.zimbra.common.zmime.ZMimeMultipart)4 DeliveryOptions (com.zimbra.cs.mailbox.DeliveryOptions)4 Message (com.zimbra.cs.mailbox.Message)4 ParseMimeMessage (com.zimbra.cs.service.mail.ParseMimeMessage)4 SendMsgRequest (com.zimbra.soap.mail.message.SendMsgRequest)4