Search in sources :

Example 6 with SharedByteArrayInputStream

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

the class MailSenderTest method updateReferenceHeaders.

@Test
public void updateReferenceHeaders() throws Exception {
    MailSender sender = new MailSender();
    Provisioning prov = Provisioning.getInstance();
    Account account = prov.getAccount(MockProvisioning.DEFAULT_ACCOUNT_ID);
    Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
    String from = "sender@example.com";
    String to = "recipient@example.com";
    String subject = "test subject";
    MessageBuilder builder = new MessageBuilder().withFrom(from).withToRecipient(to).withSubject(subject).withBody("test body");
    String content = builder.create();
    MimeMessage msg = new FixedMimeMessage(JMSession.getSession(), new SharedByteArrayInputStream(content.getBytes()));
    //default
    Assert.assertNull(msg.getHeader("Thread-Index"));
    Assert.assertNull(msg.getHeader("Thread-Topic"));
    sender.updateReferenceHeaders(msg, null, account);
    Assert.assertEquals(1, msg.getHeader("Thread-Index").length);
    Assert.assertEquals(1, msg.getHeader("Thread-Topic").length);
    //technically the normalized subject, but our test message already had normalized subject
    Assert.assertEquals(subject, msg.getHeader("Thread-Topic")[0]);
    //index set by sending agent (bg 96107)
    msg = new FixedMimeMessage(JMSession.getSession(), new SharedByteArrayInputStream(content.getBytes()));
    String presetIndex = "1234";
    msg.setHeader("Thread-Index", presetIndex);
    Assert.assertNull(msg.getHeader("Thread-Topic"));
    sender.updateReferenceHeaders(msg, null, account);
    Assert.assertEquals(1, msg.getHeader("Thread-Index").length);
    Assert.assertEquals(1, msg.getHeader("Thread-Topic").length);
    Assert.assertEquals(subject, msg.getHeader("Thread-Topic")[0]);
    Assert.assertEquals(presetIndex, msg.getHeader("Thread-Index")[0]);
    //topic set by sending agent (bug 96107)
    msg = new FixedMimeMessage(JMSession.getSession(), new SharedByteArrayInputStream(content.getBytes()));
    Assert.assertNull(msg.getHeader("Thread-Index"));
    String presetTopic = "mytopic";
    msg.setHeader("Thread-Topic", presetTopic);
    sender.updateReferenceHeaders(msg, null, account);
    Assert.assertEquals(1, msg.getHeader("Thread-Index").length);
    Assert.assertEquals(1, msg.getHeader("Thread-Topic").length);
    Assert.assertEquals(presetTopic, msg.getHeader("Thread-Topic")[0]);
    //both
    msg = new FixedMimeMessage(JMSession.getSession(), new SharedByteArrayInputStream(content.getBytes()));
    msg.setHeader("Thread-Topic", presetTopic);
    msg.setHeader("Thread-Index", presetIndex);
    sender.updateReferenceHeaders(msg, null, account);
    Assert.assertEquals(1, msg.getHeader("Thread-Index").length);
    Assert.assertEquals(1, msg.getHeader("Thread-Topic").length);
    Assert.assertEquals(presetTopic, msg.getHeader("Thread-Topic")[0]);
    Assert.assertEquals(presetIndex, msg.getHeader("Thread-Index")[0]);
    //subject changed from orig parent subject (bug 96954)
    String parentSubject = "parent subject";
    builder = new MessageBuilder().withFrom(from).withToRecipient(to).withSubject(parentSubject).withBody("parent body");
    String parentContent = builder.create();
    MimeMessage parentMsg = new FixedMimeMessage(JMSession.getSession(), new SharedByteArrayInputStream(parentContent.getBytes()));
    sender.updateReferenceHeaders(parentMsg, null, account);
    ParsedMessage pm = new ParsedMessage(parentMsg, false);
    Message mboxMessage = mbox.addMessage(null, pm, MailboxTest.STANDARD_DELIVERY_OPTIONS, null);
    sender = new MailSender();
    sender.setOriginalMessageId(new ItemId(mboxMessage));
    sender.setReplyType(MailSender.MSGTYPE_REPLY);
    msg = new FixedMimeMessage(JMSession.getSession(), new SharedByteArrayInputStream(content.getBytes()));
    sender.updateReferenceHeaders(msg, null, account);
    Assert.assertEquals(1, msg.getHeader("Thread-Index").length);
    Assert.assertEquals(1, msg.getHeader("Thread-Topic").length);
    Assert.assertEquals(subject, msg.getHeader("Thread-Topic")[0]);
    //keep agent specified topic and/or index even when parent specified
    msg = new FixedMimeMessage(JMSession.getSession(), new SharedByteArrayInputStream(content.getBytes()));
    msg.setHeader("Thread-Topic", presetTopic);
    sender.updateReferenceHeaders(msg, null, account);
    Assert.assertEquals(1, msg.getHeader("Thread-Index").length);
    Assert.assertEquals(1, msg.getHeader("Thread-Topic").length);
    Assert.assertEquals(presetTopic, msg.getHeader("Thread-Topic")[0]);
    msg = new FixedMimeMessage(JMSession.getSession(), new SharedByteArrayInputStream(content.getBytes()));
    msg.setHeader("Thread-Index", presetIndex);
    sender.updateReferenceHeaders(msg, null, account);
    Assert.assertEquals(1, msg.getHeader("Thread-Index").length);
    Assert.assertEquals(1, msg.getHeader("Thread-Topic").length);
    Assert.assertEquals(subject, msg.getHeader("Thread-Topic")[0]);
    Assert.assertEquals(presetIndex, msg.getHeader("Thread-Index")[0]);
    msg = new FixedMimeMessage(JMSession.getSession(), new SharedByteArrayInputStream(content.getBytes()));
    msg.setHeader("Thread-Topic", presetTopic);
    msg.setHeader("Thread-Index", presetIndex);
    sender.updateReferenceHeaders(msg, null, account);
    Assert.assertEquals(1, msg.getHeader("Thread-Index").length);
    Assert.assertEquals(1, msg.getHeader("Thread-Topic").length);
    Assert.assertEquals(presetTopic, msg.getHeader("Thread-Topic")[0]);
    Assert.assertEquals(presetIndex, msg.getHeader("Thread-Index")[0]);
}
Also used : Account(com.zimbra.cs.account.Account) MessageBuilder(com.zimbra.qa.unittest.MessageBuilder) MimeMessage(javax.mail.internet.MimeMessage) FixedMimeMessage(com.zimbra.cs.mime.Mime.FixedMimeMessage) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) MimeMessage(javax.mail.internet.MimeMessage) FixedMimeMessage(com.zimbra.cs.mime.Mime.FixedMimeMessage) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) FixedMimeMessage(com.zimbra.cs.mime.Mime.FixedMimeMessage) ItemId(com.zimbra.cs.service.util.ItemId) MockProvisioning(com.zimbra.cs.account.MockProvisioning) Provisioning(com.zimbra.cs.account.Provisioning) SharedByteArrayInputStream(javax.mail.util.SharedByteArrayInputStream) Test(org.junit.Test)

Example 7 with SharedByteArrayInputStream

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

the class SmtpTransportTest method dataError.

@Test(timeout = 3000)
public void dataError() throws Exception {
    server = MockTcpServer.scenario().sendLine("220 test ready").recvLine().sendLine("250 OK").recvLine().sendLine("250 OK").recvLine().sendLine("250 OK").recvLine().sendLine("451 error").recvLine().build().start(PORT);
    Session session = JMSession.getSession();
    Transport transport = session.getTransport("smtp");
    transport.connect("localhost", PORT, null, null);
    String raw = "From: sender@zimbra.com\nTo: rcpt@zimbra.com\nSubject: test\n\ntest";
    MimeMessage msg = new ZMimeMessage(session, new SharedByteArrayInputStream(raw.getBytes(Charsets.ISO_8859_1)));
    try {
        transport.sendMessage(msg, msg.getAllRecipients());
        Assert.fail();
    } catch (SendFailedException e) {
        Assert.assertEquals(1, e.getValidSentAddresses().length);
        Assert.assertEquals(0, e.getValidUnsentAddresses().length);
        Assert.assertEquals(0, e.getInvalidAddresses().length);
    } finally {
        transport.close();
    }
    server.shutdown(1000);
    Assert.assertEquals("EHLO localhost\r\n", server.replay());
    Assert.assertEquals("MAIL FROM:<sender@zimbra.com>\r\n", server.replay());
    Assert.assertEquals("RCPT TO:<rcpt@zimbra.com>\r\n", server.replay());
    Assert.assertEquals("DATA\r\n", server.replay());
    Assert.assertEquals("QUIT\r\n", server.replay());
    Assert.assertNull(server.replay());
}
Also used : SendFailedException(javax.mail.SendFailedException) ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) MimeMessage(javax.mail.internet.MimeMessage) Transport(javax.mail.Transport) JMSession(com.zimbra.cs.util.JMSession) Session(javax.mail.Session) SharedByteArrayInputStream(javax.mail.util.SharedByteArrayInputStream) Test(org.junit.Test)

Example 8 with SharedByteArrayInputStream

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

the class SmtpTransportTest method quitNoResponse.

@Test(timeout = 3000)
public void quitNoResponse() throws Exception {
    server = MockTcpServer.scenario().sendLine("220 test ready").recvLine().sendLine("250 OK").recvLine().sendLine("250 OK").recvLine().sendLine("250 OK").recvLine().sendLine("354 OK").swallowUntil("\r\n.\r\n").sendLine("250 OK").recvLine().build().start(PORT);
    Session session = JMSession.getSession();
    Transport transport = session.getTransport("smtp");
    transport.connect("localhost", PORT, null, null);
    String raw = "From: sender@zimbra.com\nTo: rcpt@zimbra.com\nSubject: test\n\ntest";
    MimeMessage msg = new ZMimeMessage(session, new SharedByteArrayInputStream(raw.getBytes(Charsets.ISO_8859_1)));
    transport.sendMessage(msg, msg.getAllRecipients());
    transport.close();
    server.shutdown(1000);
    Assert.assertEquals("EHLO localhost\r\n", server.replay());
    Assert.assertEquals("MAIL FROM:<sender@zimbra.com>\r\n", server.replay());
    Assert.assertEquals("RCPT TO:<rcpt@zimbra.com>\r\n", server.replay());
    Assert.assertEquals("DATA\r\n", server.replay());
    Assert.assertEquals("QUIT\r\n", server.replay());
    Assert.assertNull(server.replay());
}
Also used : ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) MimeMessage(javax.mail.internet.MimeMessage) Transport(javax.mail.Transport) JMSession(com.zimbra.cs.util.JMSession) Session(javax.mail.Session) SharedByteArrayInputStream(javax.mail.util.SharedByteArrayInputStream) Test(org.junit.Test)

Example 9 with SharedByteArrayInputStream

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

the class MimeTest method testMultipartPGP.

@Test
public void testMultipartPGP() throws Exception {
    String content = "From: user1@example.com\r\n" + "To: user2@example.com\r\n" + "Subject: test\r\n" + "Content-Type: multipart/encyrpted;\r\n" + " protocol=\"application/pgp-encrypted\";\r\n" + " boundary=" + boundary + "\r\n" + "Content-Transfer-Encoding: base64\r\n\r\n" + "------------1111971890AC3BB91\r\n" + "Content-Type: application/pgp-encrypted\r\n" + "Content-Description: PGP/MIME version identification\r\n\r\n" + "Version: 1\r\n\r\n" + "------------1111971890AC3BB91\r\n" + "Content-Type: application/octet-stream; name=\"encrypted.asc\"\r\n" + "Content-Description: OpenPGP encrypted message\r\n" + "Content-Disposition: inline; filename=\"encrypted.asc\"\r\n\r\n" + "-----BEGIN PGP MESSAGE-----\r\n" + "Version: GnuPG v2.0.22 (GNU/Linux)\r\n\r\n" + //not a real message, just some placeholder data
    "o82ejqwkjeh12398123bjkbas731321\r\n" + "-----END PGP MESSAGE-----\r\n\r\n";
    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 multiPart = parts.get(0);
    Assert.assertEquals("multipart/encyrpted", multiPart.getContentType());
    Assert.assertEquals("multipart/encyrpted;\r\n protocol=\"application/pgp-encrypted\";\r\n boundary=" + boundary, multiPart.getFullContentType());
    MPartInfo pgpVersion = parts.get(1);
    Assert.assertEquals("application/pgp-encrypted", pgpVersion.getContentType());
    MPartInfo pgpMsg = parts.get(2);
    Assert.assertEquals("application/octet-stream", pgpMsg.getContentType());
    Assert.assertEquals("application/octet-stream; name=\"encrypted.asc\"", pgpMsg.getFullContentType());
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) SharedByteArrayInputStream(javax.mail.util.SharedByteArrayInputStream) Test(org.junit.Test)

Example 10 with SharedByteArrayInputStream

use of javax.mail.util.SharedByteArrayInputStream 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)

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