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, Exception e) 

Source Link

Document

Constructs a MessagingException with the specified Exception and detail message.

Usage

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

/**
 * Load the message headers from the internal source.
 * /*w ww.j a va 2s.c o m*/
 * @throws MessagingException
 *             if an error is encountered while loading the headers
 */
protected synchronized void loadHeaders() throws MessagingException {
    if (headers != null) {
        // Another thread has already loaded these headers
    } else if (source != null) {
        try {
            InputStream in = source.getInputStream();
            try {
                headers = createInternetHeaders(in);

            } finally {
                IOUtils.closeQuietly(in);
            }
        } catch (IOException ioe) {
            throw new MessagingException("Unable to parse headers from stream: " + ioe.getMessage(), ioe);
        }
    } else {
        throw new MessagingException(
                "loadHeaders called for a message with no source, contentStream or stream");
    }
}

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

/**
 * Load the complete MimeMessage from the internal source.
 * //from  ww w. j  a  v a2  s . co  m
 * @throws MessagingException
 *             if an error is encountered while loading the message
 */
public synchronized void loadMessage() throws MessagingException {
    if (messageParsed) {
        // Another thread has already loaded this message
    } else if (source != null) {
        sourceIn = null;
        try {
            sourceIn = source.getInputStream();

            parse(sourceIn);
            // TODO is it ok?
            saved = true;

        } catch (IOException ioe) {
            IOUtils.closeQuietly(sourceIn);
            sourceIn = null;
            throw new MessagingException("Unable to parse stream: " + ioe.getMessage(), ioe);
        }
    } else {
        throw new MessagingException("loadHeaders called for an unparsed message with no source");
    }
}

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

/**
 * Returns size of message, ie headers and content
 *///  w  w  w .  j a  v a 2 s  .  com
public long getMessageSize() throws MessagingException {
    if (source != null && !isModified()) {
        try {
            return source.getMessageSize();
        } catch (IOException ioe) {
            throw new MessagingException("Error retrieving message size", ioe);
        }
    } else {
        return MimeMessageUtil.calculateMessageSize(this);
    }
}

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

/**
 * @see javax.mail.internet.MimeMessage#getRawInputStream()
 */// w w  w. j  av  a 2s  .  c  om
@Override
public synchronized InputStream getRawInputStream() throws MessagingException {
    if (!messageParsed && !isModified() && source != null) {
        InputStream is;
        try {
            is = source.getInputStream();
            // skip the headers.
            new MailHeaders(is);
            return is;
        } catch (IOException e) {
            throw new MessagingException("Unable to read the stream: " + e.getMessage(), e);
        }
    } else {
        return super.getRawInputStream();
    }
}

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

/**
 * Return an {@link InputStream} which holds the full content of the
 * message. This method tries to optimize this call as far as possible. This
 * stream contains the updated {@link MimeMessage} content if something was
 * changed/*from   w ww  .  j  a  va  2 s  . c o  m*/
 * 
 * @return messageInputStream
 * @throws MessagingException
 */

public synchronized InputStream getMessageInputStream() throws MessagingException {
    if (!messageParsed && !isModified() && source != null) {
        try {
            return source.getInputStream();
        } catch (IOException e) {
            throw new MessagingException("Unable to get inputstream", e);
        }
    } else {
        try {

            // Try to optimize if possible to prevent OOM on big mails.
            // See JAMES-1252 for an example
            if (!bodyModified && source != null) {
                // ok only the headers were modified so we don't need to
                // copy the whole message content into memory
                InputStream in = source.getInputStream();

                // skip over headers from original stream we want to use the
                // in memory ones
                new MailHeaders(in);

                // now construct the new stream using the in memory headers
                // and the body from the original source
                return new SequenceInputStream(new InternetHeadersInputStream(getAllHeaderLines()), in);
            } else {
                // the body was changed so we have no other solution to copy
                // it into memory first :(
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                writeTo(out);
                return new ByteArrayInputStream(out.toByteArray());
            }
        } catch (IOException e) {
            throw new MessagingException("Unable to get inputstream", e);
        }
    }
}

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

/**
 * Place a mail on the spool for processing
 *
 * @param mail the mail to place on the spool
 *
 * @throws MessagingException if an exception is caught while placing the mail
 *                            on the spool
 *///from  w  ww  .  j av  a  2  s . c  o  m
public void sendMail(Mail mail) throws MessagingException {
    try {
        spool.store(mail);
    } catch (Exception e) {
        getLogger().error("Error storing message: " + e.getMessage(), e);
        try {
            spool.remove(mail);
        } catch (Exception ignored) {
            getLogger().error("Error removing message after an error storing it: " + e.getMessage(), e);
        }
        throw new MessagingException("Exception spooling message: " + e.getMessage(), e);
    }
    if (getLogger().isDebugEnabled()) {
        StringBuffer logBuffer = new StringBuffer(64).append("Mail ").append(mail.getName())
                .append(" pushed in spool");
        getLogger().debug(logBuffer.toString());
    }
}

From source file:org.apache.james.jdkim.mailets.DKIMSign.java

public void init() throws MessagingException {
    signatureTemplate = getInitParameter("signatureTemplate");
    String privateKeyString = getInitParameter("privateKey");
    String privateKeyPassword = getInitParameter("privateKeyPassword", null);
    forceCRLF = getInitParameter("forceCRLF", true);
    try {/*from  w w  w . j ava 2 s  .  c om*/
        PKCS8Key pkcs8 = new PKCS8Key(new ByteArrayInputStream(privateKeyString.getBytes()),
                privateKeyPassword != null ? privateKeyPassword.toCharArray() : null);
        privateKey = pkcs8.getPrivateKey();
        // privateKey = DKIMSigner.getPrivateKey(privateKeyString);
    } catch (NoSuchAlgorithmException e) {
        throw new MessagingException("Unknown private key algorythm: " + e.getMessage(), e);
    } catch (InvalidKeySpecException e) {
        throw new MessagingException(
                "PrivateKey should be in base64 encoded PKCS8 (der) format: " + e.getMessage(), e);
    } catch (GeneralSecurityException e) {
        throw new MessagingException("General security exception: " + e.getMessage(), e);
    }
}

From source file:org.apache.james.jdkim.mailets.DKIMSign.java

public void service(Mail mail) throws MessagingException {
    DKIMSigner signer = new DKIMSigner(getSignatureTemplate(), getPrivateKey());
    SignatureRecord signRecord = signer.newSignatureRecordTemplate(getSignatureTemplate());
    try {// w  ww  . j av a  2s . c o m
        BodyHasher bhj = signer.newBodyHasher(signRecord);
        MimeMessage message = mail.getMessage();
        Headers headers = new MimeMessageHeaders(message);
        try {
            OutputStream os = new HeaderSkippingOutputStream(bhj.getOutputStream());
            if (forceCRLF)
                os = new CRLFOutputStream(os);
            message.writeTo(os);
            bhj.getOutputStream().close();
        } catch (IOException e) {
            throw new MessagingException("Exception calculating bodyhash: " + e.getMessage(), e);
        }
        String signatureHeader = signer.sign(headers, bhj);
        // Unfortunately JavaMail does not give us a method to add headers
        // on top.
        // message.addHeaderLine(signatureHeader);
        prependHeader(message, signatureHeader);
    } catch (PermFailException e) {
        throw new MessagingException("PermFail while signing: " + e.getMessage(), e);
    }

}

From source file:org.apache.james.mailrepository.file.FileMailRepository.java

@Override
public Mail retrieve(String key) throws MessagingException {
    try {//from   w w w  . ja  v a2 s .com
        Mail mc;
        try {
            mc = (Mail) objectRepository.get(key);
        } catch (RuntimeException re) {
            StringBuilder exceptionBuffer = new StringBuilder(128);
            if (re.getCause() instanceof Error) {
                exceptionBuffer.append("Error when retrieving mail, not deleting: ").append(re.toString());
            } else {
                exceptionBuffer.append("Exception retrieving mail: ").append(re.toString())
                        .append(", so we're deleting it.");
                remove(key);
            }
            final String errorMessage = exceptionBuffer.toString();
            getLogger().warn(errorMessage);
            getLogger().debug(errorMessage, re);
            return null;
        }
        MimeMessageStreamRepositorySource source = new MimeMessageStreamRepositorySource(streamRepository,
                destination, key);
        mc.setMessage(new MimeMessageCopyOnWriteProxy(source));

        return mc;
    } catch (Exception me) {
        getLogger().error("Exception retrieving mail: " + me);
        throw new MessagingException("Exception while retrieving mail: " + me.getMessage(), me);
    }
}

From source file:org.apache.james.mailrepository.jcr.JCRMailRepository.java

public Iterator<String> list() throws MessagingException {
    try {// w  ww  .  j  a v  a  2 s  . c o  m
        Session session = login();
        try {
            Collection<String> keys = new ArrayList<String>();
            QueryManager manager = session.getWorkspace().getQueryManager();
            @SuppressWarnings("deprecation")
            Query query = manager.createQuery("/jcr:root/" + MAIL_PATH + "//element(*,james:mail)",
                    Query.XPATH);
            NodeIterator iterator = query.execute().getNodes();
            while (iterator.hasNext()) {
                String name = iterator.nextNode().getName();
                keys.add(Text.unescapeIllegalJcrChars(name));
            }
            return keys.iterator();
        } finally {
            session.logout();
        }
    } catch (RepositoryException e) {
        throw new MessagingException("Unable to list messages", e);
    }
}