Example usage for javax.mail.internet MimeMessage getDataHandler

List of usage examples for javax.mail.internet MimeMessage getDataHandler

Introduction

In this page you can find the example usage for javax.mail.internet MimeMessage getDataHandler.

Prototype

@Override
public synchronized DataHandler getDataHandler() throws MessagingException 

Source Link

Document

Return a DataHandler for this Message's content.

Usage

From source file:com.meltmedia.cadmium.email.TestingMessageTransformer.java

public MimeBodyPart newOriginalBodyPart(MimeMessage message) throws MessagingException {
    // get the data handler for this message.
    DataHandler handler = message.getDataHandler();

    // add the original body to the new body part.
    MimeBodyPart originalPart = new MimeBodyPart();
    originalPart.setDataHandler(handler);
    originalPart.setHeader("Content-ID", "original-message");

    return originalPart;
}

From source file:davmail.smtp.TestSmtp.java

public void sendAndCheckMessage(MimeMessage mimeMessage, String from, String bcc)
        throws IOException, MessagingException, InterruptedException {
    // generate message id
    mimeMessage.saveChanges();/*from w w  w.  jav a2s .  c  o  m*/
    // mimeMessage.writeTo(System.out);

    // copy Message-id to references header
    mimeMessage.addHeader("references", mimeMessage.getHeader("message-id")[0]);
    if (from != null) {
        writeLine("MAIL FROM:" + from);
    } else {
        writeLine("MAIL FROM:" + session.getEmail());
    }
    readLine();
    if (bcc != null) {
        writeLine("RCPT TO:" + bcc);
        readLine();
    }
    writeLine("RCPT TO:" + Settings.getProperty("davmail.to"));
    readLine();
    writeLine("DATA");
    assertEquals("354 Start mail input; end with <CRLF>.<CRLF>", readLine());
    mimeMessage.writeTo(new DoubleDotOutputStream(socketOutputStream));
    writeLine("");
    writeLine(".");
    assertEquals("250 Queued mail for delivery", readLine());
    // wait for asynchronous message send
    ExchangeSession.MessageList messages = null;
    for (int i = 0; i < 5; i++) {
        messages = session.searchMessages("Sent",
                session.headerIsEqualTo("references", mimeMessage.getMessageID()));
        if (messages.size() > 0) {
            break;
        }
        Thread.sleep(1000);
    }
    assertEquals(1, messages.size());
    ExchangeSession.Message sentMessage = messages.get(0);
    sentMessage.getMimeMessage().writeTo(System.out);
    assertEquals(mimeMessage.getDataHandler().getContent(),
            sentMessage.getMimeMessage().getDataHandler().getContent());
}

From source file:com.duroty.utils.mail.MessageUtilities.java

/**
 * DOCUMENT ME!/*from ww w  . j ava2 s .  c om*/
 *
 * @param from DOCUMENT ME!
 * @param to DOCUMENT ME!
 * @param subjectPrefix DOCUMENT ME!
 * @param subjectSuffix DOCUMENT ME!
 * @param msgText DOCUMENT ME!
 * @param message DOCUMENT ME!
 * @param session DOCUMENT ME!
 *
 * @return DOCUMENT ME!
 *
 * @throws Exception DOCUMENT ME!
 * @throws IllegalArgumentException DOCUMENT ME!
 */
public static MimeMessage createNewMessage(Address from, Address[] to, String subjectPrefix,
        String subjectSuffix, String msgText, MimeMessage message, Session session) throws Exception {
    if (from == null) {
        throw new IllegalArgumentException("from addres cannot be null.");
    }

    if ((to == null) || (to.length <= 0)) {
        throw new IllegalArgumentException("to addres cannot be null.");
    }

    if (message == null) {
        throw new IllegalArgumentException("to message cannot be null.");
    }

    try {
        //Create the message
        MimeMessage newMessage = new MimeMessage(session);

        StringBuffer buffer = new StringBuffer();

        if (subjectPrefix != null) {
            buffer.append(subjectPrefix);
        }

        if (message.getSubject() != null) {
            buffer.append(message.getSubject());
        }

        if (subjectSuffix != null) {
            buffer.append(subjectSuffix);
        }

        if (buffer.length() > 0) {
            newMessage.setSubject(buffer.toString());
        }

        newMessage.setFrom(from);
        newMessage.addRecipients(Message.RecipientType.TO, to);

        //Create your new message part
        BodyPart messageBodyPart1 = new MimeBodyPart();
        messageBodyPart1.setText(msgText);

        //Create a multi-part to combine the parts
        Multipart multipart = new MimeMultipart();
        multipart.addBodyPart(messageBodyPart1);

        //Create and fill part for the forwarded content
        BodyPart messageBodyPart2 = new MimeBodyPart();
        messageBodyPart2.setDataHandler(message.getDataHandler());

        //Add part to multi part
        multipart.addBodyPart(messageBodyPart2);

        //Associate multi-part with message
        newMessage.setContent(multipart);

        newMessage.saveChanges();

        return newMessage;
    } finally {
    }
}

From source file:org.apache.james.postage.mail.MailMatchingUtils.java

public static MimeMultipart convertToMimeMultipart(MimeMessage message) {
    try {// w  w w. ja v a2 s.c  o m
        return new MimeMultipart(message.getDataHandler().getDataSource());
    } catch (MessagingException e) {
        throw new RuntimeException("could not convert MimeMessage to MimeMultipart", e);
    }
}