Java Mail-以字符串形式读取附件 [英] Java Mail - read in the attachment as String

查看:154
本文介绍了Java Mail-以字符串形式读取附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始使用Java Mail,并且对处理附件有一些疑问:

I am beginning to work with Java Mail and I have a few questions about processing of attachment:

  1. 如果我们的内容是Multipart,即具有附件,则实际附件位于哪个索引,在哪个位置可以找到消息内容?我只想只处理附件.

  1. If our content is a Multipart i.e. has an attachment, at which index is the actual attachment and at which can we find the message content? I simply want to process just attachments.

这是正确的方法吗?如果我想将给定的附件转换为字符串,只需阅读附件部分的流(使用getStream())并将其附加到字符串生成器,然后返回字符串?

Would it be a correct approach. if I wanted to convert a given attachment to a String, to simply read through the stream of the attachment part (using getStream()) and append it to String builder and then return a String?

非常感谢您提供任何建议

Thanks a lot for any advice

推荐答案

1.) 这来自我的一个应用程序(也基于其他人的代码,但我找不到功劳).

1.) This is from one of my applications (also based on someone else's code but i could not find to give credit).

如您所见,我正在递归处理Part对象(首先它实际上是一条消息).

As you can see i'm recursively processing a Part object (first it's actually a Message).

我删除了一些不相关的代码.

I removed some code which is irrelevant.

private BodyPartDOM collectBodyParts(Part part) throws IOException, MessagingException {

        BodyPartDOM dom = new BodyPartDOM();

        Object content = part.getContent();

        if (content instanceof String) {

            // process as string

        } else if (content instanceof Multipart) {


            Multipart innerMultiPart = (Multipart) content;
            int count = innerMultiPart.getCount();

            for (int i = 0; i < count; i++) {

                BodyPart innerBodyPart = innerMultiPart.getBodyPart(i);
                BodyPartDOM subDom = collectBodyParts(innerBodyPart);
                // further recursive processing

            }

        } else if (content instanceof InputStream) {

            // process inputStream

        }

        return dom;

    }

2.)如果将其转换为String,请继续.但是请注意文件流.

2.) If you convert it to a String go ahead. But watch out for file streams for example.

您可以检查mime类型的内容类型.以下是维基百科提供的一些信息,它们将对您有所帮助(http://en.wikipedia.org/wiki/MIME#Content-Type):

You can check the mime type for the content type. Here are some infor from wikipedia that will help (http://en.wikipedia.org/wiki/MIME#Content-Type):

Content-Type此标头指示 消息内容,例如,由类型和子类型组成

Content-Type This header indicates the Internet media type of the message content, consisting of a type and subtype, for example

Content-Type:文本/纯文本通过使用多部分类型MIME 允许邮件的各个部分以树状结构排列,其中 叶节点是任何非多部分内容类型,非叶节点 是多种多部分类型中的任何一种.该机制支持:

Content-Type: text/plain Through the use of the multipart type, MIME allows messages to have parts arranged in a tree structure where the leaf nodes are any non-multipart content type and the non-leaf nodes are any of a variety of multipart types. This mechanism supports:

使用文本/纯文本的简单文本消息(默认值 内容类型:")文本以及附件(多部分/与 文字/纯文字部分和其他非文字部分).包含以下内容的MIME消息: 附加文件通常用指示文件的原始名称. 标头为"Content-disposition:",因此文件类型均显示为 通过MIME内容类型和(通常是特定于操作系统的)文件名 带原始附件的扩展名回复(多部分/与 文本/纯文本部分,原始消息作为message/rfc822部分) 替代内容,例如以纯文本和 另一种格式,例如HTML(相同的多部分/替代格式 文本/纯文本和文本/html形式的内容)图像,音频,视频和 应用程序(例如,图像/jpeg,音频/mp3,视频/mp4和 应用程序/msword等)许多其他消息构造

simple text messages using text/plain (the default value for "Content-Type: ") text plus attachments (multipart/mixed with a text/plain part and other non-text parts). A MIME message including an attached file generally indicates the file's original name with the "Content-disposition:" header, so the type of file is indicated both by the MIME content-type and the (usually OS-specific) filename extension reply with original attached (multipart/mixed with a text/plain part and the original message as a message/rfc822 part) alternative content, such as a message sent in both plain text and another format such as HTML (multipart/alternative with the same content in text/plain and text/html forms) image, audio, video and application (for example, image/jpeg, audio/mp3, video/mp4, and application/msword and so on) many other message constructs

如何将其付诸实践可以在这里找到: http://www.oracle.com/technetwork/java/faq-135477.html

How to put that into practice can be found here: http://www.oracle.com/technetwork/java/faq-135477.html

祝您编程愉快!

这篇关于Java Mail-以字符串形式读取附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆