Example usage for javax.mail MessagingException MessagingException

List of usage examples for javax.mail MessagingException MessagingException

Introduction

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

Prototype

public MessagingException(String s) 

Source Link

Document

Constructs a MessagingException with the specified detail message.

Usage

From source file:org.apache.james.James.java

/**
 * Place a mail on the spool for processing
 *
 * @param sender the sender of the mail//  w  w w.  j  a v a  2  s. c om
 * @param recipients the recipients of the mail
 * @param msg an <code>InputStream</code> containing the message
 *
 * @throws MessagingException if an exception is caught while placing the mail
 *                            on the spool
 */
public void sendMail(MailAddress sender, Collection recipients, InputStream msg) throws MessagingException {
    // parse headers
    MailHeaders headers = new MailHeaders(msg);

    // if headers do not contains minimum REQUIRED headers fields throw Exception
    if (!headers.isValid()) {
        throw new MessagingException("Some REQURED header field is missing. Invalid Message");
    }
    ByteArrayInputStream headersIn = new ByteArrayInputStream(headers.toByteArray());
    sendMail(new MailImpl(getId(), sender, recipients, new SequenceInputStream(headersIn, msg)));
}

From source file:org.apache.james.mailetcontainer.lib.AbstractStateCompositeProcessor.java

/**
 * Service the given {@link Mail} by hand the {@link Mail} over the
 * {@link MailProcessor} which is responsible for the
 * {@link Mail#getState()}//from www  .j av a2 s  . c o  m
 */
public void service(Mail mail) throws MessagingException {
    long start = System.currentTimeMillis();
    MessagingException ex = null;
    MailProcessor processor = getProcessor(mail.getState());

    if (processor != null) {
        logger.debug("Call MailProcessor " + mail.getState());
        try {
            processor.service(mail);

            if (Mail.GHOST.equals(mail.getState())) {
                LifecycleUtil.dispose(mail);
            }
            /*
             * // check the mail needs further processing if
             * (Mail.GHOST.equalsIgnoreCase(mail.getState()) == false) {
             * service(mail); } else { LifecycleUtil.dispose(mail); }
             */
        } catch (MessagingException e) {
            ex = e;
            throw e;
        } finally {
            long end = System.currentTimeMillis() - start;
            for (CompositeProcessorListener listener : listeners) {
                listener.afterProcessor(processor, mail.getName(), end, ex);
            }
        }
    } else {
        throw new MessagingException(
                "No processor found for mail " + mail.getName() + " with state " + mail.getState());
    }
}

From source file:org.apache.james.mailrepository.lib.AbstractMailRepository.java

/**
 * @see org.apache.james.mailrepository.api.MailRepository#remove(String)
 *//*from   ww  w. ja  v a 2  s  . com*/
public void remove(String key) throws MessagingException {
    if (lock(key)) {
        try {
            internalRemove(key);
        } finally {
            unlock(key);
        }
    } else {
        String exceptionBuffer = "Cannot lock " + key + " to remove it";
        throw new MessagingException(exceptionBuffer.toString());
    }
}

From source file:org.apache.james.pop3server.POP3Handler.java

/**
 * Writes the content of the message, up to a total number of lines, out to 
 * an OutputStream.//from w  w  w. j a  v  a2s.  c om
 *
 * @param out the OutputStream to which to write the content
 * @param lines the number of lines to write to the stream
 *
 * @throws MessagingException if the MimeMessage is not set for this MailImpl
 * @throws IOException if an error occurs while reading or writing from the stream
 */
public void writeMessageContentTo(MimeMessage message, OutputStream out, int lines)
        throws IOException, MessagingException {
    String line;
    BufferedReader br;
    if (message != null) {
        br = new BufferedReader(new InputStreamReader(message.getRawInputStream()));
        try {
            while (lines-- > 0) {
                if ((line = br.readLine()) == null) {
                    break;
                }
                line += "\r\n";
                out.write(line.getBytes());
            }
        } finally {
            br.close();
        }
    } else {
        throw new MessagingException("No message set for this MailImpl.");
    }
}

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

private static void detectPossibleLoop(String currentName, int loopThreshold, char separator)
        throws MessagingException {
    long occurrences = currentName.chars().filter(c -> Chars.saturatedCast(c) == separator).count();

    // It looks like a configuration loop. It's better to stop.
    if (occurrences > loopThreshold) {
        throw new MessagingException(
                "Unable to create a new message name: too long. Possible loop in config.xml.");
    }// w  ww  . ja  v a2 s  . c om
}

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

/**
 * Writes the message out to an OutputStream.
 *
 * @param out the OutputStream to which to write the content
 * @throws MessagingException if the MimeMessage is not set for this MailImpl
 * @throws IOException        if an error occurs while reading or writing from the stream
 *//*from  w w w.j  a v  a 2 s .  co m*/
public void writeMessageTo(OutputStream out) throws IOException, MessagingException {
    if (message != null) {
        message.writeTo(out);
    } else {
        throw new MessagingException("No message set for this MailImpl.");
    }
}

From source file:org.apache.james.spamassassin.SpamAssassinInvoker.java

public SpamAssassinResult scanMailWithAdditionalHeaders(MimeMessage message, String... additionalHeaders)
        throws MessagingException {
    try (Socket socket = new Socket(spamdHost, spamdPort);
            OutputStream out = socket.getOutputStream();
            BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(out);
            PrintWriter writer = new PrintWriter(bufferedOutputStream);
            BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()))) {

        writer.write("CHECK SPAMC/1.2");
        writer.write(CRLF);/*from   w ww .j  av  a  2 s .  co m*/

        Arrays.stream(additionalHeaders).forEach(header -> {
            writer.write(header);
            writer.write(CRLF);
        });

        writer.write(CRLF);
        writer.flush();

        // pass the message to spamd
        message.writeTo(out);
        out.flush();
        socket.shutdownOutput();

        return in.lines().filter(this::isSpam).map(this::processSpam).findFirst()
                .orElse(SpamAssassinResult.empty());
    } catch (UnknownHostException e) {
        throw new MessagingException("Error communicating with spamd. Unknown host: " + spamdHost);
    } catch (IOException | MessagingException e) {
        throw new MessagingException("Error communicating with spamd on " + spamdHost + ":" + spamdPort, e);
    }
}

From source file:org.apache.james.spamassassin.SpamAssassinInvoker.java

private boolean reportMessageAs(InputStream message, User user, MessageClass messageClass)
        throws MessagingException {
    try (Socket socket = new Socket(spamdHost, spamdPort);
            OutputStream out = socket.getOutputStream();
            BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(out);
            PrintWriter writer = new PrintWriter(bufferedOutputStream);
            BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()))) {

        byte[] byteArray = IOUtils.toByteArray(message);
        writer.write("TELL SPAMC/1.2");
        writer.write(CRLF);//  w w w .  j ava2 s .c om
        writer.write("Content-length: " + byteArray.length);
        writer.write(CRLF);
        writer.write("Message-class: " + messageClass.value);
        writer.write(CRLF);
        writer.write("Set: local, remote");
        writer.write(CRLF);
        writer.write("User: " + user.asString());
        writer.write(CRLF);
        writer.write(CRLF);
        writer.flush();

        out.write(byteArray);
        out.flush();
        socket.shutdownOutput();

        return in.lines().anyMatch(this::hasBeenSet);
    } catch (UnknownHostException e) {
        throw new MessagingException("Error communicating with spamd. Unknown host: " + spamdHost);
    } catch (IOException e) {
        throw new MessagingException("Error communicating with spamd on " + spamdHost + ":" + spamdPort, e);
    }
}

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

@Override
public void init() throws MessagingException {
    sourceAttributeName = getInitParameter(SOURCE_ATTRIBUTE_PARAMETER_NAME,
            SOURCE_ATTRIBUTE_PARAMETER_DEFAULT_VALUE);
    if (Strings.isNullOrEmpty(sourceAttributeName)) {
        throw new MessagingException("source attribute cannot be empty");
    }/* ww  w .ja va2  s.co  m*/
    destinationAttributeName = getInitParameter(DESTINATION_ATTRIBUTE_PARAMETER_NAME,
            DESTINATION_ATTRIBUTE_PARAMETER_DEFAULT_VALUE);
    if (Strings.isNullOrEmpty(destinationAttributeName)) {
        throw new MessagingException("destination attribute cannot be empty");
    }
}

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

@Override
public void init() throws MessagingException {
    sourceAttributeName = getInitParameter(SOURCE_ATTRIBUTE_NAME, DEFAULT_SOURCE_ATTRIBUTE_NAME);
    rawSourceAttributeName = getInitParameter(RAW_SOURCE_ATTRIBUTE_NAME, DEFAULT_RAW_SOURCE_ATTRIBUTE_NAME);
    destinationAttributeName = getInitParameter(DESTINATION_ATTRIBUTE_NAME, DEFAULT_DESTINATION_ATTRIBUTE_NAME);
    if (Strings.isNullOrEmpty(sourceAttributeName)) {
        throw new MessagingException(
                SOURCE_ATTRIBUTE_NAME + " configuration parameter can not be null or empty");
    }/*w  w w . j av  a  2s .com*/
    if (Strings.isNullOrEmpty(rawSourceAttributeName)) {
        throw new MessagingException(
                RAW_SOURCE_ATTRIBUTE_NAME + " configuration parameter can not be null or empty");
    }
    if (Strings.isNullOrEmpty(destinationAttributeName)) {
        throw new MessagingException(
                DESTINATION_ATTRIBUTE_NAME + " configuration parameter can not be null or empty");
    }
}