无法使用JavaMail下载PDF附件和java(Unable to download PDF attachment with java by using JavaMail)

编程入门 行业动态 更新时间:2024-10-12 10:25:04
无法使用JavaMail下载PDF附件和java(Unable to download PDF attachment with java by using JavaMail)

我正在使用JavaMail API创建电子邮件客户端。 一切正常,就像我能够连接到邮件服务器(使用IMAP),删除邮件,检索收到的邮件并将其显示给用户等。

现在问题来自下载“PDF附件”。 PDF文件没有完全下载...它缺少一些包含。

如果我使用IE或任何其他网络浏览器下载附件时某些PDF附件的大小为38 Kb,但是当我使用我的java代码下载它时它的大小为37.3 Kb。 它不完整因此,当我尝试使用Adobe Reader打开它时,它显示错误消息“文件已损坏......”

这是我写的下载附件的代码:

public boolean saveFile(String filename,Part part) throws IOException, MessagingException { boolean ren = true; FileOutputStream fos = null; BufferedInputStream fin = null; InputStream input = part.getInputStream(); File pdffile = new File("d:/"+filename); try{ if(!pdffile.exists()){ fos = new FileOutputStream(pdffile); fin = new BufferedInputStream(input); int size = 512; byte[] buf = new byte[size]; int len; while ( (len = fin.read(buf)) != -1 ) { fos.write(buf, 0, len); } input.close(); fos.close(); }else{ System.out.println("File already exists"); } }catch(Exception e ){ ren = false; } return ren; }

我错过了什么吗? 任何有用的帮助表示赞赏。

I am creating Email client using JavaMail API. Everything is working fine like I am able to connect to mail server(using IMAP), Delete mail, retrieving received mails and displaying them to user etc.

Now problem comes when it comes to download "PDF Attachments". PDF files are not downloading completely... it is missing some contains.

If some PDF attachment is of size 38 Kb when I am downloading attachment using IE or any other web browser but when I am downloading it using my java code it is of size 37.3 Kb. It is not complete Hence when I try to open it using Adobe Reader it shows error message that "File is corrupted..."

Here is code I have written to download attachment:

public boolean saveFile(String filename,Part part) throws IOException, MessagingException { boolean ren = true; FileOutputStream fos = null; BufferedInputStream fin = null; InputStream input = part.getInputStream(); File pdffile = new File("d:/"+filename); try{ if(!pdffile.exists()){ fos = new FileOutputStream(pdffile); fin = new BufferedInputStream(input); int size = 512; byte[] buf = new byte[size]; int len; while ( (len = fin.read(buf)) != -1 ) { fos.write(buf, 0, len); } input.close(); fos.close(); }else{ System.out.println("File already exists"); } }catch(Exception e ){ ren = false; } return ren; }

Am I missing something? Any useful help is appreciated.

最满意答案

花了几个小时,终于搞清楚了。

props.setProperty("mail.imaps.partialfetch", "false");

为我做了。 与上面的@Shantanu几乎相同,但因为我正在使用

store = session.getStore("imaps");

我也需要使用“imap s ”来进行partialfetch。

奇迹般有效。

完整代码如下:

// Load mail properties Properties mailProperties = System.getProperties(); mailProperties.put("mail.mime.base64.ignoreerrors", "true"); mailProperties.put("mail.imaps.partialfetch", "false"); // Connect to Gmail Session session = Session.getInstance(mailProperties, null); store = session.getStore("imaps"); store.connect("imap.gmail.com", -1, "username", "password"); // Access label folder Folder defaultFolder = store.getDefaultFolder(); Folder labelFolder = defaultFolder.getFolder("mylabel"); labelFolder.open(Folder.READ_WRITE); Message[] messages = labelFolder.getMessages(); saveAttachments(messages);

...

private void saveAttachments(Message[] messages) throws Exception { for (Message msg : messages) { if (msg.getContent() instanceof Multipart) { Multipart multipart = (Multipart) msg.getContent(); for (int i = 0; i < multipart.getCount(); i++) { Part part = multipart.getBodyPart(i); String disposition = part.getDisposition(); if ((disposition != null) && ((disposition.equalsIgnoreCase(Part.ATTACHMENT) || (disposition.equalsIgnoreCase(Part.INLINE))))) { MimeBodyPart mimeBodyPart = (MimeBodyPart) part; String fileName = mimeBodyPart.getFileName(); File fileToSave = new File(fileName); mimeBodyPart.saveFile(fileToSave); } } } } }

Finally I found solution at JavaMail FAQ Reading Mail, IMAP section Gmail server is running bug with attachments

First I tried to set partialfetch property false but sometimes it works sometimes it doesn't

props.setProperty("mail.imap.partialfetch", "false");

There is another way listed in FAQ which is just use copy constructor of MimeMessage and store orignal object in some tempmsg and then get content of tempmsg

MimeMessage tempmsg = new MimeMessage(msg); Multipart part = (Multipart) tempmsg.getContent();

and now perform all operations it should work..

For detailed information about what actually happens goto JavaMail FAQ Reading Mail, IMAP section you will find all answers..

更多推荐

本文发布于:2023-07-15 05:46:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1110770.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:附件   PDF   JavaMail   java   attachment

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!