Example usage for javax.mail.internet MimeMessage getContent

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

Introduction

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

Prototype

@Override
public Object getContent() throws IOException, MessagingException 

Source Link

Document

Return the content as a Java object.

Usage

From source file:it.ozimov.springboot.templating.mail.utils.EmailToMimeMessageTest.java

public static void validateBody(final Email email, final MimeMessage sentMessage)
        throws MessagingException, IOException {
    assertThat(sentMessage.getContent(), is(email.getBody()));
}

From source file:it.greenvulcano.gvesb.virtual.utils.MimeMessageHelper.java

public static Body getMessageBody(MimeMessage message, String mimeType) {
    try {/* w w  w  .j  a v  a 2s  . c  om*/

        if (message.getContent() instanceof Multipart) {

            Multipart multipartMessage = (Multipart) message.getContent();

            for (int i = 0; i < multipartMessage.getCount(); i++) {
                BodyPart bodyPart = multipartMessage.getBodyPart(i);

                if (bodyPart.isMimeType(mimeType) && (Part.INLINE.equalsIgnoreCase(bodyPart.getDisposition())
                        || Objects.isNull(bodyPart.getDisposition()))) {

                    return new Body(bodyPart.getContentType(), bodyPart.getContent().toString());

                }
            }
        } else {

            return new Body(message.getContentType(), message.getContent().toString());
        }

    } catch (Exception e) {
        // do nothing
    }

    return null;
}

From source file:it.greenvulcano.gvesb.virtual.utils.MimeMessageHelper.java

public static List<Attachment> getMessageAttachments(MimeMessage message) {

    List<Attachment> attachments = new LinkedList<>();

    try {/* w  w w .j a  va  2 s .  co  m*/

        Multipart multipartMessage = (Multipart) message.getContent();

        for (int i = 0; i < multipartMessage.getCount(); i++) {
            BodyPart bodyPart = multipartMessage.getBodyPart(i);

            if (bodyPart.getDisposition() != null
                    && bodyPart.getDisposition().equalsIgnoreCase(Part.ATTACHMENT)) {
                byte[] content = IOUtils.toByteArray(bodyPart.getInputStream());
                attachments.add(new Attachment(bodyPart.getContentType(), bodyPart.getFileName(),
                        Base64.encodeBase64String(content)));
            }
        }

    } catch (Exception e) {
        // do nothing
    }

    return attachments;

}

From source file:mitm.common.mail.BodyPartUtils.java

/**
 * Searches for an embedded RFC822 messages. Returns the first embedded RFC822 it finds.
 * Only one level deep is searched./* www . j  ava 2 s.  c  o m*/
 *  
 * Returns null if there are no embedded message.
 */
public static MimeMessage searchForRFC822(MimeMessage message) throws MessagingException, IOException {
    /*
     * Fast fail. Only multipart mixed messages are supported. 
     */
    if (!message.isMimeType("multipart/mixed")) {
        return null;
    }

    Multipart mp;

    try {
        mp = (Multipart) message.getContent();
    } catch (IOException e) {
        throw new MessagingException("Error getting message content.", e);
    }

    MimeMessage embeddedMessage = null;

    for (int i = 0; i < mp.getCount(); i++) {
        BodyPart part = mp.getBodyPart(i);

        if (part.isMimeType("message/rfc822")) {
            embeddedMessage = BodyPartUtils.extractFromRFC822(part);

            break;
        }
    }

    return embeddedMessage;
}

From source file:com.zimbra.cs.util.SpamExtract.java

private static void writeAttachedMessages(MimeMessage mm, File outdir, String msgUri)
        throws IOException, MessagingException {
    // Not raw - ignore the spam report and extract messages that are in attachments...
    if (!(mm.getContent() instanceof MimeMultipart)) {
        LOG.warn("Spam/notspam messages must have attachments (skipping " + msgUri + ")");
        return;/*from  w w w . j a  v  a2  s. co m*/
    }

    MimeMultipart mmp = (MimeMultipart) mm.getContent();
    int nAttachments = mmp.getCount();
    boolean foundAtleastOneAttachedMessage = false;
    for (int i = 0; i < nAttachments; i++) {
        BodyPart bp = mmp.getBodyPart(i);
        if (!bp.isMimeType("message/rfc822")) {
            // Let's ignore all parts that are not messages.
            continue;
        }
        foundAtleastOneAttachedMessage = true;
        Part msg = (Part) bp.getContent(); // the actual message
        File file = new File(outdir, mOutputPrefix + "-" + mExtractIndex++);
        OutputStream os = null;
        try {
            os = new BufferedOutputStream(new FileOutputStream(file));
            if (msg instanceof MimeMessage) {
                //bug 74435 clone into newMsg so our parser has a chance to handle headers which choke javamail
                ZMimeMessage newMsg = new ZMimeMessage((MimeMessage) msg);
                newMsg.writeTo(os);
            } else {
                msg.writeTo(os);
            }
        } finally {
            os.close();
        }
        if (verbose)
            LOG.info("Wrote: " + file);
    }

    if (!foundAtleastOneAttachedMessage) {
        String msgid = mm.getHeader("Message-ID", " ");
        LOG.warn("message uri=" + msgUri + " message-id=" + msgid + " had no attachments");
    }
}

From source file:nl.surfnet.coin.shared.service.MockJavaMailSender.java

@Override
public void send(MimeMessage mimeMessage) throws MailException {
    try {/*  w w  w. ja v  a  2  s. c om*/
        String body = mimeMessage.getContent().toString();
        logger.info("Subject: " + mimeMessage.getSubject() + "\n" + "Message: " + body);
    } catch (Exception e) {
        logger.error("Something went wrong", e);
    }
}

From source file:it.jugpadova.mock.ParancoeMockMailSender.java

@Override
protected void doSend(MimeMessage[] mimeMessages, Object[] originalMessages) throws MailException {
    if (mimeMessages != null) {
        for (MimeMessage mimeMessage : mimeMessages) {
            try {
                logger.info(mimeMessage.getContent());
            } catch (Exception ex) {
                logger.error("Can't get message content", ex);
            }/*from   ww  w  .j ava2 s . c o  m*/
        }
    }
    if (originalMessages != null) {
        for (Object o : originalMessages) {
            logger.info(o);
        }
    }
}

From source file:com.angstoverseer.service.mail.MailServiceImpl.java

@Override
public void handleIncomingEmail(InputStream inputStream) {
    Properties props = new Properties();
    Session session = Session.getDefaultInstance(props, null);

    try {// www  .jav a 2 s.c  om
        MimeMessage mimeMessage = new MimeMessage(session, inputStream);

        Object messageContent = mimeMessage.getContent();
        String message;
        if (messageContent instanceof Multipart) {
            Multipart mp = (Multipart) messageContent;
            BodyPart bodyPart = mp.getBodyPart(0);
            InputStream is = bodyPart.getInputStream();
            message = convertStreamToString(is);
        } else {
            message = (String) messageContent;
        }

        Address sender = mimeMessage.getFrom()[0];
        final String commandOutput = commandService.process(message, extractEmail(sender.toString()));

        sendResponseMail(sender, commandOutput, mimeMessage.getSubject());
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:eu.openanalytics.rsb.EmailDepositITCase.java

private void verifyErrorResult(final MimeMessage rsbResponseMessage) throws IOException, MessagingException {
    final Multipart parts = (Multipart) rsbResponseMessage.getContent();

    final String responseBody = StringUtils
            .normalizeSpace(((MimeMultipart) getMailBodyPart(parts, "multipart/related").getContent())
                    .getBodyPart(0).getContent().toString());
    assertThat(StringUtils.containsIgnoreCase(responseBody, "error"), is(true));
}

From source file:com.glaf.mail.MxMailHelper.java

public Mail getMail(InputStream inputStream) {
    try {//w w  w  .j  a va 2s  .c om
        Properties props = new Properties();
        props.put("mail.pop3.host", "abcd.com");
        Session session = Session.getInstance(props);
        MimeMessage mimeMessage = new MimeMessage(session, inputStream);
        Object body = mimeMessage.getContent();
        Mail mail = new Mail();
        if (body instanceof Multipart) {
            processMultipart((Multipart) body, mail);
        } else {
            processPart(mimeMessage, mail);
        }
        return mail;
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
}