Search in sources :

Example 16 with SharedByteArrayInputStream

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

the class MimeTest method semiColonAddressSeparator.

@Test
public void semiColonAddressSeparator() throws Exception {
    StringBuilder to = new StringBuilder("To: ");
    int count = 4;
    for (int i = 1; i < count + 1; i++) {
        to.append("user").append(count).append("@example.com").append(";");
    }
    to.setLength(to.length() - 1);
    to.append("\r\n");
    String content = "From: user1@example.com\r\n" + to.toString() + "Subject: test\r\n" + "Content-Type: test/plain\r\n\r\n" + "test message";
    MimeMessage mm = new Mime.FixedMimeMessage(JMSession.getSession(), new SharedByteArrayInputStream(content.getBytes()));
    Address[] recipients = mm.getAllRecipients();
    Assert.assertEquals(count, recipients.length);
}
Also used : Address(javax.mail.Address) InternetAddress(javax.mail.internet.InternetAddress) MimeMessage(javax.mail.internet.MimeMessage) SharedByteArrayInputStream(javax.mail.util.SharedByteArrayInputStream) Test(org.junit.Test)

Example 17 with SharedByteArrayInputStream

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

the class ParseMimeMessage method setContent.

/**
     * The <mp>'s from the client and the MimeBodyParts in alternatives[] all want to be "content"
     * of this MimeMessage.  The alternatives[] all need to be "alternative" to whatever the client sends
     * us....but we want to be careful so that we do NOT create a nested multipart/alternative structure
     * within another one (that would be very tacky)....so this is a bit complicated.
     */
private static void setContent(MimeMessage mm, MimeMultipart mmp, Element elem, MimeBodyPart[] alternatives, ParseMessageContext ctxt) throws MessagingException, ServiceException, IOException {
    String type = elem.getAttribute(MailConstants.A_CONTENT_TYPE, MimeConstants.CT_DEFAULT).trim();
    ContentType ctype = new ContentType(type, ctxt.use2231).cleanup();
    // is the client passing us a multipart?
    if (ctype.getPrimaryType().equals("multipart")) {
        // handle multipart content separately...
        setMultipartContent(ctype, mm, mmp, elem, alternatives, ctxt);
        return;
    }
    Element inline = elem.getOptionalElement(MailConstants.E_ATTACH);
    if (inline != null) {
        handleAttachments(inline, mmp, ctxt, elem.getAttribute(MailConstants.A_CONTENT_ID, null), Part.INLINE);
        return;
    }
    if (alternatives != null) {
        // create a multipart/alternative to hold all the alternatives
        MimeMultipart mmpNew = new ZMimeMultipart("alternative");
        if (mmp == null) {
            mm.setContent(mmpNew);
        } else {
            MimeBodyPart mbpWrapper = new ZMimeBodyPart();
            mbpWrapper.setContent(mmpNew);
            mmp.addBodyPart(mbpWrapper);
        }
        mmp = mmpNew;
    }
    // once we get here, mmp is either NULL, a multipart/mixed from the toplevel,
    // or a multipart/alternative created just above....either way we are safe to stick
    // the client's nice and simple body right here
    String text = elem.getAttribute(MailConstants.E_CONTENT, "");
    byte[] raw = text.getBytes(Charsets.UTF_8);
    if (raw.length > 0 || !LC.mime_exclude_empty_content.booleanValue() || ctype.getPrimaryType().equals("text")) {
        ctxt.incrementSize("message body", raw.length);
        // if the user has specified an alternative charset, make sure it exists and can encode the content
        String charset = CharsetUtil.checkCharset(text, ctxt.defaultCharset);
        ctype.setCharset(charset).setParameter(MimeConstants.P_CHARSET, charset);
        Object content = ctype.getContentType().equals(ContentType.MESSAGE_RFC822) ? new ZMimeMessage(JMSession.getSession(), new SharedByteArrayInputStream(raw)) : text;
        if (mmp != null) {
            MimeBodyPart mbp = new ZMimeBodyPart();
            mbp.setContent(content, ctype.toString());
            mmp.addBodyPart(mbp);
        } else {
            mm.setContent(content, ctype.toString());
        }
    }
    if (alternatives != null) {
        for (int i = 0; i < alternatives.length; i++) {
            ctxt.incrementSize("alternative body", alternatives[i].getSize());
            mmp.addBodyPart(alternatives[i]);
        }
    }
}
Also used : ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) ContentType(com.zimbra.common.mime.ContentType) ZMimeMultipart(com.zimbra.common.zmime.ZMimeMultipart) MimeMultipart(javax.mail.internet.MimeMultipart) ZMimeBodyPart(com.zimbra.common.zmime.ZMimeBodyPart) Element(com.zimbra.common.soap.Element) ZMimeMultipart(com.zimbra.common.zmime.ZMimeMultipart) ZMimeBodyPart(com.zimbra.common.zmime.ZMimeBodyPart) MimeBodyPart(javax.mail.internet.MimeBodyPart) Mountpoint(com.zimbra.cs.mailbox.Mountpoint) SharedByteArrayInputStream(javax.mail.util.SharedByteArrayInputStream)

Example 18 with SharedByteArrayInputStream

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

the class TestUserServlet method verifyTarball.

private void verifyTarball(ZMailbox mbox, String relativePath, boolean hasMeta, boolean hasBody) throws Exception {
    InputStream in = mbox.getRESTResource(relativePath);
    TarInputStream tarIn = new TarInputStream(new GZIPInputStream(in), "UTF-8");
    TarEntry entry = null;
    boolean foundMeta = false;
    boolean foundMessage = false;
    while ((entry = tarIn.getNextEntry()) != null) {
        if (entry.getName().endsWith(".meta")) {
            Assert.assertTrue("Fround " + entry.getName(), hasMeta);
            foundMeta = true;
        }
        if (entry.getName().endsWith(".eml")) {
            byte[] content = new byte[(int) entry.getSize()];
            Assert.assertEquals(content.length, tarIn.read(content));
            MimeMessage message = new ZMimeMessage(JMSession.getSession(), new SharedByteArrayInputStream(content));
            byte[] body = ByteUtil.getContent(message.getInputStream(), 0);
            if (hasBody) {
                Assert.assertTrue(entry.getName() + " has no body", body.length > 0);
            } else {
                Assert.assertEquals(entry.getName() + " has a body", 0, body.length);
            }
            foundMessage = true;
        }
    }
    tarIn.close();
    Assert.assertTrue(foundMessage);
    if (hasMeta) {
        Assert.assertTrue(foundMeta);
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) MimeMessage(javax.mail.internet.MimeMessage) GZIPInputStream(java.util.zip.GZIPInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) TarInputStream(com.zimbra.common.util.tar.TarInputStream) ZipInputStream(java.util.zip.ZipInputStream) SharedByteArrayInputStream(javax.mail.util.SharedByteArrayInputStream) InputStream(java.io.InputStream) TarInputStream(com.zimbra.common.util.tar.TarInputStream) TarEntry(com.zimbra.common.util.tar.TarEntry) SharedByteArrayInputStream(javax.mail.util.SharedByteArrayInputStream)

Example 19 with SharedByteArrayInputStream

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

the class TestUserServlet method verifyZipFile.

private void verifyZipFile(ZMailbox mbox, String relativePath, boolean hasBody) throws Exception {
    InputStream in = mbox.getRESTResource(relativePath);
    ZipInputStream zipIn = new ZipInputStream(in);
    ZipEntry entry;
    boolean foundMessage = false;
    while ((entry = zipIn.getNextEntry()) != null) {
        if (entry.getName().endsWith(".eml")) {
            ByteArrayOutputStream buf = new ByteArrayOutputStream();
            ByteUtil.copy(zipIn, false, buf, true);
            byte[] content = buf.toByteArray();
            MimeMessage message = new ZMimeMessage(JMSession.getSession(), new SharedByteArrayInputStream(content));
            byte[] body = ByteUtil.getContent(message.getInputStream(), 0);
            if (hasBody) {
                Assert.assertTrue(entry.getName() + " has no body", body.length > 0);
            } else {
                Assert.assertEquals(entry.getName() + " has a body", 0, body.length);
            }
            foundMessage = true;
        }
    }
    zipIn.close();
    Assert.assertTrue(foundMessage);
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) MimeMessage(javax.mail.internet.MimeMessage) GZIPInputStream(java.util.zip.GZIPInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) TarInputStream(com.zimbra.common.util.tar.TarInputStream) ZipInputStream(java.util.zip.ZipInputStream) SharedByteArrayInputStream(javax.mail.util.SharedByteArrayInputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SharedByteArrayInputStream(javax.mail.util.SharedByteArrayInputStream)

Example 20 with SharedByteArrayInputStream

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

the class TestUtil method getHeaderValue.

public static String getHeaderValue(ZMailbox mbox, ZMessage msg, String headerName) throws Exception {
    String content = msg.getContent();
    if (content == null) {
        content = getContent(mbox, msg.getId());
    }
    assertNotNull("Content was not fetched from the server", content);
    MimeMessage mimeMsg = new ZMimeMessage(JMSession.getSession(), new SharedByteArrayInputStream(content.getBytes()));
    return mimeMsg.getHeader(headerName, null);
}
Also used : ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) MimeMessage(javax.mail.internet.MimeMessage) ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) SharedByteArrayInputStream(javax.mail.util.SharedByteArrayInputStream)

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