Search in sources :

Example 56 with SharedByteArrayInputStream

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

the class MimeTest method testFullContentType.

@Test
public void testFullContentType() throws Exception {
    String content = "From: user1@example.com\r\n" + "To: user2@example.com\r\n" + "Subject: test\r\n" + "Content-Type: text/plain;param=foo\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");
    Assert.assertEquals("text/plain;param=foo", part.getContentType());
    List<MPartInfo> parts = Mime.getParts(mm);
    Assert.assertNotNull(parts);
    Assert.assertEquals(1, parts.size());
    MPartInfo info = parts.get(0);
    Assert.assertEquals("text/plain", info.getContentType());
    Assert.assertEquals("text/plain;param=foo", info.getFullContentType());
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) MimePart(javax.mail.internet.MimePart) SharedByteArrayInputStream(javax.mail.util.SharedByteArrayInputStream) Test(org.junit.Test)

Example 57 with SharedByteArrayInputStream

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

the class TestMailSender method testRejectRecipient.

@Test
public void testRejectRecipient() throws Exception {
    String errorMsg = "Sender address rejected: User unknown in relay recipient table";
    String bogusAddress = TestUtil.getAddress("bogus");
    startDummySmtpServer(bogusAddress, errorMsg);
    Server server = Provisioning.getInstance().getLocalServer();
    server.setSmtpPort(TEST_SMTP_PORT);
    String content = TestUtil.getTestMessage(NAME_PREFIX + " testRejectSender", bogusAddress, SENDER_NAME, null);
    MimeMessage msg = new ZMimeMessage(JMSession.getSession(), new SharedByteArrayInputStream(content.getBytes()));
    Mailbox mbox = TestUtil.getMailbox(SENDER_NAME);
    // Test reject first recipient, get partial send value from LDAP.
    boolean sendFailed = false;
    server.setSmtpSendPartial(false);
    try {
        mbox.getMailSender().sendMimeMessage(null, mbox, msg);
    } catch (MailServiceException e) {
        validateException(e, MailServiceException.SEND_ABORTED_ADDRESS_FAILURE, bogusAddress, errorMsg);
        sendFailed = true;
    }
    Assert.assertTrue(sendFailed);
    // Test reject first recipient, set partial send value explicitly.
    startDummySmtpServer(bogusAddress, errorMsg);
    sendFailed = false;
    server.setSmtpSendPartial(true);
    MailSender sender = mbox.getMailSender().setSendPartial(false);
    try {
        sender.sendMimeMessage(null, mbox, msg);
    } catch (MailServiceException e) {
        validateException(e, MailServiceException.SEND_ABORTED_ADDRESS_FAILURE, bogusAddress, errorMsg);
        sendFailed = true;
    }
    Assert.assertTrue(sendFailed);
    // Test reject second recipient, get partial send value from LDAP.
    startDummySmtpServer(bogusAddress, errorMsg);
    sendFailed = false;
    String validAddress = TestUtil.getAddress(RECIPIENT_NAME);
    InternetAddress[] recipients = new InternetAddress[2];
    recipients[0] = new JavaMailInternetAddress(validAddress);
    recipients[1] = new JavaMailInternetAddress(bogusAddress);
    msg.setRecipients(MimeMessage.RecipientType.TO, recipients);
    server.setSmtpSendPartial(false);
    try {
        mbox.getMailSender().sendMimeMessage(null, mbox, msg);
    } catch (MailServiceException e) {
        validateException(e, MailServiceException.SEND_ABORTED_ADDRESS_FAILURE, bogusAddress, errorMsg);
        sendFailed = true;
    }
    Assert.assertTrue(sendFailed);
    // Test partial send, get value from LDAP.
    startDummySmtpServer(bogusAddress, errorMsg);
    server.setSmtpSendPartial(true);
    sendFailed = false;
    try {
        mbox.getMailSender().sendMimeMessage(null, mbox, msg);
    } catch (MailServiceException e) {
        validateException(e, MailServiceException.SEND_PARTIAL_ADDRESS_FAILURE, bogusAddress, null);
        sendFailed = true;
    }
    Assert.assertTrue(sendFailed);
    // Test partial send, specify value explicitly.
    server.setSmtpSendPartial(false);
    startDummySmtpServer(bogusAddress, errorMsg);
    sendFailed = false;
    sender = mbox.getMailSender().setSendPartial(true);
    try {
        sender.sendMimeMessage(null, mbox, msg);
    } catch (MailServiceException e) {
        // Don't check error message.  JavaMail does not give us the SMTP protocol error in the
        // partial send case.
        validateException(e, MailServiceException.SEND_PARTIAL_ADDRESS_FAILURE, bogusAddress, null);
        sendFailed = true;
    }
    Assert.assertTrue(sendFailed);
}
Also used : JavaMailInternetAddress(com.zimbra.common.mime.shim.JavaMailInternetAddress) InternetAddress(javax.mail.internet.InternetAddress) ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) Server(com.zimbra.cs.account.Server) Mailbox(com.zimbra.cs.mailbox.Mailbox) ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) MimeMessage(javax.mail.internet.MimeMessage) FixedMimeMessage(com.zimbra.cs.mime.Mime.FixedMimeMessage) JavaMailInternetAddress(com.zimbra.common.mime.shim.JavaMailInternetAddress) MailSender(com.zimbra.cs.mailbox.MailSender) MailServiceException(com.zimbra.cs.mailbox.MailServiceException) SharedByteArrayInputStream(javax.mail.util.SharedByteArrayInputStream) Test(org.junit.Test)

Example 58 with SharedByteArrayInputStream

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

the class TestMessageIntercept method compareContent.

/**
     * Confirm that the message attached to the intercept message matches the original.
     */
private void compareContent(ZMailbox tappedMbox, ZMessage tappedMsg, ZMailbox interceptorMbox, ZMessage interceptMsg) throws Exception {
    String relativeUrl = String.format("?id=%s&part=2", interceptMsg.getId());
    InputStream in = interceptorMbox.getRESTResource(relativeUrl);
    String interceptedMsgContent = new String(ByteUtil.getContent(in, -1)).trim();
    String tappedMsgContent = TestUtil.getContent(tappedMbox, tappedMsg.getId()).trim();
    Account account = TestUtil.getAccount(RECIPIENT_NAME);
    // Compare headers
    MimeMessage tappedMimeMsg = new ZMimeMessage(JMSession.getSession(), new SharedByteArrayInputStream(tappedMsgContent.getBytes()));
    MimeMessage interceptedMimeMsg = new ZMimeMessage(JMSession.getSession(), new SharedByteArrayInputStream(interceptedMsgContent.getBytes()));
    boolean headersOnly = account.getBooleanAttr(Provisioning.A_zimbraInterceptSendHeadersOnly, false);
    Set<String> tappedHeaderLines = getHeaderLines(tappedMimeMsg);
    Set<String> interceptedHeaderLines = getHeaderLines(interceptedMimeMsg);
    tappedHeaderLines.removeAll(getHeaderLines(interceptedMimeMsg));
    interceptedHeaderLines.removeAll(getHeaderLines(tappedMimeMsg));
    String context = "Unexpected headers found.  tapped: " + StringUtil.join(",", tappedHeaderLines) + ".  intercepted: " + StringUtil.join(",", interceptedHeaderLines) + ".";
    assertTrue(context, tappedHeaderLines.size() == 0 && interceptedHeaderLines.size() == 0);
    // Compare body
    if (headersOnly) {
        String interceptedBody = new String(ByteUtil.getContent(interceptedMimeMsg.getInputStream(), 0));
        if (interceptedBody != null) {
            interceptedBody = interceptedBody.trim();
        }
        assertTrue("Unexpected body: '" + interceptedBody + "'", interceptedBody == null || interceptedBody.length() == 0);
    } else {
        TestUtil.assertMessageContains(tappedMsgContent, interceptedMsgContent);
    }
}
Also used : Account(com.zimbra.cs.account.Account) ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) MimeMessage(javax.mail.internet.MimeMessage) SharedByteArrayInputStream(javax.mail.util.SharedByteArrayInputStream) InputStream(java.io.InputStream) SharedByteArrayInputStream(javax.mail.util.SharedByteArrayInputStream)

Example 59 with SharedByteArrayInputStream

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

the class TestParsedMessage method testParsedMessage.

@Test
public void testParsedMessage() throws Exception {
    ExpectedResults expected = new ExpectedResults();
    String subject = NAME_PREFIX + " testParsedMessage";
    expected.convertedSubject = subject;
    expected.rawContent = TestUtil.getTestMessage(subject, RECIPIENT_NAME, SENDER_NAME, null);
    expected.wasMutated = false;
    // Test ParsedMessage created from byte[]
    ParsedMessage pm = new ParsedMessage(expected.rawContent.getBytes(), false);
    verifyParsedMessage(pm, expected);
    pm = new ParsedMessage(expected.rawContent.getBytes(), true);
    verifyParsedMessage(pm, expected);
    // Test ParsedMessage created from File
    mFile = File.createTempFile("TestParsedMessage", ".msg");
    FileOutputStream out = new FileOutputStream(mFile);
    out.write(expected.rawContent.getBytes());
    pm = new ParsedMessage(mFile, null, false);
    verifyParsedMessage(pm, expected);
    pm = new ParsedMessage(mFile, null, true);
    verifyParsedMessage(pm, expected);
    // Test ParsedMessage created from MimeMessage.
    MimeMessage mimeMsg = new ZMimeMessage(JMSession.getSession(), new SharedByteArrayInputStream(expected.rawContent.getBytes()));
    pm = new ParsedMessage(mimeMsg, false);
    verifyParsedMessage(pm, expected);
    pm = new ParsedMessage(mimeMsg, true);
    verifyParsedMessage(pm, expected);
}
Also used : ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) MimeMessage(javax.mail.internet.MimeMessage) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) FileOutputStream(java.io.FileOutputStream) SharedByteArrayInputStream(javax.mail.util.SharedByteArrayInputStream) Test(org.junit.Test)

Example 60 with SharedByteArrayInputStream

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

the class TestParsedMessage method testMimeConverter.

@Test
public void testMimeConverter() throws Exception {
    MimeVisitor.registerConverter(TestMimeVisitor.class);
    ExpectedResults expected = new ExpectedResults();
    String subject = NAME_PREFIX + " testMimeConverter oldsubject";
    expected.convertedSubject = NAME_PREFIX + " testMimeConverter newsubject";
    expected.rawContent = TestUtil.getTestMessage(subject, RECIPIENT_NAME, SENDER_NAME, null);
    expected.wasMutated = false;
    // Test ParsedMessage created from byte[]
    ParsedMessage pm = new ParsedMessage(expected.rawContent.getBytes(), false);
    verifyParsedMessage(pm, expected);
    pm = new ParsedMessage(expected.rawContent.getBytes(), true);
    verifyParsedMessage(pm, expected);
    // Test ParsedMessage created from File
    mFile = File.createTempFile("TestParsedMessage", ".msg");
    FileOutputStream out = new FileOutputStream(mFile);
    out.write(expected.rawContent.getBytes());
    out.close();
    pm = new ParsedMessage(mFile, null, false);
    verifyParsedMessage(pm, expected);
    pm = new ParsedMessage(mFile, null, true);
    verifyParsedMessage(pm, expected);
    // Test ParsedMessage created from MimeMessage.  Can't verify entire content
    // because JavaMail mangles the headers.
    MimeMessage mimeMsg = new ZMimeMessage(JMSession.getSession(), new SharedByteArrayInputStream(expected.rawContent.getBytes()));
    pm = new ParsedMessage(mimeMsg, false);
    Assert.assertTrue((new String(pm.getRawData()).contains("oldsubject")));
    Assert.assertTrue(getContent(pm.getMimeMessage()).contains("newsubject"));
    Assert.assertTrue(pm.getSubject().contains("newsubject"));
    pm = new ParsedMessage(mimeMsg, true);
    Assert.assertTrue((new String(pm.getRawData()).contains("oldsubject")));
    Assert.assertTrue(getContent(pm.getMimeMessage()).contains("newsubject"));
    Assert.assertTrue(pm.getSubject().contains("newsubject"));
}
Also used : ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) MimeMessage(javax.mail.internet.MimeMessage) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) FileOutputStream(java.io.FileOutputStream) 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