有没有一种简单的方法在JPanel中显示电子邮件?

时间:2016-04-26 13:39:59

标签: java jpanel javamail

我试图在JPanel中显示使用JavaMail检索的电子邮件。但是我不知道如何正确地做到这一点。有没有一种简单的方法来识别邮件的内容并显示它们?现在我只需将我能从邮件中添加到TextArea中的任何内容,结果显然只是一些文本,但即使该文本也包含html代码。 感谢。

我的代码:

static void afisare(Message messages, JTextArea aux) {
    Message message = messages;
    try {
        writePart(message, aux);
    } catch (Exception e) {
        e.printStackTrace();
    }
    try {
        message.writeTo(System.out);
    } catch (IOException | MessagingException e) {
        e.printStackTrace();
    }
}

public static void writePart(Part p, JTextArea aux) throws Exception {
    if (p instanceof Message)
        // Call method writeEnvelope

    writeEnvelope((Message) p, aux);

    aux.append("----------------------------\n");
    aux.append("CONTENT-TYPE: " + p.getContentType());

    // check if the content is plain text
    if (p.isMimeType("text/plain")) {
        aux.append("This is plain text\n");
        aux.append("---------------------------\n");
        aux.append((String) p.getContent());
    }
    // check if the content has attachment
    else if (p.isMimeType("multipart/*")) {
        aux.append("This is a Multipart\n");
        aux.append("---------------------------");
        Multipart mp = (Multipart) p.getContent();
        int count = mp.getCount();
        for (int i = 0; i < count; i++)
            writePart(mp.getBodyPart(i), aux);
    }
    // check if the content is a nested message
    else if (p.isMimeType("message/rfc822")) {
        aux.append("This is a Nested Message\n");
        aux.append("---------------------------");
        writePart((Part) p.getContent(), aux);
    }
    // check if the content is an inline image
    else if (p.isMimeType("image/jpeg")) {

        byte[] bArray = new byte[5000];
        aux.append("--------> image/jpeg");
        Object o = p.getContent();

        InputStream x = (InputStream) o;
        // Construct the required byte array
        aux.append("x.length = " + x.available());
        int i;
        while ((i = (int) ((InputStream) x).available()) > 0) {
            int result = (int) (((InputStream) x).read(bArray));
            if (result == -1)
                bArray = new byte[x.available()];

            break;
        }
        FileOutputStream f2 = new FileOutputStream("/tmp/image.jpg");
        f2.write(bArray);
        x.close();
        f2.close();
    } else if (p.getContentType().contains("image/")) {
        aux.append("content type" + p.getContentType());
        File f = new File("image" + new Date(0).getTime() + ".jpg");
        DataOutputStream output = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(f)));
        com.sun.mail.util.BASE64DecoderStream test = (com.sun.mail.util.BASE64DecoderStream) p.getContent();
        byte[] buffer = new byte[1024];
        int bytesRead;
        while ((bytesRead = test.read(buffer)) != -1) {
            output.write(buffer, 0, bytesRead);
        }
        output.close();
    } else {
        Object o = p.getContent();
        if (o instanceof String) {
            aux.append("This is a string\n");
            aux.append("---------------------------\n");
            aux.append((String) o);
        } else if (o instanceof InputStream) {
            aux.append("This is just an input stream\n");
            aux.append("---------------------------\n");
            InputStream is = (InputStream) o;
            is = (InputStream) o;
            int c;
            while ((c = is.read()) != -1)
                System.out.write(c);
            is.close();
        } else {
            aux.append("This is an unknown type\n");
            aux.append("---------------------------\n");
            aux.append(o.toString());
        }
    }
}

public static void writeEnvelope(Message m, JTextArea aux) throws Exception {
    aux.append("This is the message envelope\n");
    aux.append("---------------------------\n");
    Address[] a;

    // FROM
    if ((a = m.getFrom()) != null) {
        for (int j = 0; j < a.length; j++)
            aux.append("FROM: " + a[j].toString() + "\n");
    }

    // TO
    if ((a = m.getRecipients(Message.RecipientType.TO)) != null) {
        for (int j = 0; j < a.length; j++)
            aux.append("TO: " + a[j].toString() + "\n");
    }

    // SUBJECT
    if (m.getSubject() != null);
        aux.append("SUBJECT: " + m.getSubject() + "\n");

}

0 个答案:

没有答案