How do I read a message with an attachment and save the attachment from the content string?

筅森魡賤 提交于 2019-12-02 15:05:18

问题


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?

Thanks

this what I got

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);
            }

回答1:


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.



来源:https://stackoverflow.com/questions/22609548/how-do-i-read-a-message-with-an-attachment-and-save-the-attachment-from-the-cont

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!