Search in sources :

Example 6 with ZMimeMessage

use of com.zimbra.common.zmime.ZMimeMessage in project zm-mailbox by Zimbra.

the class TestContentTransferEncoding method testBug98015.

/*
     * Tests that the message from bug 98015 retains the correct Content Transfer Encoding via the mechanism of
     * inferring it from the existing message referenced by the "origid" parameter.
     * This scenario also happens to test the case when a CTE header on a sub-part is inherited from the top-level.
     */
@Ignore("disabled until bug 98015 is fixed")
@Test
public void testBug98015() throws Exception {
    MimeMessage mimeMsg = new ZMimeMessage(JMSession.getSession(), new SharedByteArrayInputStream(getBug98015MimeString().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: QP bug");
    msgToSend.setMimePart(MimePartInfo.createForContentTypeAndContent("text/plain", "\n\n----- Forwarded Message -----\nSubject: QP Bug\n\n\\รถ/\nid=577281"));
    AttachmentsInfo attach = new AttachmentsInfo();
    attach.addAttachment(new MimePartAttachSpec(String.valueOf(msg.getId()), "2"));
    msgToSend.setAttachments(attach);
    SendMsgRequest req = new SendMsgRequest();
    req.setMsg(msgToSend);
    MimeMessage parsed = sendForwardedMessage(req, msg);
    ZMimeMultipart mmp = (ZMimeMultipart) parsed.getContent();
    assertEquals("8bit", mmp.getBodyPart(0).getHeader("Content-Transfer-Encoding")[0]);
    assertEquals("base64", mmp.getBodyPart(1).getHeader("Content-Transfer-Encoding")[0]);
}
Also used : AttachmentsInfo(com.zimbra.soap.mail.type.AttachmentsInfo) 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) 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) SendMsgRequest(com.zimbra.soap.mail.message.SendMsgRequest) DeliveryOptions(com.zimbra.cs.mailbox.DeliveryOptions) SharedByteArrayInputStream(javax.mail.util.SharedByteArrayInputStream) MimePartAttachSpec(com.zimbra.soap.mail.type.MimePartAttachSpec) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 7 with ZMimeMessage

use of com.zimbra.common.zmime.ZMimeMessage 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 8 with ZMimeMessage

use of com.zimbra.common.zmime.ZMimeMessage in project zm-mailbox by Zimbra.

the class SpamExtract method writeAttachedMessages.

private static void writeAttachedMessages(MimeMessage mm, File outdir, String msgUri) throws IOException, MessagingException {
    // Not raw - ignore the spam report and extract messages that are in attachments...
    if (!(mm.getContent() instanceof MimeMultipart)) {
        LOG.warn("Spam/notspam messages must have attachments (skipping " + msgUri + ")");
        return;
    }
    MimeMultipart mmp = (MimeMultipart) mm.getContent();
    int nAttachments = mmp.getCount();
    boolean foundAtleastOneAttachedMessage = false;
    for (int i = 0; i < nAttachments; i++) {
        BodyPart bp = mmp.getBodyPart(i);
        if (!bp.isMimeType("message/rfc822")) {
            // Let's ignore all parts that are not messages.
            continue;
        }
        foundAtleastOneAttachedMessage = true;
        // the actual message
        Part msg = (Part) bp.getContent();
        File file = new File(outdir, mOutputPrefix + "-" + mExtractIndex++);
        OutputStream os = null;
        try {
            os = new BufferedOutputStream(new FileOutputStream(file));
            if (msg instanceof MimeMessage) {
                //bug 74435 clone into newMsg so our parser has a chance to handle headers which choke javamail
                ZMimeMessage newMsg = new ZMimeMessage((MimeMessage) msg);
                newMsg.writeTo(os);
            } else {
                msg.writeTo(os);
            }
        } finally {
            os.close();
        }
        if (verbose)
            LOG.info("Wrote: " + file);
    }
    if (!foundAtleastOneAttachedMessage) {
        String msgid = mm.getHeader("Message-ID", " ");
        LOG.warn("message uri=" + msgUri + " message-id=" + msgid + " had no attachments");
    }
}
Also used : BodyPart(javax.mail.BodyPart) ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) MimeMessage(javax.mail.internet.MimeMessage) Part(javax.mail.Part) BodyPart(javax.mail.BodyPart) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream)

Example 9 with ZMimeMessage

use of com.zimbra.common.zmime.ZMimeMessage in project zm-mailbox by Zimbra.

the class TestImapClient method newTestMessage.

private static MimeMessage newTestMessage(int num) throws MessagingException {
    MimeMessage mm = new ZMimeMessage(JMSession.getSession());
    mm.setHeader("Message-Id", "<test-" + num + "@foo.com>");
    mm.setHeader("To", "nobody@foo.com");
    mm.setHeader("From", "nobody@bar.com");
    mm.setSubject("Test message " + num);
    mm.setSentDate(new Date(System.currentTimeMillis()));
    mm.setContent("This is test message " + num, "text/plain");
    return mm;
}
Also used : ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) MimeMessage(javax.mail.internet.MimeMessage) Date(java.util.Date)

Example 10 with ZMimeMessage

use of com.zimbra.common.zmime.ZMimeMessage in project zm-mailbox by Zimbra.

the class TestSmtpClient method testMimeMessage.

@Test
public void testMimeMessage() throws Exception {
    TestUtil.createAccount(USER_NAME);
    TestUtil.createAccount(USER2_NAME);
    // Assemble the message.
    MimeMessage mm = new ZMimeMessage(JMSession.getSession());
    InternetAddress addr = new JavaMailInternetAddress(TestUtil.getAddress(USER_NAME));
    mm.setFrom(addr);
    mm.setRecipient(RecipientType.TO, addr);
    String subject = NAME_PREFIX + " testMimeMessage";
    mm.setSubject(subject);
    mm.setText("testMimeMessage");
    mm.saveChanges();
    // Initialize SMTP client.
    SmtpConfig config = new SmtpConfig(mHost, mPort, "localhost");
    SmtpConnection conn = new SmtpConnection(config);
    conn.sendMessage(mm);
    // Make sure it arrived.
    ZMailbox mbox = TestUtil.getZMailbox(USER_NAME);
    TestUtil.waitForMessage(mbox, "in:inbox subject:\"" + subject + "\"");
    // Send the same message to a different envelope recipient.
    conn.sendMessage(addr.getAddress(), new String[] { TestUtil.getAddress(USER2_NAME) }, mm);
    mbox = TestUtil.getZMailbox(USER2_NAME);
    TestUtil.waitForMessage(mbox, "in:inbox subject:\"" + subject + "\"");
}
Also used : SmtpConfig(com.zimbra.cs.mailclient.smtp.SmtpConfig) SmtpConnection(com.zimbra.cs.mailclient.smtp.SmtpConnection) JavaMailInternetAddress(com.zimbra.common.mime.shim.JavaMailInternetAddress) InternetAddress(javax.mail.internet.InternetAddress) ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) ZMailbox(com.zimbra.client.ZMailbox) ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) MimeMessage(javax.mail.internet.MimeMessage) JavaMailInternetAddress(com.zimbra.common.mime.shim.JavaMailInternetAddress) Test(org.junit.Test)

Aggregations

ZMimeMessage (com.zimbra.common.zmime.ZMimeMessage)53 MimeMessage (javax.mail.internet.MimeMessage)50 Test (org.junit.Test)34 SharedByteArrayInputStream (javax.mail.util.SharedByteArrayInputStream)30 InputStream (java.io.InputStream)17 Session (javax.mail.Session)14 Transport (javax.mail.Transport)14 Mailbox (com.zimbra.cs.mailbox.Mailbox)13 ParsedMessage (com.zimbra.cs.mime.ParsedMessage)13 JMSession (com.zimbra.cs.util.JMSession)13 Message (com.zimbra.cs.mailbox.Message)12 Account (com.zimbra.cs.account.Account)10 MessagingException (javax.mail.MessagingException)10 OperationContext (com.zimbra.cs.mailbox.OperationContext)9 ItemId (com.zimbra.cs.service.util.ItemId)9 InternetAddress (javax.mail.internet.InternetAddress)9 JavaMailInternetAddress (com.zimbra.common.mime.shim.JavaMailInternetAddress)8 DeliveryContext (com.zimbra.cs.mailbox.DeliveryContext)8 Date (java.util.Date)7 ByteArrayInputStream (java.io.ByteArrayInputStream)6