Example usage for javax.mail Message getDataHandler

List of usage examples for javax.mail Message getDataHandler

Introduction

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

Prototype

public DataHandler getDataHandler() throws MessagingException;

Source Link

Document

Return a DataHandler for the content within this part.

Usage

From source file:com.ephesoft.dcma.mail.service.MailServiceImpl.java

@Override
public void sendMailWithPreviousMailContent(final MailMetaData mailMetaData, final String text,
        final Message previousMailMessage) {
    LOGGER.debug("Sending mail with content from previous mail(MailServiceImpl)");
    if (suppressMail) {
        return;/*from ww  w .  j a  v a  2 s .com*/
    }
    setMailProperties();
    MimeMessagePreparator messagePreparator = new MimeMessagePreparator() {

        public void prepare(MimeMessage mimeMessage) throws Exception {

            if (null != mailMetaData.getCcAddresses() && mailMetaData.getCcAddresses().size() > 0) {
                setAddressToMessage(RecipientType.CC, mimeMessage, mailMetaData.getCcAddresses());
            }
            if (null != mailMetaData.getBccAddresses() && mailMetaData.getBccAddresses().size() > 0) {
                setAddressToMessage(RecipientType.BCC, mimeMessage, mailMetaData.getBccAddresses());
            }
            if (null != mailMetaData.getToAddresses() && mailMetaData.getToAddresses().size() > 0) {
                setAddressToMessage(RecipientType.TO, mimeMessage, mailMetaData.getToAddresses());
            }

            if (null != mailMetaData.getFromAddress()) {
                mimeMessage.setFrom(new InternetAddress(new StringBuilder().append(mailMetaData.getFromName())
                        .append(MailConstants.LESS_SYMBOL).append(mailMetaData.getFromAddress())
                        .append(MailConstants.GREATER_SYMBOL).toString()));
            }

            if (null != mailMetaData.getSubject() && null != text) {
                mimeMessage.setSubject(mailMetaData.getSubject());
            }

            if (null != text) {
                mimeMessage.setText(text);
            }

            // Create your new message part
            BodyPart messageBodyPart = new MimeBodyPart();
            messageBodyPart.setText(EphesoftStringUtil.concatenate(text, ORIGINAL_MAIL_MESSAGE));

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

            // Create and fill part for the forwarded content
            messageBodyPart = new MimeBodyPart();
            messageBodyPart.setDataHandler(previousMailMessage.getDataHandler());

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

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

        }
    };
    try {
        mailSender.send(messagePreparator);
    } catch (MailException mailException) {
        LOGGER.error("Error while sending mail to configured mail account", mailException);
        throw new SendMailException(
                EphesoftStringUtil.concatenate("Error sending mail: ", mailMetaData.toString()), mailException);
    }
    LOGGER.info("mail sent successfully");
}

From source file:org.zilverline.core.IMAPCollection.java

/**
 * Index one message.//from  w  w w . j  av a 2  s .  c o  m
 */
private void indexMessage(final Document doc, final Message m) throws MessagingException, IOException {
    if (stopRequested) {
        log.info("Indexing stops, due to request");
        return;
    }
    final long uid = ((UIDFolder) m.getFolder()).getUID(m);

    // form a URL that mozilla seems to accept. Couldn't get it to accept
    // what I thought was the standard

    String urlPrefix = "imap://" + user + "@" + host + ":143/fetch%3EUID%3E/";

    final String url = urlPrefix + m.getFolder().getFullName() + "%3E" + uid;
    doc.add(Field.Text("name", url));

    final String subject = m.getSubject();
    final Date recv = m.getReceivedDate();
    final Date sent = m.getSentDate();
    log.info("Folder: " + m.getFolder().getFullName() + ": Message received " + recv + ", subject: " + subject);
    // -------------------------------------------------------
    // data gathered, now add to doc

    if (subject != null) {
        doc.add(Field.Text(F_SUBJECT, m.getSubject()));
        doc.add(Field.Text("title", m.getSubject()));
    }

    if (recv != null) {
        doc.add(Field.Keyword(F_RECEIVED, DateTools.timeToString(recv.getTime(), DateTools.Resolution.SECOND)));
    }

    if (sent != null) {
        doc.add(Field.Keyword(F_SENT, DateTools.timeToString(sent.getTime(), DateTools.Resolution.SECOND)));
        // store date as yyyyMMdd
        DateFormat df = new SimpleDateFormat("yyyyMMdd");
        String dfString = df.format(new Date(sent.getTime()));
        doc.add(Field.Keyword("modified", dfString));
    }

    doc.add(Field.Keyword(F_URL, url));

    Address[] addrs = m.getAllRecipients();
    if (addrs != null) {
        for (int j = 0; j < addrs.length; j++) {
            doc.add(Field.Keyword(F_TO, "" + addrs[j]));
        }
    }

    addrs = m.getFrom();
    if (addrs != null) {
        for (int j = 0; j < addrs.length; j++) {
            doc.add(Field.Keyword(F_FROM, "" + addrs[j]));
            doc.add(Field.Keyword("author", "" + addrs[j]));
        }
    }
    addrs = m.getReplyTo();
    if (addrs != null) {
        for (int j = 0; j < addrs.length; j++) {
            doc.add(Field.Keyword(F_REPLY_TO, "" + addrs[j]));
        }
    }

    doc.add(Field.Keyword(F_UID, "" + uid));

    // could ignore docs that have the deleted flag set
    for (int j = 0; j < FLAGS.length; j++) {
        boolean val = m.isSet(FLAGS[j]);
        doc.add(Field.Keyword(SFLAGS[j], (val ? "true" : "false")));
    }

    // now special case for mime
    if (m instanceof MimeMessage) {
        // mime++;
        MimeMessage mm = (MimeMessage) m;
        log.debug("index, adding MimeMessage " + m.getFileName());
        indexMimeMessage(doc, mm);

    } else {
        // nmime++;

        final DataHandler dh = m.getDataHandler();
        log.debug("index, adding (non-MIME) Content " + m.getFileName());
        doc.add(Field.Text(F_CONTENTS, new InputStreamReader(dh.getInputStream())));
    }
}

From source file:org.apache.axis2.transport.mail.MailRequestResponseClient.java

public IncomingMessage<byte[]> sendMessage(ClientOptions options, ContentType contentType, byte[] message)
        throws Exception {
    String msgId = sendMessage(contentType, message);
    Message reply = waitForReply(msgId);
    Assert.assertNotNull("No response received", reply);
    Assert.assertEquals(channel.getSender().getAddress(),
            ((InternetAddress) reply.getRecipients(Message.RecipientType.TO)[0]).getAddress());
    Assert.assertEquals(channel.getRecipient().getAddress(),
            ((InternetAddress) reply.getFrom()[0]).getAddress());
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    reply.getDataHandler().writeTo(baos);
    return new IncomingMessage<byte[]>(new ContentType(reply.getContentType()), baos.toByteArray());
}