Example usage for javax.mail Message getContent

List of usage examples for javax.mail Message getContent

Introduction

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

Prototype

public Object getContent() throws IOException, MessagingException;

Source Link

Document

Return the content as a Java object.

Usage

From source file:it.greenvulcano.gvesb.virtual.smtp.SMTPCallOperationTest.java

/**
  * Tests email send//from   w  w  w  .  j  a v a 2 s  . c o  m
  *
  * @throws Exception
  *         if any error occurs
  */
public final void testSendEmail() throws Exception {
    Node node = XMLConfig.getNode("GVCore.xml", "//*[@name='SendEmail']");
    CallOperation op = new SMTPCallOperation();
    op.init(node);

    GVBuffer gvBuffer = new GVBuffer(TEST_SYSTEM, TEST_SERVICE);
    gvBuffer.setObject(TEST_MESSAGE);
    gvBuffer.setProperty("MAIL_SENDER", "SENDER ADDITIONAL INFO");

    op.perform(gvBuffer);

    //assertTrue(server.waitForIncomingEmail(5000, 1));

    Message[] messages = server.getReceivedMessages();
    assertEquals(1, messages.length);
    Message email = messages[0];
    Multipart mp = (Multipart) email.getContent();
    assertEquals("Notifica SendEmail", email.getSubject());
    assertEquals(TEST_MESSAGE, GreenMailUtil.getBody(mp.getBodyPart(0)));

    System.out.println("---------MAIL DUMP: START");
    System.out.println("Headers:\n" + GreenMailUtil.getHeaders(email));
    System.out.println("---------");
    System.out.println("Body:\n" + GreenMailUtil.getBody(email));
    System.out.println("---------MAIL DUMP: END");
    assertEquals("Notifica SendEmail", email.getHeader("Subject")[0]);
    assertEquals("1", email.getHeader("X-Priority")[0]);
}

From source file:it.greenvulcano.gvesb.virtual.smtp.SMTPCallOperationTest.java

/**
 * Tests email send/*from ww w. j  a va 2 s.  co m*/
 *
 * @throws Exception
 *         if any error occurs
 */
public final void testSendEmailDynamicDest() throws Exception {
    Node node = XMLConfig.getNode("GVCore.xml", "//*[@name='SendEmailDynamicDest']");
    CallOperation op = new SMTPCallOperation();
    op.init(node);

    GVBuffer gvBuffer = new GVBuffer(TEST_SYSTEM, TEST_SERVICE);
    gvBuffer.setObject(TEST_MESSAGE);

    gvBuffer.setProperty("GV_SMTP_TO", "test1@gv.com");
    gvBuffer.setProperty("GV_SMTP_CC", "test2@gv.com");
    gvBuffer.setProperty("GV_SMTP_BCC", "test3@gv.com");

    op.perform(gvBuffer);

    Message[] messages = server.getReceivedMessages();
    assertEquals(3, messages.length);
    Message email = messages[0];
    Multipart mp = (Multipart) email.getContent();
    assertEquals("Notifica SendEmailDynamicDest", email.getSubject());
    assertEquals(TEST_MESSAGE_1, GreenMailUtil.getBody(mp.getBodyPart(0)));

    System.out.println("---------MAIL DUMP: START");
    System.out.println("Headers:\n" + GreenMailUtil.getHeaders(email));
    System.out.println("---------");
    System.out.println("Body:\n" + GreenMailUtil.getBody(email));
    System.out.println("---------MAIL DUMP: END");
    assertEquals("Notifica SendEmailDynamicDest", email.getHeader("Subject")[0]);
    assertEquals("test1@gv.com", email.getHeader("To")[0]);
    assertEquals("test2@gv.com", email.getHeader("Cc")[0]);
}

From source file:com.mycompany.apsdtask.MessageToEmailConverter.java

EMail convert(Message message) {
    EMail eMail = new EMail();
    try {//  w w  w. j a v  a  2 s.c o m
        eMail.setAuthor(((InternetAddress) message.getFrom()[0]).getAddress());
        eMail.setDate(message.getSentDate());
        eMail.setSubject(message.getSubject());
        Object contnet = message.getContent();
        eMail.setBody((contnet instanceof MimeMultipart)
                ? ((MimeMultipart) contnet).getBodyPart(0).getContent().toString()
                : contnet.toString());
    } catch (IOException | MessagingException e) {
        throw new RuntimeException(e);
    }
    return eMail;
}

From source file:com.mycompany.apsdtask.domain.MessageToEmailConverter.java

@Override
public EMail convert(Message message) {
    EMail eMail = new EMail();
    try {//  www . ja va2s.  co  m
        eMail.setAuthor(((InternetAddress) message.getFrom()[0]).getAddress());
        eMail.setDate(message.getSentDate());
        eMail.setSubject(message.getSubject());
        Object contnet = message.getContent();
        eMail.setBody((contnet instanceof MimeMultipart)
                ? ((MimeMultipart) contnet).getBodyPart(0).getContent().toString()
                : contnet.toString());
    } catch (IOException | MessagingException e) {
        throw new RuntimeException(e);
    }
    return eMail;
}

From source file:org.codice.alliance.core.email.impl.EmailSenderImplTest.java

@Test
public void testAttachment() throws IOException, MessagingException {

    final List<String> attachmentLines = new LinkedList<>();
    Holder<String> filename = new Holder<>();

    int bodyPartIndex = 1;

    emailSender = new EmailSenderImpl() {
        @Override/* w ww . j  a  v  a  2  s. c o  m*/
        void send(Message message) throws MessagingException {
            try {
                FileInputStream fis = (FileInputStream) ((MimeMultipart) message.getContent())
                        .getBodyPart(bodyPartIndex).getContent();
                filename.value = ((MimeMultipart) message.getContent()).getBodyPart(bodyPartIndex)
                        .getFileName();
                attachmentLines.addAll(IOUtils.readLines(fis));
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    };

    emailSender.setMailHost(HOST);

    String attachedData = "attached data";
    String attachedFilename = "fileName.txt";

    emailSender.sendEmail(FROM_ADDR, TO_ADDR, SUBJECT_LINE, BODY, Collections.singletonList(
            new ImmutablePair<>(attachedFilename, new ByteArrayInputStream(attachedData.getBytes()))));

    assertThat(filename.value, is(attachedFilename));
    assertThat(attachmentLines, is(Collections.singletonList(attachedData)));
}

From source file:org.codice.alliance.core.email.impl.EmailSenderImplTest.java

/** Set the max attachment size to 1MB and send an attachment of size 2MB. */
@Test(expected = IOException.class)
public void testTooBigAttachment() throws IOException, MessagingException {

    final List<String> attachmentLines = new LinkedList<>();
    Holder<String> filename = new Holder<>();

    int bodyPartIndex = 1;

    emailSender = new EmailSenderImpl() {
        @Override//from w ww . jav a  2s  . co  m
        void send(Message message) throws MessagingException {
            try {
                FileInputStream fis = (FileInputStream) ((MimeMultipart) message.getContent())
                        .getBodyPart(bodyPartIndex).getContent();
                filename.value = ((MimeMultipart) message.getContent()).getBodyPart(bodyPartIndex)
                        .getFileName();
                attachmentLines.addAll(IOUtils.readLines(fis));
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    };

    emailSender.setMaxAttachmentSize(1);

    emailSender.setMailHost(HOST);

    String attachedData = generateString(2000000);
    String attachedFilename = "fileName.txt";

    emailSender.sendEmail(FROM_ADDR, TO_ADDR, SUBJECT_LINE, BODY, Collections.singletonList(
            new ImmutablePair<>(attachedFilename, new ByteArrayInputStream(attachedData.getBytes()))));
}

From source file:EmailBean.java

private void displayMessage(Message msg, PrintWriter out) throws MessagingException, IOException {

    if (msg != null && msg.getContent() instanceof String) {

        if (msg.getFrom()[0] instanceof InternetAddress) {
            out.println(/*from ww  w  . j  av  a2 s  . c o  m*/
                    "Message received from: " + ((InternetAddress) msg.getFrom()[0]).getAddress() + "<br />");
        }
        out.println("Message received on: " + msg.getReceivedDate() + "<br />");
        out.println("Message content type: " + msg.getContentType() + "<br />");
        out.println("Message content type: " + (String) msg.getContent());
    } else {

        out.println("<h2>The received email message was not of a text content type.</h2>");

    }

}

From source file:com.canoo.webtest.plugins.emailtest.EmailMessageStructureFilter.java

protected void filterContent(final Message message) throws MessagingException {
    if (!StringUtils.isEmpty(getHeaders())) {
        prepareHeaders();/*ww w.  ja  va 2  s  .co m*/
    }
    try {
        final Object content = message.getContent();
        if (content instanceof Multipart) {
            filterMultiPartMessage(content, message);
            return;
        }
        filterSimpleMessage((String) content, message);
    } catch (IOException e) {
        LOG.error("Error processing email message: ", e);
        throw new MessagingException("Error processing email message: " + e.getMessage());
    }
}

From source file:com.canoo.webtest.plugins.emailtest.EmailStoreHeader.java

/**
 * Calculate the result.//from www.  j a v  a2 s  . c  o  m
 *
 * @param message
 * @return The result
 */
protected String performOperation(final Message message) throws MessagingException {
    if (StringUtils.isEmpty(getPartIndex())) {
        return arrayToString(message.getHeader(getHeaderName()));
    }
    final Object content;
    try {
        content = message.getContent();
    } catch (IOException e) {
        LOG.error("Error processing email message: ", e);
        throw new MessagingException("Error processing email message: " + e.getMessage());
    }
    if (content instanceof Multipart) {
        final Multipart mp = (Multipart) content;
        final int part = ConversionUtil.convertToInt(getPartIndex(), 0);
        if (part >= mp.getCount()) {
            throw new StepFailedException("PartIndex too large.", this);
        }
        return arrayToString(mp.getBodyPart(part).getHeader(getHeaderName()));
    }
    throw new StepFailedException("PartIndex supplied for a non-MultiPart message.", this);
}

From source file:org.webguitoolkit.messagebox.mail.MailChannel.java

/**
 * Drag the content from the message into a String
 * /* w ww  .j  av  a2s . co  m*/
 * @param m
 * @return
 */
private String getContent(Message m) {
    try {
        Object content = m.getContent();
        if (content instanceof String) {
            return (String) content;
        } else if (content instanceof Multipart) {

            Multipart multipart = (Multipart) content;
            int partCount = multipart.getCount();
            StringBuffer result = new StringBuffer();

            for (int j = 0; j < partCount; j++) {

                BodyPart bodyPart = multipart.getBodyPart(j);
                String mimeType = bodyPart.getContentType();

                Object partContent = bodyPart.getContent();
                if (partContent instanceof String) {
                    result.append((String) partContent);
                } else if (partContent instanceof Multipart) {

                    Multipart innerMultiPartContent = (Multipart) partContent;
                    int innerPartCount = innerMultiPartContent.getCount();
                    result.append("*** It has " + innerPartCount + "further BodyParts in it ***");
                } else if (partContent instanceof InputStream) {
                    result.append(IOUtils.toString((InputStream) partContent));

                }
            } // End of for
            return result.toString();
        } else if (content instanceof InputStream)
            return IOUtils.toString((InputStream) content);

    } catch (Exception e) {
        return "ERROR reading mail content " + e.getMessage();
    }
    return "ERROR reading mail content - unknown format";
}