Search in sources :

Example 1 with MessageImpl

use of org.apache.james.mime4j.message.MessageImpl in project Gargoyle by callakrsos.

the class MimeViewerExam method extracted.

private void extracted(StringBuilder sb, Body body) {
    if (body instanceof Multipart) {
        Multipart mbody = (Multipart) body;
        for (Entity part : mbody.getBodyParts()) {
            extracted(sb, part);
        }
    } else if (body instanceof MessageImpl) {
        extracted(sb, body);
    } else if (body instanceof TextBody) {
        /*
			 * A text body. Display its contents.
			 */
        TextBody textBody = (TextBody) body;
        try {
            Reader r = textBody.getReader();
            StringBuilder _sb = new StringBuilder();
            int c;
            while ((c = r.read()) != -1) {
                _sb.append((char) c);
            }
            System.out.println(_sb.toString());
            sb.append(_sb.toString());
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    } else if (body instanceof BinaryBody) {
        BinaryBody bBody = (BinaryBody) body;
        Entity parent = bBody.getParent();
        //			String dispositionType = parent.getDispositionType();
        //			String filename = parent.getFilename();
        //			String contentTransferEncoding = parent.getContentTransferEncoding();
        //			String mimeType = parent.getMimeType();
        Field field = parent.getHeader().getField("Content-ID");
        String body2 = field.getBody();
        String contentId = body2.replace("<", "").replace(">", "");
        StringBuffer buf = new StringBuffer();
        try (InputStream is = bBody.getInputStream()) {
            int read = -1;
            while ((read = is.read()) != -1) {
                buf.append((char) read);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        meta.put(contentId, buf.toString());
    } else /*
		 * Ignore Fields </br>
		 * 
		 * ContentTypeField,AddressListField,DateTimeField UnstructuredField,
		 * Field
		 * 
		 */
    {
        sb.append(body.toString());
    }
}
Also used : Entity(org.apache.james.mime4j.dom.Entity) Multipart(org.apache.james.mime4j.dom.Multipart) InputStream(java.io.InputStream) Reader(java.io.Reader) IOException(java.io.IOException) BinaryBody(org.apache.james.mime4j.dom.BinaryBody) TextBody(org.apache.james.mime4j.dom.TextBody) Field(org.apache.james.mime4j.stream.Field) MessageImpl(org.apache.james.mime4j.message.MessageImpl)

Example 2 with MessageImpl

use of org.apache.james.mime4j.message.MessageImpl in project sling by apache.

the class MessageStoreImplAttachmentsTest method recursiveMultipartMessageTest.

@Test
public void recursiveMultipartMessageTest() throws IOException {
    Multipart multipart = new MultipartImpl("mixed");
    BodyPart att1 = createRandomBinaryAttachment(100);
    multipart.addBodyPart(att1);
    BodyPart att2 = createRandomBinaryAttachment(133);
    multipart.addBodyPart(att2);
    Multipart nestedMultipart = new MultipartImpl("mixed");
    BodyPart nBody = createTextBody("Some sample text here...?!", "plain", false);
    nestedMultipart.addBodyPart(nBody);
    BodyPart nAtt1 = createRandomBinaryAttachment(300);
    nestedMultipart.addBodyPart(nAtt1);
    BodyPart NAtt2 = createRandomBinaryAttachment(100);
    nestedMultipart.addBodyPart(NAtt2);
    BodyPart nAtt3 = createTextBody("Some other text here...<br>?!", "html", true);
    nestedMultipart.addBodyPart(nAtt3);
    BodyPart nestedMessage = new BodyPart();
    nestedMessage.setMultipart(nestedMultipart);
    multipart.addBodyPart(nestedMessage);
    MessageImpl message = new MessageImpl();
    message.setMultipart(multipart);
    message.setSubject("Template message");
    message.setDate(new Date());
    message.getHeader().setField(new RawField(LIST_ID, "<list.example.com>"));
    assertSaveMessageWithAttachments(message, 5);
}
Also used : BodyPart(org.apache.james.mime4j.message.BodyPart) Multipart(org.apache.james.mime4j.dom.Multipart) MultipartImpl(org.apache.james.mime4j.message.MultipartImpl) RawField(org.apache.james.mime4j.stream.RawField) MessageImpl(org.apache.james.mime4j.message.MessageImpl) Date(java.util.Date) Test(org.junit.Test)

Example 3 with MessageImpl

use of org.apache.james.mime4j.message.MessageImpl in project sling by apache.

the class MessageStoreImplAttachmentsTest method simpleMultipartMessageTest.

@Test
public void simpleMultipartMessageTest() throws IOException {
    Multipart multipart = new MultipartImpl("mixed");
    BodyPart att0 = createTextBody("This is the first part of the template..", "plain", true);
    multipart.addBodyPart(att0);
    BodyPart att1 = createRandomBinaryAttachment(200);
    multipart.addBodyPart(att1);
    BodyPart att2 = createRandomBinaryAttachment(300);
    multipart.addBodyPart(att2);
    BodyPart att3 = createTextBody("Some sample text here...?!", "html", true);
    multipart.addBodyPart(att3);
    BodyPart att4 = createRandomBinaryAttachment(100);
    multipart.addBodyPart(att4);
    BodyPart att5 = createTextBody("Some other text here...?!", "plain", true);
    multipart.addBodyPart(att5);
    MessageImpl message = new MessageImpl();
    message.setMultipart(multipart);
    message.setSubject("Template message");
    message.setDate(new Date());
    message.getHeader().setField(new RawField(LIST_ID, "<list.example.com>"));
    assertSaveMessageWithAttachments(message, 6);
}
Also used : BodyPart(org.apache.james.mime4j.message.BodyPart) Multipart(org.apache.james.mime4j.dom.Multipart) MultipartImpl(org.apache.james.mime4j.message.MultipartImpl) RawField(org.apache.james.mime4j.stream.RawField) MessageImpl(org.apache.james.mime4j.message.MessageImpl) Date(java.util.Date) Test(org.junit.Test)

Aggregations

Multipart (org.apache.james.mime4j.dom.Multipart)3 MessageImpl (org.apache.james.mime4j.message.MessageImpl)3 Date (java.util.Date)2 BodyPart (org.apache.james.mime4j.message.BodyPart)2 MultipartImpl (org.apache.james.mime4j.message.MultipartImpl)2 RawField (org.apache.james.mime4j.stream.RawField)2 Test (org.junit.Test)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Reader (java.io.Reader)1 BinaryBody (org.apache.james.mime4j.dom.BinaryBody)1 Entity (org.apache.james.mime4j.dom.Entity)1 TextBody (org.apache.james.mime4j.dom.TextBody)1 Field (org.apache.james.mime4j.stream.Field)1