Search in sources :

Example 36 with MimeMultipart

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

the class ParseMimeMessageTest method staleReference.

@Test
public void staleReference() throws Exception {
    Account acct = Provisioning.getInstance().getAccount(MockProvisioning.DEFAULT_ACCOUNT_ID);
    Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(acct);
    // first, create the original draft
    OperationContext octxt = new OperationContext(acct);
    Document doc = mbox.createDocument(octxt, Mailbox.ID_FOLDER_BRIEFCASE, "testdoc", MimeConstants.CT_TEXT_PLAIN, null, null, randomContent("test1", 8192));
    Document doc2 = mbox.createDocument(octxt, Mailbox.ID_FOLDER_BRIEFCASE, "testdoc2", MimeConstants.CT_TEXT_PLAIN, null, null, randomContent("test2", 8192));
    Element el = new Element.JSONElement(MailConstants.E_MSG), attach;
    el.addAttribute(MailConstants.E_SUBJECT, "has attachment");
    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.");
    attach = el.addElement(MailConstants.E_ATTACH);
    attach.addElement(MailConstants.E_DOC).addAttribute(MailConstants.A_ID, doc.getId());
    attach.addElement(MailConstants.E_DOC).addAttribute(MailConstants.A_ID, doc2.getId());
    ZimbraSoapContext zsc = getMockSoapContext();
    MimeMessage mm = ParseMimeMessage.parseMimeMsgSoap(zsc, octxt, null, el, null, new ParseMimeMessage.MimeMessageData());
    Message draft = mbox.saveDraft(octxt, new ParsedMessage(mm, false), -1);
    // then, create a new draft that references one of the original draft's attachments
    attach.detach();
    (attach = el.addElement(MailConstants.E_ATTACH)).addElement(MailConstants.E_MIMEPART).addAttribute(MailConstants.A_MESSAGE_ID, draft.getId()).addAttribute(MailConstants.A_PART, "3");
    mm = ParseMimeMessage.parseMimeMsgSoap(zsc, octxt, null, el, null, new ParseMimeMessage.MimeMessageData());
    // delete the draft itself and then try to save the new draft
    mbox.delete(octxt, draft.getId(), MailItem.Type.MESSAGE);
    Message draft2 = mbox.saveDraft(octxt, new ParsedMessage(mm, false), -1);
    // check that the attachment's content is present and correct
    MimeMultipart multi = (MimeMultipart) (draft2.getMimeMessage().getContent());
    Assert.assertEquals("2 parts in draft", 2, multi.getCount());
    Assert.assertEquals("attached part content", "test2", firstLine((MimeBodyPart) multi.getBodyPart(1)));
}
Also used : OperationContext(com.zimbra.cs.mailbox.OperationContext) Account(com.zimbra.cs.account.Account) Message(com.zimbra.cs.mailbox.Message) MimeMessage(javax.mail.internet.MimeMessage) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) Element(com.zimbra.common.soap.Element) Document(com.zimbra.cs.mailbox.Document) Mailbox(com.zimbra.cs.mailbox.Mailbox) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) ZimbraSoapContext(com.zimbra.soap.ZimbraSoapContext) MimeBodyPart(javax.mail.internet.MimeBodyPart) Test(org.junit.Test)

Example 37 with MimeMultipart

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

the class SendShareNotificationTest method generateShareNotification.

@Override
protected MimeMessage generateShareNotification(Account authAccount, Account ownerAccount, ShareInfoData sid, String notes, SendShareNotificationRequest.Action action, Collection<String> internalRecipients, String externalRecipient) throws ServiceException, MessagingException {
    MimeMessage mm = super.generateShareNotification(authAccount, ownerAccount, sid, notes, action, internalRecipients, externalRecipient);
    try {
        MimeMultipart mmp = (MimeMultipart) mm.getContent();
        BodyPart bp = mmp.getBodyPart(2);
        Assert.assertTrue(bp.getContentType().startsWith(MimeConstants.CT_XML_ZIMBRA_SHARE));
        Element share = Element.parseXML(bp.getInputStream());
        if (revoke) {
            Assert.assertEquals(share.getQName().getName(), MailConstants.ShareConstants.E_REVOKE);
        } else {
            Assert.assertEquals(share.getQName().getName(), MailConstants.ShareConstants.E_SHARE);
        }
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    }
    return mm;
}
Also used : BodyPart(javax.mail.BodyPart) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) Element(com.zimbra.common.soap.Element) MessagingException(javax.mail.MessagingException) ServiceException(com.zimbra.common.service.ServiceException) IOException(java.io.IOException)

Example 38 with MimeMultipart

use of javax.mail.internet.MimeMultipart in project perun by CESNET.

the class MessagePreparator method prepare.

/*
	 * (non-Javadoc)
	 *
	 * @see org.springframework.mail.javamail.MimeMessagePreparator#prepare(javax.mail.internet.MimeMessage)
	 */
public void prepare(MimeMessage mimeMessage) throws Exception {
    MimeMultipart mpRoot = new MimeMultipart("mixed");
    Multipart mp = new MimeMultipart("alternative");
    // Create a body part to house the multipart/alternative Part
    MimeBodyPart contentPartRoot = new MimeBodyPart();
    contentPartRoot.setContent(mp);
    // Add the root body part to the root multipart
    mpRoot.addBodyPart(contentPartRoot);
    // adding recipients, cc and bcc
    if (getTo() != null) {
        for (String to : getTo()) {
            mimeMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
        }
    }
    if (getCc() != null) {
        for (String cc : getCc()) {
            mimeMessage.addRecipient(Message.RecipientType.CC, new InternetAddress(cc));
        }
    }
    if (getBcc() != null) {
        for (String bcc : getBcc()) {
            mimeMessage.addRecipient(Message.RecipientType.BCC, new InternetAddress(bcc));
        }
    }
    // adding from and subject
    mimeMessage.setFrom(new InternetAddress(getFrom(), getFromText()));
    mimeMessage.setSubject(getSubject());
    if (getEmailType().equals(EmailType.HTML)) {
        mp.addBodyPart(createHtmlMessage());
    } else if (getEmailType().equals(EmailType.PLAIN)) {
        mp.addBodyPart(createTextMessage());
    }
    // Create an "ATTACHMENT" - we must put it to mpRoot(mixed content)
    if (getFileNames() != null) {
        for (String filename : getFileNames()) {
            mpRoot.addBodyPart(createAttachment(filename));
        }
    }
    mimeMessage.setContent(mpRoot);
    logger.debug("Message is prepared to send");
}
Also used : MimeMultipart(javax.mail.internet.MimeMultipart) Multipart(javax.mail.Multipart) InternetAddress(javax.mail.internet.InternetAddress) MimeMultipart(javax.mail.internet.MimeMultipart) MimeBodyPart(javax.mail.internet.MimeBodyPart)

Example 39 with MimeMultipart

use of javax.mail.internet.MimeMultipart in project perun by CESNET.

the class MessagePreparator method createHtmlMessage.

/**
	 * Creates HTML message from freemarker template
	 *
	 * @return
	 * @throws Exception
	 */
private BodyPart createHtmlMessage() throws Exception {
    Multipart htmlContent = new MimeMultipart("related");
    BodyPart htmlPage = new MimeBodyPart();
    htmlPage.setDataHandler(createDataHandler(content.getBytes("utf-8"), "text/html;charset=utf-8"));
    htmlContent.addBodyPart(htmlPage);
    BodyPart htmlPart = new MimeBodyPart();
    htmlPart.setContent(htmlContent);
    logger.debug("HTML MESSAGE CREATED ; content: " + content);
    return htmlPart;
}
Also used : MimeBodyPart(javax.mail.internet.MimeBodyPart) BodyPart(javax.mail.BodyPart) MimeMultipart(javax.mail.internet.MimeMultipart) Multipart(javax.mail.Multipart) MimeMultipart(javax.mail.internet.MimeMultipart) MimeBodyPart(javax.mail.internet.MimeBodyPart)

Example 40 with MimeMultipart

use of javax.mail.internet.MimeMultipart in project ats-framework by Axway.

the class MimePackage method addAlternativePart.

/**
     * Add a multipart/alternative part to message body
     *
     * @param plainContent
     *            the content of the text/plain sub-part
     * @param htmlContent
     *            the content of the text/html sub-part
     * @param charset
     *            the character set for the part
     * @throws PackageException
     */
@PublicAtsApi
public void addAlternativePart(String plainContent, String htmlContent, String charset) throws PackageException {
    MimeMultipart alternativePart = new MimeMultipart("alternative");
    try {
        // create a new text/plain part
        MimeBodyPart plainPart = new MimeBodyPart();
        plainPart.setText(plainContent, charset, PART_TYPE_TEXT_PLAIN);
        plainPart.setDisposition(MimeBodyPart.INLINE);
        MimeBodyPart htmlPart = new MimeBodyPart();
        htmlPart.setText(htmlContent, charset, PART_TYPE_TEXT_HTML);
        htmlPart.setDisposition(MimeBodyPart.INLINE);
        alternativePart.addBodyPart(plainPart, 0);
        alternativePart.addBodyPart(htmlPart, 1);
        MimeBodyPart mimePart = new MimeBodyPart();
        mimePart.setContent(alternativePart);
        addPart(mimePart, PART_POSITION_LAST);
    } catch (MessagingException me) {
        throw new PackageException(me);
    }
}
Also used : MimeMultipart(javax.mail.internet.MimeMultipart) MessagingException(javax.mail.MessagingException) NoSuchMimePackageException(com.axway.ats.action.objects.model.NoSuchMimePackageException) PackageException(com.axway.ats.action.objects.model.PackageException) MimeBodyPart(javax.mail.internet.MimeBodyPart) PublicAtsApi(com.axway.ats.common.PublicAtsApi)

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