如何阅读带有附件的邮件并从内容字符串中保存附件? [英] How do I read a message with an attachment and save the attachment from the content string?

查看:44
本文介绍了如何阅读带有附件的邮件并从内容字符串中保存附件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 java 邮件从 gmail 获取带有附件的电子邮件,附件在内容中以字符串形式出现,我如何将其转换为文件?

I am using java mail to get emails from gmail with attachments, the attachment come as String in the content, how I can convert it to file?

谢谢

这是我得到的

Content :
begin 644 myfile.csv
M(E-T871U<R(L(E-T87)T(BPB4W1A<G0@9&%T92(L(E-T87)T('1I;64B+")%
M;F0B+")%;F0@9&%T92(L(D5N9"!T:6UE(BPB0V%L;&EN9R!C=7-T;VUE<B(L
.................
end

Object content = message.getContent();
            if (content instanceof String) {//here I got the attachment
                System.out.println(content);
            } else if (content instanceof Multipart) {
                Multipart multiPart = (Multipart) content;
                procesMultiPart(multiPart);
            }

推荐答案

看起来你有一个包含 uuencoded 内容的单部分消息.如果邮件的格式正确,并带有uuencode"的 Content-Transfer-Encoding 标头,JavaMail 会自动为您解码.但看起来不是.你可以自己解码:

Looks like you have a single part message with uuencoded content. If the message was properly formatted with a Content-Transfer-Encoding header of "uuencode", JavaMail would decode it automatically for you. But it looks like it is not. You can decode it yourself using:

InputStream is = MimeUtility.decode(message.getInputStream(), "uuencode");

然后读取输入流以获取解码后的数据,例如,将其复制到文件中.

Then read the input stream to get the decoded data, e.g., to copy it to a file.

这篇关于如何阅读带有附件的邮件并从内容字符串中保存附件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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