Java Code Examples for javax.mail.internet.MimeMessage#getSize()

The following examples show how to use javax.mail.internet.MimeMessage#getSize() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LogMessage.java    From james-project with Apache License 2.0 5 votes vote down vote up
private int messageSizeOrUnlimited(MimeMessage message) throws MessagingException {
    int computedSize = message.getSize();
    if (computedSize > 0) {
        return computedSize;
    }
    return Integer.MAX_VALUE;
}
 
Example 2
Source File: MimeMessageUtilTest.java    From james-project with Apache License 2.0 5 votes vote down vote up
@Test
public void testWriteMimeMessageMultipartWithMessageID() throws MessagingException, IOException {
    String message = "Received: from localhost.localdomain ([127.0.0.1])\r\n" +
    "          by athlon14 (JAMES SMTP Server 2.3-dev) with SMTP ID 694\r\n" +
    "          for <[email protected]>;\r\n" +
    "          Sat, 18 Feb 2006 19:30:53 +0100 (CET)\r\n" +
    "Subject: ext2int\r\n" +
    "X-James-Postage: This is a test mail sent by James Postage\r\n" +
    "Mime-Version: 1.0\r\n" +
    "Content-Type: multipart/alternative; boundary=\"XyoYyxCQIfmZ5Sxofid6XQVZt5Z09XtTnqBF4Z45XSA=\"\r\n" +
    "Date: Sat, 18 Feb 2006 19:30:53 +0100 (CET)\r\n" +
    "From: [email protected]\r\n" +
    "\r\n" +
    "\r\n" +
    "--XyoYyxCQIfmZ5Sxofid6XQVZt5Z09XtTnqBF4Z45XSA=\r\n" +
    "Content-Type: text/plain\r\n" +
    "\r\n" +
    "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\r\n" +
    "--XyoYyxCQIfmZ5Sxofid6XQVZt5Z09XtTnqBF4Z45XSA=\r\n" +
    "Content-Type: application/octet-stream\r\n" +
    "\r\n" +
    "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\r\n" +
    "--XyoYyxCQIfmZ5Sxofid6XQVZt5Z09XtTnqBF4Z45XSA=--\r\n";

    MimeMessage mimeMessage = new MimeMessage(Session.getDefaultInstance(new Properties()), new ByteArrayInputStream(message.getBytes()));
    mimeMessage.getSize();
    ByteArrayOutputStream headerOut = new ByteArrayOutputStream();
    ByteArrayOutputStream bodyOut = new ByteArrayOutputStream();
    MimeMessageUtil.writeTo(mimeMessage, headerOut, bodyOut);
}
 
Example 3
Source File: BayesianAnalysisFeeder.java    From james-project with Apache License 2.0 4 votes vote down vote up
/**
 * Scans the mail and updates the token frequencies in the database.
 * 
 * The method is synchronized in order to avoid too much database locking,
 * as thousands of rows may be updated just for one message fed.
 * 
 * @param mail
 *            The Mail message to be scanned.
 */
@Override
public void service(Mail mail) {
    boolean dbUpdated = false;

    mail.setState(Mail.GHOST);

    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    Connection conn = null;

    try {

        MimeMessage message = mail.getMessage();

        String messageId = message.getMessageID();

        if (message.getSize() > getMaxSize()) {
            LOGGER.debug("{} Feeding HAM/SPAM ignored because message size > {}: {}", messageId, getMaxSize(), message.getSize());
            return;
        }

        clearAllHeaders(message);

        message.writeTo(baos);

        BufferedReader br = new BufferedReader(new StringReader(baos.toString()));

        // this is synchronized to avoid concurrent update of the corpus
        synchronized (JDBCBayesianAnalyzer.DATABASE_LOCK) {

            conn = datasource.getConnection();

            if (conn.getAutoCommit()) {
                conn.setAutoCommit(false);
            }

            dbUpdated = true;

            // Clear out any existing word/counts etc..
            analyzer.clear();

            if ("ham".equalsIgnoreCase(feedType)) {
                LOGGER.debug("{} Feeding HAM", messageId);
                // Process the stream as ham (not spam).
                analyzer.addHam(br);

                // Update storage statistics.
                analyzer.updateHamTokens(conn);
            } else {
                LOGGER.debug("{} Feeding SPAM", messageId);
                // Process the stream as spam.
                analyzer.addSpam(br);

                // Update storage statistics.
                analyzer.updateSpamTokens(conn);
            }

            // Commit our changes if necessary.
            if (conn != null && dbUpdated && !conn.getAutoCommit()) {
                conn.commit();
                dbUpdated = false;
                LOGGER.debug("{} Training ended successfully", messageId);
                JDBCBayesianAnalyzer.touchLastDatabaseUpdateTime();
            }

        }

    } catch (java.sql.SQLException se) {
        LOGGER.error("SQLException: ", se);
    } catch (java.io.IOException ioe) {
        LOGGER.error("IOException: ", ioe);
    } catch (javax.mail.MessagingException me) {
        LOGGER.error("MessagingException: ", me);
    } finally {
        // Rollback our changes if necessary.
        try {
            if (conn != null && dbUpdated && !conn.getAutoCommit()) {
                conn.rollback();
                dbUpdated = false;
            }
        } catch (Exception e) {
            LOGGER.error("Failed to rollback after last error.", e);
        }
        theJDBCUtil.closeJDBCConnection(conn);
    }
}
 
Example 4
Source File: BayesianAnalysis.java    From james-project with Apache License 2.0 4 votes vote down vote up
/**
 * Scans the mail and determines the spam probability.
 * 
 * @param mail
 *            The Mail message to be scanned.
 * @throws MessagingException
 *             if a problem arises
 */
@Override
public void service(Mail mail) throws MessagingException {

    try {
        MimeMessage message = mail.getMessage();

        if (ignoreLocalSender && isSenderLocal(mail)) {
            // ignore the message if the sender is local
            return;
        }

        String[] headerArray = message.getHeader(headerName);
        // ignore the message if already analyzed
        if (headerArray != null && headerArray.length > 0) {
            return;
        }

        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        double probability;

        if (message.getSize() < getMaxSize()) {
            message.writeTo(baos);
            probability = analyzer.computeSpamProbability(new BufferedReader(new StringReader(baos.toString())));
        } else {
            probability = 0.0;
        }

        mail.setAttribute(new Attribute(MAIL_ATTRIBUTE_NAME, AttributeValue.of(probability)));
        message.setHeader(headerName, Double.toString(probability));

        DecimalFormat probabilityForm = (DecimalFormat) DecimalFormat.getInstance();
        probabilityForm.applyPattern("##0.##%");
        String probabilityString = probabilityForm.format(probability);

        String senderString = mail.getMaybeSender().asString("null");
        if (probability > 0.1) {
            final Collection<MailAddress> recipients = mail.getRecipients();
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug(headerName + ": " + probabilityString + "; From: " + senderString + "; Recipient(s): " + getAddressesString(recipients));
            }

            // Check if we should tag the subject
            if (tagSubject) {
                appendToSubject(message, " [" + probabilityString + (probability > 0.9 ? " SPAM" : " spam") + "]");
            }
        }

        saveChanges(message);

    } catch (Exception e) {
        LOGGER.error("Exception: {}", e.getMessage(), e);
        throw new MessagingException("Exception thrown", e);
    }
}