Search in sources :

Example 31 with MimeMultipart

use of javax.mail.internet.MimeMultipart in project zm-mailbox by Zimbra.

the class ZMimeParserTest method parse.

@Test
public void parse() throws Exception {
    ByteBuilder bb = new ByteBuilder(CharsetUtil.UTF_8);
    bb.append("Content-Type: text/plain\r\n");
    bb.append("From: <foo@example.com\r\n");
    bb.append("Subject: sample\r\n");
    bb.append("Content-Type: multipart/alternative; boundary=").append(BOUNDARY1).append("\r\n");
    bb.append("\r\n");
    bb.append("--").append(BOUNDARY1).append("\r\n");
    bb.append("Content-Type: text/plain\r\n");
    bb.append("\r\n");
    bb.append("foo!  bar!  loud noises\r\n\r\n");
    bb.append("--").append(BOUNDARY1).append("--\r\n");
    try {
        MimeMessage mm = ZMimeParser.parse(getSession(), new SharedByteArrayInputStream(bb.toByteArray()));
        Assert.assertFalse("content isn't multipart", mm.getContent() instanceof MimeMultipart);
        Assert.assertEquals("text/plain", "text/plain", new ZContentType(mm.getContentType()).getBaseType());
    } catch (ClassCastException e) {
        Assert.fail("mishandled double Content-Type headers");
    }
}
Also used : ByteBuilder(com.zimbra.common.zmime.ZMimeUtility.ByteBuilder) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) SharedByteArrayInputStream(javax.mail.util.SharedByteArrayInputStream) Test(org.junit.Test)

Example 32 with MimeMultipart

use of javax.mail.internet.MimeMultipart in project zm-mailbox by Zimbra.

the class ExtShareInfoTest method testGenNotifyBody.

@Test
public void testGenNotifyBody() {
    Locale locale = new Locale("en", "US");
    String notes = "none";
    ShareInfoData sid = new ShareInfoData();
    sid.setGranteeDisplayName("Demo User Three");
    sid.setGranteeId(testAcct.getId());
    sid.setGranteeName(testAcct.getName());
    sid.setGranteeType(ACL.GRANTEE_GUEST);
    sid.setPath("/Inbox/Test");
    sid.setFolderDefaultView(MailItem.Type.MESSAGE);
    sid.setItemUuid("9badf685-3420-458b-9ce5-826b0bec638f");
    sid.setItemId(257);
    sid.setOwnerAcctId(ownerAcct.getId());
    sid.setOwnerAcctEmail(ownerAcct.getName());
    sid.setOwnerAcctDisplayName("Demo User Two");
    try {
        sid.setRights(ACL.stringToRights("rwidxap"));
        MimeMultipart mmp = ShareInfo.NotificationSender.genNotifBody(sid, notes, locale, null, null);
        Assert.assertNotNull(mmp);
        String body = (String) mmp.getBodyPart(0).getDataHandler().getContent();
        int index = body.indexOf("http");
        int endIndex = body.indexOf(".", index);
        String authUrl = body.substring(index, endIndex);
        index = authUrl.indexOf("p=");
        String authToken = authUrl.substring(index + 2);
        try {
            ZimbraAuthToken.getAuthToken(authToken);
            fail("Authtoken should fail");
        } catch (AuthTokenException e) {
            assertTrue(e.getMessage().contains("hmac failure"));
        }
    // Commenting for now, need to figure out why it fails on hudson
    //           try {
    //               ExternalUserProvServlet.validatePrelimToken(authToken);
    //           } catch (Exception e) {
    //               fail("Should not throw Exception" + e.getMessage());
    //           }
    } catch (ServiceException | MessagingException | IOException e) {
        fail("Exception should not be thrown: " + e.getMessage());
    }
}
Also used : Locale(java.util.Locale) ServiceException(com.zimbra.common.service.ServiceException) MimeMultipart(javax.mail.internet.MimeMultipart) MessagingException(javax.mail.MessagingException) IOException(java.io.IOException) Test(org.junit.Test)

Example 33 with MimeMultipart

use of javax.mail.internet.MimeMultipart in project zm-mailbox by Zimbra.

the class ShareInfoTest method testGenNotifyBody.

@Test
public void testGenNotifyBody() {
    Locale locale = new Locale("en", "US");
    String notes = "none";
    ShareInfoData sid = new ShareInfoData();
    sid.setGranteeDisplayName("Demo User Three");
    sid.setGranteeId("46031e4c-deb4-4724-b5bb-8f854d0c518a");
    sid.setGranteeName("Demo User Three");
    sid.setGranteeType(ACL.GRANTEE_USER);
    sid.setPath("/Calendar/Cal1");
    sid.setFolderDefaultView(MailItem.Type.APPOINTMENT);
    sid.setItemUuid("9badf685-3420-458b-9ce5-826b0bec638f");
    sid.setItemId(257);
    sid.setOwnerAcctId("bbf152ca-e7cd-477e-9f72-70fef715c5f9");
    sid.setOwnerAcctEmail("test@zimbra.com");
    sid.setOwnerAcctDisplayName("Demo User Two");
    try {
        sid.setRights(ACL.stringToRights("rwidxap"));
        MimeMultipart mmp = ShareInfo.NotificationSender.genNotifBody(sid, notes, locale, null, null);
        Assert.assertNotNull(mmp);
        String body = (String) mmp.getBodyPart(0).getDataHandler().getContent();
        assertTrue(body.indexOf("Role: Admin") != -1);
    } catch (ServiceException | MessagingException | IOException e) {
        fail("Exception should not be thrown: " + e.getMessage());
    }
}
Also used : Locale(java.util.Locale) ServiceException(com.zimbra.common.service.ServiceException) MimeMultipart(javax.mail.internet.MimeMultipart) MessagingException(javax.mail.MessagingException) IOException(java.io.IOException) Test(org.junit.Test)

Example 34 with MimeMultipart

use of javax.mail.internet.MimeMultipart in project zm-mailbox by Zimbra.

the class MailboxTestUtil method generateMessageWithAttachment.

public static ParsedMessage generateMessageWithAttachment(String subject) throws Exception {
    MimeMessage mm = new Mime.FixedMimeMessage(JMSession.getSession());
    mm.setHeader("From", "Vera Oliphant <oli@example.com>");
    mm.setHeader("To", "Jimmy Dean <jdean@example.com>");
    mm.setHeader("Subject", subject);
    mm.setText("Good as gold");
    MimeMultipart multi = new ZMimeMultipart("mixed");
    ContentDisposition cdisp = new ContentDisposition(Part.ATTACHMENT);
    cdisp.setParameter("filename", "fun.txt");
    ZMimeBodyPart bp = new ZMimeBodyPart();
    // it gets called before setting Content-Type and CTE headers.
    try {
        bp.setDataHandler(new DataHandler(new ByteArrayDataSource("Feeling attached.", "text/plain")));
    } catch (IOException e) {
        throw new MessagingException("could not generate mime part content", e);
    }
    bp.addHeader("Content-Disposition", cdisp.toString());
    bp.addHeader("Content-Type", "text/plain");
    bp.addHeader("Content-Transfer-Encoding", MimeConstants.ET_8BIT);
    multi.addBodyPart(bp);
    mm.setContent(multi);
    mm.saveChanges();
    return new ParsedMessage(mm, false);
}
Also used : ContentDisposition(com.zimbra.common.mime.ContentDisposition) MimeMessage(javax.mail.internet.MimeMessage) ZMimeMultipart(com.zimbra.common.zmime.ZMimeMultipart) MimeMultipart(javax.mail.internet.MimeMultipart) ZMimeBodyPart(com.zimbra.common.zmime.ZMimeBodyPart) MessagingException(javax.mail.MessagingException) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) ZMimeMultipart(com.zimbra.common.zmime.ZMimeMultipart) DataHandler(javax.activation.DataHandler) IOException(java.io.IOException) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource)

Example 35 with MimeMultipart

use of javax.mail.internet.MimeMultipart in project zm-mailbox by Zimbra.

the class ParseMimeMessageTest method attachZimbraDocument.

@Test
public void attachZimbraDocument() throws Exception {
    Account acct = Provisioning.getInstance().getAccount(MockProvisioning.DEFAULT_ACCOUNT_ID);
    Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(acct);
    OperationContext octxt = new OperationContext(acct);
    Document doc = mbox.createDocument(octxt, Mailbox.ID_FOLDER_BRIEFCASE, "testdoc", MimeConstants.CT_APPLICATION_ZIMBRA_DOC, "author", "description", new ByteArrayInputStream("test123".getBytes()));
    Element el = new Element.JSONElement(MailConstants.E_MSG);
    el.addAttribute(MailConstants.E_SUBJECT, "attach message");
    el.addElement(MailConstants.E_EMAIL).addAttribute(MailConstants.A_ADDRESS_TYPE, EmailType.TO.toString()).addAttribute(MailConstants.A_ADDRESS, "rcpt@zimbra.com");
    el.addElement(MailConstants.E_MIMEPART).addAttribute(MailConstants.A_CONTENT_TYPE, "text/plain").addAttribute(MailConstants.E_CONTENT, "This is the content.");
    el.addElement(MailConstants.E_ATTACH).addElement(MailConstants.E_DOC).addAttribute(MailConstants.A_ID, doc.getId());
    ZimbraSoapContext zsc = getMockSoapContext();
    MimeMessage mm = ParseMimeMessage.parseMimeMsgSoap(zsc, octxt, null, el, null, new ParseMimeMessage.MimeMessageData());
    MimeMultipart mmp = (MimeMultipart) mm.getContent();
    MimeBodyPart part = (MimeBodyPart) mmp.getBodyPart(1);
    Assert.assertEquals(MimeConstants.CT_TEXT_HTML, new ContentType(part.getContentType()).getContentType());
}
Also used : OperationContext(com.zimbra.cs.mailbox.OperationContext) Account(com.zimbra.cs.account.Account) ContentType(com.zimbra.common.mime.ContentType) Element(com.zimbra.common.soap.Element) Document(com.zimbra.cs.mailbox.Document) Mailbox(com.zimbra.cs.mailbox.Mailbox) ByteArrayInputStream(java.io.ByteArrayInputStream) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) ZimbraSoapContext(com.zimbra.soap.ZimbraSoapContext) MimeBodyPart(javax.mail.internet.MimeBodyPart) Test(org.junit.Test)

Aggregations

MimeMultipart (javax.mail.internet.MimeMultipart)213 MimeBodyPart (javax.mail.internet.MimeBodyPart)134 MimeMessage (javax.mail.internet.MimeMessage)85 MessagingException (javax.mail.MessagingException)69 ByteArrayOutputStream (java.io.ByteArrayOutputStream)55 BodyPart (javax.mail.BodyPart)55 IOException (java.io.IOException)47 ByteString (com.linkedin.data.ByteString)37 DataHandler (javax.activation.DataHandler)33 InternetAddress (javax.mail.internet.InternetAddress)33 Test (org.testng.annotations.Test)31 ZMimeMultipart (com.zimbra.common.zmime.ZMimeMultipart)27 HashMap (java.util.HashMap)27 ZMimeBodyPart (com.zimbra.common.zmime.ZMimeBodyPart)26 Multipart (javax.mail.Multipart)26 Properties (java.util.Properties)25 Session (javax.mail.Session)24 ByteArrayDataSource (javax.mail.util.ByteArrayDataSource)24 InputStream (java.io.InputStream)23 Date (java.util.Date)21