Example usage for javax.mail.internet MimeMessage getRawInputStream

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

Introduction

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

Prototype

public InputStream getRawInputStream() throws MessagingException 

Source Link

Document

Return an InputStream to the raw data with any Content-Transfer-Encoding intact.

Usage

From source file:com.sonicle.webtop.mail.Service.java

private String getTextContentAsString(Part p) throws IOException, MessagingException {
    java.io.InputStream istream = null;
    String charset = MailUtils.getCharsetOrDefault(p.getContentType());
    if (!java.nio.charset.Charset.isSupported(charset)) {
        charset = "ISO-8859-1";
    }/*from  w  w  w.j  a  v  a  2 s  . c om*/
    try {
        if (p instanceof javax.mail.internet.MimeMessage) {
            javax.mail.internet.MimeMessage mm = (javax.mail.internet.MimeMessage) p;
            istream = mm.getInputStream();
        } else if (p instanceof javax.mail.internet.MimeBodyPart) {
            javax.mail.internet.MimeBodyPart mm = (javax.mail.internet.MimeBodyPart) p;
            istream = mm.getInputStream();
        }
    } catch (Exception exc) { //unhandled format, get Raw data
        if (p instanceof javax.mail.internet.MimeMessage) {
            javax.mail.internet.MimeMessage mm = (javax.mail.internet.MimeMessage) p;
            istream = mm.getRawInputStream();
        } else if (p instanceof javax.mail.internet.MimeBodyPart) {
            javax.mail.internet.MimeBodyPart mm = (javax.mail.internet.MimeBodyPart) p;
            istream = mm.getRawInputStream();
        }
    }

    if (istream == null) {
        throw new IOException("Unknown message class " + p.getClass().getName());
    }

    java.io.BufferedReader br = new java.io.BufferedReader(new java.io.InputStreamReader(istream, charset));
    String line = null;
    StringBuffer sb = new StringBuffer();
    while ((line = br.readLine()) != null) {
        sb.append(line);
        sb.append("\n");
    }
    br.close();
    return sb.toString();
}