Package org.apache.james.mime4j.message

Examples of org.apache.james.mime4j.message.Message$MessageBuilder


            Message message = message();
            resultMail.mailData = new Mime4jMessageMessageContent(message);
        }

        private Message message() {
            Message message = new Message();
            Mime4jHeaderBuilder headerBuilder =
                    new Mime4jHeaderBuilder(mime4jFieldFactory);
            headerBuilder.add("MIME-Version", "1.0");
            headerBuilder.add("Date", new Date());
            message.setHeader(headerBuilder.toHeader());

            message.createMessageId(reportingMtaName);
            message.setSubject("Delivery Status Notification");
            message.setFrom(fromAddress.toMime4jMailbox());
            message.setTo(Mailbox.parse(originalMail.from.getSmtpText()));

            Multipart report = multipartReport();
            message.setMultipart(report,
                    Collections.singletonMap("report-type", "delivery-status"));
            return message;
        }
View Full Code Here


        writeToFileForDebugging(dsnMail.mailData);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        dsnMail.mailData.writeTo(out);
        byte[] bytes = out.toByteArray();
        ByteArrayInputStream in = new ByteArrayInputStream(bytes);
        Message message = new Message(in);
        assertEquals(message.getMimeType(), "multipart/report");
    }
View Full Code Here

        FileOutputStream fout = new FileOutputStream(file);
        dsnMail.mailData.writeTo(fout);
        fout.close();

        FileInputStream in = new FileInputStream(file);
        Message message = new Message(in);
        in.close();
        assertEquals(message.getMimeType(), "multipart/report");
        file.delete();
    }
View Full Code Here

    }
  
    protected void runTest() throws Throwable {
        MimeEntityConfig config = new MimeEntityConfig();
        config.setMaxLineLen(-1);
        Message inputMessage = new Message(new FileInputStream(file), config);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        inputMessage.writeTo(out, MessageUtils.LENIENT);
       
        String msgoutFile = file.getAbsolutePath().substring(0, file.getAbsolutePath().lastIndexOf('.')) + ".out";
        String msgoutFileMime4j = file.getAbsolutePath().substring(0, file.getAbsolutePath().lastIndexOf('.')) + ".mime4j.out";
       
        try {
            ByteArrayOutputStream expectedstream = new ByteArrayOutputStream();
            CodecUtil.copy(new FileInputStream(msgoutFile), expectedstream);
            assertEquals("Wrong Expected result", new String(expectedstream.toByteArray()), new String(out.toByteArray()));
           
            Message roundtripMessage = new Message(new FileInputStream(msgoutFile), config);
            ByteArrayOutputStream outRoundtrip = new ByteArrayOutputStream();
            roundtripMessage.writeTo(outRoundtrip, MessageUtils.LENIENT);
            assertEquals("Failed LENIENT roundtrip", new String(out.toByteArray()), new String(outRoundtrip.toByteArray()));

            roundtripMessage = new Message(new FileInputStream(msgoutFile), config);
            outRoundtrip = new ByteArrayOutputStream();
            roundtripMessage.writeTo(outRoundtrip, MessageUtils.STRICT_ERROR);
            assertEquals("Failed STRICT roundtrip", new String(out.toByteArray()), new String(outRoundtrip.toByteArray()));

        } catch (FileNotFoundException e) {
            FileOutputStream fos = new FileOutputStream(msgoutFileMime4j);
            fos.write(out.toByteArray());
View Full Code Here

*/
public class TextPlainMessage {
    public static void main(String[] args) throws IOException {
        // 1) start with an empty message

        Message message = new Message();

        // 2) set header fields

        // Date and From are required fields
        message.setDate(new Date());
        message.setFrom(Mailbox.parse("John Doe <jdoe@machine.example>"));

        // Message-ID should be present
        message.createMessageId("machine.example");

        // set some optional fields
        message.setTo(Mailbox.parse("Mary Smith <mary@example.net>"));
        message.setSubject("Saying Hello");

        // 3) set a text body

        BodyFactory bodyFactory = new BodyFactory();
        TextBody body = bodyFactory.textBody("This is a message just to "
                + "say hello.\r\nSo, \"Hello\".");

        // note that setText also sets the Content-Type header field
        message.setText(body);

        // 4) print message to standard output

        message.writeTo(System.out);

        // 5) message is no longer needed and should be disposed of

        message.dispose();
    }
View Full Code Here

public class MultipartMessage {

    public static void main(String[] args) throws Exception {
        // 1) start with an empty message

        Message message = new Message();

        // 2) set header fields

        // Date and From are required fields
        message.setDate(new Date());
        message.setFrom(Mailbox.parse("John Doe <jdoe@machine.example>"));

        // Message-ID should be present
        message.createMessageId("machine.example");

        // set some optional fields
        message.setTo(Mailbox.parse("Mary Smith <mary@example.net>"));
        message.setSubject("An image for you");

        // 3) set a multipart body

        Multipart multipart = new Multipart("mixed");

        // a multipart may have a preamble
        multipart.setPreamble("This is a multi-part message in MIME format.");

        // first part is text/plain
        BodyFactory bodyFactory = new BodyFactory();
        BodyPart textPart = createTextPart(bodyFactory, "Why so serious?");
        multipart.addBodyPart(textPart);

        // second part is image/png (image is created on the fly)
        BufferedImage image = renderSampleImage();
        BodyPart imagePart = createImagePart(bodyFactory, image);
        multipart.addBodyPart(imagePart);

        // setMultipart also sets the Content-Type header field
        message.setMultipart(multipart);

        // 4) print message to standard output

        message.writeTo(System.out);

        // 5) message is no longer needed and should be disposed of

        message.dispose();
    }
View Full Code Here

    }
   
    public static void main(String[] args) {
        try {
           
            final Message message = new Message(new FileInputStream(args[0]));
           
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI(message);
                }
View Full Code Here

        DefaultStorageProvider.setInstance(storageProvider);

        // Create a template message. It would be possible to load a message
        // from an input stream but for this example a message object is created
        // from scratch for demonstration purposes.
        Message template = createTemplate();

        // Create a new message by transforming the template.
        Message transformed = transform(template);

        // Print transformed message.
        System.out.println("\n\nTransformed message:\n--------------------\n");
        transformed.writeTo(System.out);

        // Messages should be disposed of when they are no longer needed.
        // Disposing of a message also disposes of all child elements (e.g. body
        // parts) of the message.
        transformed.dispose();

        // Print original message to illustrate that it was not affected by the
        // transformation.
        System.out.println("\n\nOriginal template:\n------------------\n");
        template.writeTo(System.out);
View Full Code Here

     * Copies the given message and makes some arbitrary changes to the copy.
     */
    private static Message transform(Message original) throws IOException {
        // Create a copy of the template. The copy can be modified without
        // affecting the original.
        Message message = new Message(original);

        // In this example we know we have a multipart message. Use
        // Message#isMultipart() if uncertain.
        Multipart multipart = (Multipart) message.getBody();

        // Insert a new text/plain body part after every body part of the
        // template.
        final int count = multipart.getCount();
        for (int i = 0; i < count; i++) {
            String text = "Text inserted after part " + (i + 1);
            BodyPart bodyPart = createTextPart(text);
            multipart.addBodyPart(bodyPart, 2 * i + 1);
        }

        // For no particular reason remove the second binary body part (now
        // at index four).
        BodyPart removed = multipart.removeBodyPart(4);

        // The removed body part no longer has a parent entity it belongs to so
        // it should be disposed of.
        removed.dispose();

        // Set some headers on the transformed message
        message.createMessageId(HOSTNAME);
        message.setSubject("Transformed message");
        message.setDate(new Date());
        message.setFrom(Mailbox.parse("John Doe <jdoe@machine.example>"));

        return message;
    }
View Full Code Here

        multipart.addBodyPart(part2);

        BodyPart part3 = createRandomBinaryPart(300);
        multipart.addBodyPart(part3);

        Message message = new Message();
        message.setMultipart(multipart);

        message.setSubject("Template message");

        return message;
    }
View Full Code Here

TOP

Related Classes of org.apache.james.mime4j.message.Message$MessageBuilder

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.