当前位置: 技术问答>java相关
用JavaMail接收邮件时如何判断邮件是否有附件?
来源: 互联网 发布时间:2017-03-25
本文导语: 用JavaMail接收邮件时如何判断邮件是否有附件? | Q: How do I read a message with an attachment and save the attachment? A: As described above, a message with an attachment is represented in MIME as a multip...
用JavaMail接收邮件时如何判断邮件是否有附件?
|
Q: How do I read a message with an attachment and save the attachment?
A: As described above, a message with an attachment is represented in MIME as a multipart message. In the simple case, the results of the Message object's getContent method will be a MimeMultipart object. The first body part of the multipart object wil be the main text of the message. The other body parts will be attachments. The msgshow.java demo program shows how to traverse all the multipart objects in a message and extract the data of each of the body parts. The getDisposition method will give you a hint as to whether the body part should be displayed inline or should be considered an attachment (but note that not all mailers provide this information).
To save the data in a body part into a file (for example), use the getInputStream method to access the attachment content and copy the data to a FileOutputStream.
Note that there are also more complicated cases to be handled as well. For example, some mailers send the main body as both plain text and html. This will typically appear as a multipart/alternative content (and a MimeMultipart object) in place of a simple text body part. Also, messages that are digitally signed or encrypted are even more complex. Handling all these cases can be challenging. Please refer to the various MIME specifications and other resources listed on our main page.
A: As described above, a message with an attachment is represented in MIME as a multipart message. In the simple case, the results of the Message object's getContent method will be a MimeMultipart object. The first body part of the multipart object wil be the main text of the message. The other body parts will be attachments. The msgshow.java demo program shows how to traverse all the multipart objects in a message and extract the data of each of the body parts. The getDisposition method will give you a hint as to whether the body part should be displayed inline or should be considered an attachment (but note that not all mailers provide this information).
To save the data in a body part into a file (for example), use the getInputStream method to access the attachment content and copy the data to a FileOutputStream.
Note that there are also more complicated cases to be handled as well. For example, some mailers send the main body as both plain text and html. This will typically appear as a multipart/alternative content (and a MimeMultipart object) in place of a simple text body part. Also, messages that are digitally signed or encrypted are even more complex. Handling all these cases can be challenging. Please refer to the various MIME specifications and other resources listed on our main page.
|
Object content = message.getContent();
if (content instanceof Multipart) {
handleMultipart((Multipart)content);//处理附件
} else {
handlePart(message);
}
if (content instanceof Multipart) {
handleMultipart((Multipart)content);//处理附件
} else {
handlePart(message);
}
|
Multipart mp =null;
Part part=mp.getBodyPart(j);
String disposition = part.getDisposition();
if ((disposition !=null) &&
((disposition.equals(Part.ATTACHMENT)||
(disposition.equals(Part.INLINE)))))
{//saveFile(part.getFileName(),part.getInputStream());
System.out.println("there is a fujian");
}
Part part=mp.getBodyPart(j);
String disposition = part.getDisposition();
if ((disposition !=null) &&
((disposition.equals(Part.ATTACHMENT)||
(disposition.equals(Part.INLINE)))))
{//saveFile(part.getFileName(),part.getInputStream());
System.out.println("there is a fujian");
}
|
这些方法都只是适合于附件不是中文,或者中文名较短的情况!
福建中文明成过长时就不是用了,因为javamail Api中的
part.getDisposition()根本就不能得到正确的数据了!!
比较保险的办法是-------
自己分析每一个part的 content-type
福建中文明成过长时就不是用了,因为javamail Api中的
part.getDisposition()根本就不能得到正确的数据了!!
比较保险的办法是-------
自己分析每一个part的 content-type