Search in sources :

Example 26 with MimePart

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

the class MimePackage method getPartHeaderValues.

/**
     * Get all header values from a specified MIME part
     *
     * @param headerName
     * @param partNum
     * @return
     * @throws PackageException
     */
@PublicAtsApi
public String[] getPartHeaderValues(String headerName, int partNum) throws PackageException {
    try {
        String[] headers;
        if (partNum >= parts.size()) {
            throw new NoSuchMimePartException("No MIME part at position '" + partNum + "'");
        }
        MimePart part = parts.get(partNum);
        headers = part.getHeader(headerName);
        if ((headers != null) && (headers.length > 0)) {
            return headers;
        } else {
            throw new NoSuchHeaderException(headerName, partNum);
        }
    } catch (MessagingException me) {
        throw new PackageException(me);
    }
}
Also used : MessagingException(javax.mail.MessagingException) NoSuchMimePartException(com.axway.ats.action.objects.model.NoSuchMimePartException) MimePart(javax.mail.internet.MimePart) NoSuchMimePackageException(com.axway.ats.action.objects.model.NoSuchMimePackageException) PackageException(com.axway.ats.action.objects.model.PackageException) NoSuchHeaderException(com.axway.ats.action.objects.model.NoSuchHeaderException) PublicAtsApi(com.axway.ats.common.PublicAtsApi)

Example 27 with MimePart

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

the class MimePackage method getAttachmentContentType.

/**
     * Get the attachment content type
     *
     * @param partIndex
     * @return
     * @throws PackageException
     */
@PublicAtsApi
public String getAttachmentContentType(int partIndex) throws PackageException {
    // first check if there is part at this position at all
    if (partIndex >= attachmentPartIndices.size()) {
        throw new NoSuchMimePartException("No attachment at position '" + partIndex + "'");
    }
    try {
        MimePart part = getPart(attachmentPartIndices.get(partIndex));
        // get the content type header
        ContentType contentType = new ContentType(part.getContentType());
        return contentType.getBaseType();
    } catch (MessagingException me) {
        throw new PackageException(me);
    }
}
Also used : ContentType(javax.mail.internet.ContentType) MessagingException(javax.mail.MessagingException) NoSuchMimePartException(com.axway.ats.action.objects.model.NoSuchMimePartException) MimePart(javax.mail.internet.MimePart) NoSuchMimePackageException(com.axway.ats.action.objects.model.NoSuchMimePackageException) PackageException(com.axway.ats.action.objects.model.PackageException) PublicAtsApi(com.axway.ats.common.PublicAtsApi)

Example 28 with MimePart

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

the class Test_Rfc822Headers method parse1Reg2Att.

/**
     * Test is parsing mail with no nested parts. It get the plain/text part on level 1.
     *  
     * - multipart/mixed;
     *   - text/plain
     *   - application/octet-stream
     *   - message/rfc822
     */
@Test
public void parse1Reg2Att() throws Exception {
    String mailMessagePath = Test_MimePackage.class.getResource("RFC822-headers-1_Regular_2_Attachments.eml").getPath();
    MimePackage mimeMessage = PackageLoader.loadMimePackageFromFile(mailMessagePath);
    assertEquals(1, mimeMessage.getRegularPartCount());
    assertEquals(1, mimeMessage.getAttachmentPartCount());
    // first attachment exists and is parsed
    MimePart part = mimeMessage.getPart(0, true);
    assertTrue(part != null);
    // one regular part with text
    assertTrue(mimeMessage.getPlainTextBody().startsWith("This report relates to a message you sent"));
    // nested MimePackage - the RFC822-headers
    MimePackage nestedPackWithHeadersOnly = mimeMessage.getNeededMimePackage(new int[] { 0 });
    List<PackageHeader> headers = nestedPackWithHeadersOnly.getAllHeaders();
    assertTrue(headers.size() == 31);
/* For test debugging
        int i = 0;
        for( PackageHeader packageHeader : headers ) {
            log.info("header[" + i + "] name: [" +  packageHeader.getName()
                               +"] value: [" +  packageHeader.getValue() + "]");
            i++;
        }
        */
}
Also used : MimePackage(com.axway.ats.action.objects.MimePackage) PackageHeader(com.axway.ats.action.objects.model.PackageHeader) MimePart(javax.mail.internet.MimePart) BaseTest(com.axway.ats.action.BaseTest) Test(org.junit.Test)

Example 29 with MimePart

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

the class Test_MimePackage method setBody_textPart_only.

@Test
public void setBody_textPart_only() throws Exception {
    // create a new message. It comes by default with TEXT part only(no multiparts)
    MimePackage newMailMessage = new MimePackage();
    // verify there are no any parts yet
    try {
        newMailMessage.getPart(0, false);
        assertTrue("There are some parts, while we expect to have none", false);
    } catch (NoSuchMimePartException e) {
    }
    // set the body, effectively the only TEXT part
    newMailMessage.setBody("text plain body");
    MimePart textPart = newMailMessage.getPart(0, false);
    assertEquals(textPart.getContent(), "text plain body");
    assertEquals(textPart.getContentType(), "text/plain; charset=us-ascii");
    // verify there are no more parts
    try {
        newMailMessage.getPart(1, false);
        assertTrue("There is more than 1 part, while we expect to have just 1", false);
    } catch (NoSuchMimePartException e) {
    }
}
Also used : MimePackage(com.axway.ats.action.objects.MimePackage) NoSuchMimePartException(com.axway.ats.action.objects.model.NoSuchMimePartException) MimePart(javax.mail.internet.MimePart) BaseTest(com.axway.ats.action.BaseTest) Test(org.junit.Test)

Example 30 with MimePart

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

the class Test_MimePackage method setBody_multiParts_with_textPart_only.

@Test
public void setBody_multiParts_with_textPart_only() throws Exception {
    // create a new message and add TEXT part to it
    MimePackage newMailMessage = new MimePackage();
    newMailMessage.addPart("text plain body", MimePackage.PART_TYPE_TEXT_PLAIN);
    MimePart textPart = newMailMessage.getPart(0, false);
    assertEquals(textPart.getContent(), "text plain body");
    assertEquals(textPart.getContentType(), "text/plain; charset=us-ascii");
    // modify the only part
    newMailMessage.setBody("new body");
    // verify the modifications
    MimePart newTextPart = newMailMessage.getPart(0, false);
    assertEquals(newTextPart.getContent(), "new body");
    assertEquals(newTextPart.getContentType(), "text/plain; charset=us-ascii");
    // verify there are no more parts
    try {
        newMailMessage.getPart(1, false);
        assertTrue("There is more than 1 part, while we expect to have just 1", false);
    } catch (NoSuchMimePartException e) {
    }
}
Also used : MimePackage(com.axway.ats.action.objects.MimePackage) MimePart(javax.mail.internet.MimePart) NoSuchMimePartException(com.axway.ats.action.objects.model.NoSuchMimePartException) BaseTest(com.axway.ats.action.BaseTest) Test(org.junit.Test)

Aggregations

MimePart (javax.mail.internet.MimePart)46 MessagingException (javax.mail.MessagingException)22 MimeMessage (javax.mail.internet.MimeMessage)22 IOException (java.io.IOException)15 NoSuchMimePartException (com.axway.ats.action.objects.model.NoSuchMimePartException)12 PackageException (com.axway.ats.action.objects.model.PackageException)11 NoSuchMimePackageException (com.axway.ats.action.objects.model.NoSuchMimePackageException)10 PublicAtsApi (com.axway.ats.common.PublicAtsApi)10 InputStream (java.io.InputStream)10 ServiceException (com.zimbra.common.service.ServiceException)8 Test (org.junit.Test)8 MimePackage (com.axway.ats.action.objects.MimePackage)6 ByteArrayInputStream (java.io.ByteArrayInputStream)6 ContentType (javax.mail.internet.ContentType)6 BaseTest (com.axway.ats.action.BaseTest)5 Message (com.zimbra.cs.mailbox.Message)5 MPartInfo (com.zimbra.cs.mime.MPartInfo)5 MimeMultipart (javax.mail.internet.MimeMultipart)5 ContentType (com.zimbra.common.mime.ContentType)4 ArrayList (java.util.ArrayList)4