Example usage for javax.mail.internet MimeMessage getSize

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

Introduction

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

Prototype

@Override
public int getSize() throws MessagingException 

Source Link

Document

Return the size of the content of this message in bytes.

Usage

From source file:org.apache.james.core.MimeMessageUtil.java

/**
 * Calculate the size of the give mimeMessage
 * //  w w  w. j  av  a  2s. c  o  m
 * @param message
 *            the MimeMessage
 * @return size the calculated size
 * @throws MessagingException
 *             if a problem occours while calculate the message size
 */
public static long calculateMessageSize(MimeMessage message) throws MessagingException {
    long size;
    // SK: Should probably eventually store this as a locally
    // maintained value (so we don't have to load and reparse
    // messages each time).
    size = message.getSize();
    if (size != -1) {
        Enumeration<?> e = message.getAllHeaderLines();
        if (e.hasMoreElements()) {
            size += 2;
        }
        while (e.hasMoreElements()) {
            // add 2 bytes for the CRLF
            size += ((String) e.nextElement()).length() + 2;
        }
    }

    if (size == -1) {
        SizeCalculatorOutputStream out = new SizeCalculatorOutputStream();
        try {
            message.writeTo(out);
        } catch (IOException e) {
            // should never happen as SizeCalculator does not actually throw
            // IOExceptions.
            throw new MessagingException("IOException wrapped by getMessageSize", e);
        }
        size = out.getSize();
    }
    return size;
}

From source file:org.apache.james.core.MimeMessageWrapper.java

public MimeMessageWrapper(MimeMessage original) throws MessagingException {
    this(Session.getDefaultInstance(System.getProperties()));
    flags = original.getFlags();/*  w w  w  . j  a va 2s. c  o m*/

    if (source == null) {
        InputStream in;

        boolean useMemoryCopy = false;
        String memoryCopy = System.getProperty(USE_MEMORY_COPY);
        if (memoryCopy != null) {
            useMemoryCopy = Boolean.valueOf(memoryCopy);
        }
        try {

            if (useMemoryCopy) {
                ByteArrayOutputStream bos;
                int size = original.getSize();
                if (size > 0) {
                    bos = new ByteArrayOutputStream(size);
                } else {
                    bos = new ByteArrayOutputStream();
                }
                original.writeTo(bos);
                bos.close();
                in = new SharedByteArrayInputStream(bos.toByteArray());
                parse(in);
                in.close();
                saved = true;
            } else {
                MimeMessageInputStreamSource src = new MimeMessageInputStreamSource(
                        "MailCopy-" + UUID.randomUUID().toString());
                OutputStream out = src.getWritableOutputStream();
                original.writeTo(out);
                out.close();
                source = src;
            }

        } catch (IOException ex) {
            // should never happen, but just in case...
            throw new MessagingException("IOException while copying message", ex);
        }
    }
}

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

public void handle() throws Exception {
    MailProcessingRecord mailProcessingRecord = prepareRecord();

    MimeMessage message = loadMessage();

    // do we _really_ have to handle this?
    if (!MailMatchingUtils.isMatchCandidate(message))
        return;//from   www.  ja  va2s  .co m

    String id = MailMatchingUtils.getMailIdHeader(message);
    try {
        mailProcessingRecord.setByteReceivedTotal(message.getSize());

        mailProcessingRecord.setMailId(id);
        mailProcessingRecord.setSubject(message.getSubject());

        mailProcessingRecord.setTimeFetchEnd(System.currentTimeMillis());

    } catch (MessagingException e) {
        log.info(queue + ": failed to process mail. remains on server");
        return;
    } finally {
        MailProcessingRecord matchedAndMergedRecord = results.matchMailRecord(mailProcessingRecord);
        if (matchedAndMergedRecord != null) {
            MailMatchingUtils.validateMail(message, matchedAndMergedRecord);
            results.recordValidatedMatch(matchedAndMergedRecord);
        }
    }

    dismissMessage();
}

From source file:org.apache.james.server.core.MimeMessageUtil.java

/**
 * Calculate the size of the give mimeMessage
 * //from   www  .  ja  v  a2  s. c o  m
 * @param message
 *            the MimeMessage
 * @return size the calculated size
 * @throws MessagingException
 *             if a problem occours while calculate the message size
 */
public static long calculateMessageSize(MimeMessage message) throws MessagingException {
    long size;
    // SK: Should probably eventually store this as a locally
    // maintained value (so we don't have to load and reparse
    // messages each time).
    size = message.getSize();
    if (size != -1) {
        Enumeration<String> e = message.getAllHeaderLines();
        if (e.hasMoreElements()) {
            size += 2;
        }
        while (e.hasMoreElements()) {
            // add 2 bytes for the CRLF
            size += e.nextElement().length() + 2;
        }
    }

    if (size == -1) {
        SizeCalculatorOutputStream out = new SizeCalculatorOutputStream();
        try {
            message.writeTo(out);
        } catch (IOException e) {
            // should never happen as SizeCalculator does not actually throw
            // IOExceptions.
            throw new MessagingException("IOException wrapped by getMessageSize", e);
        }
        size = out.getSize();
    }
    return size;
}

From source file:org.apache.james.transport.mailets.LogMessage.java

private int lengthToLog(MimeMessage message) throws MessagingException {
    return bodyMax > 0 ? bodyMax : message.getSize();
}

From source file:org.apache.jmeter.assertions.SMIMEAssertion.java

/**
 * extracts a MIME message from the SampleResult
 *///from   w w w.j  a  v a  2  s  . co  m
private static MimeMessage getMessageFromResponse(SampleResult response, int messageNumber)
        throws MessagingException {
    SampleResult[] subResults = response.getSubResults();

    if (messageNumber >= subResults.length || messageNumber < 0) {
        throw new MessagingException("Message number not present in results: " + messageNumber);
    }

    final SampleResult sampleResult = subResults[messageNumber];
    if (log.isDebugEnabled()) {
        log.debug("Bytes: " + sampleResult.getBytes() + " CT: " + sampleResult.getContentType());
    }
    byte[] data = sampleResult.getResponseData();
    Session session = Session.getDefaultInstance(new Properties());
    MimeMessage msg = new MimeMessage(session, new ByteArrayInputStream(data));

    log.debug("msg.getSize() = " + msg.getSize());
    return msg;
}

From source file:org.bonitasoft.connectors.email.test.EmailConnectorTest.java

@Test
public void sendASimpleEmail() throws BonitaException, MessagingException, InterruptedException {
    executeConnector(getBasicSettings());
    final List<WiserMessage> messages = server.getMessages();
    assertEquals(1, messages.size());//from  w  w w  . ja va2  s.com
    final WiserMessage message = messages.get(0);
    assertNotNull(message.getEnvelopeSender());
    assertEquals(ADDRESSJOHN, message.getEnvelopeReceiver());
    final MimeMessage mime = message.getMimeMessage();
    assertEquals(SUBJECT, mime.getSubject());
    assertEquals(0, mime.getSize());
}

From source file:org.bonitasoft.connectors.email.test.EmailConnectorTest.java

@Test
public void testSendEmailWithFromAddress() throws Exception {
    final Map<String, Object> parameters = getBasicSettings();
    parameters.put("from", ADDRESSMARK);
    executeConnector(parameters);/*from ww  w  .j  av a2  s .  com*/
    final List<WiserMessage> messages = server.getMessages();
    assertEquals(1, messages.size());
    final WiserMessage message = messages.get(0);
    assertEquals(ADDRESSMARK, message.getEnvelopeSender());
    assertEquals(ADDRESSJOHN, message.getEnvelopeReceiver());
    final MimeMessage mime = message.getMimeMessage();
    assertEquals(SUBJECT, mime.getSubject());
    assertEquals(0, mime.getSize());
}

From source file:org.bonitasoft.connectors.email.test.EmailConnectorTest.java

@Test
public void testSendEmailWithAutentication() throws Exception {
    final Map<String, Object> parameters = getBasicSettings();
    parameters.put("userName", "john");
    parameters.put("password", "doe");
    executeConnector(parameters);//from w w w.  j av  a 2 s.  com
    final List<WiserMessage> messages = server.getMessages();
    assertEquals(1, messages.size());
    final WiserMessage message = messages.get(0);
    assertNotNull(message.getEnvelopeSender());
    assertEquals(ADDRESSJOHN, message.getEnvelopeReceiver());
    final MimeMessage mime = message.getMimeMessage();
    assertEquals(SUBJECT, mime.getSubject());
    assertEquals(0, mime.getSize());
}

From source file:org.bonitasoft.connectors.email.test.EmailConnectorTest.java

@Test
public void sendEmailWithToRecipientsAddresses() throws Exception {
    final Map<String, Object> parameters = getBasicSettings();
    parameters.put("to", ADDRESSJOHN + ", " + ADDRESSPATTY);
    parameters.put("from", ADDRESSMARK);
    executeConnector(parameters);/*  w  ww . j av  a2  s.co m*/
    final List<WiserMessage> messages = server.getMessages();
    assertEquals(2, messages.size());
    WiserMessage message = messages.get(0);
    assertEquals(ADDRESSMARK, message.getEnvelopeSender());
    assertEquals(ADDRESSJOHN, message.getEnvelopeReceiver());
    MimeMessage mime = message.getMimeMessage();
    assertEquals(SUBJECT, mime.getSubject());
    assertEquals(0, mime.getSize());

    message = messages.get(1);
    assertEquals(ADDRESSMARK, message.getEnvelopeSender());
    assertEquals(ADDRESSPATTY, message.getEnvelopeReceiver());
    mime = message.getMimeMessage();
    assertEquals(SUBJECT, mime.getSubject());
    assertEquals(0, mime.getSize());
}