简体   繁体   English

使用 javamail 读取同一邮件的邮件和附件名称

[英]read message and attachment name of the same mail using javamail

i need to use java to view mails.我需要使用java来查看邮件。 from these mails i want to know the name of the attachment (if there is one) and i want to know the message.从这些邮件中,我想知道附件的名称(如果有的话)并且我想知道消息。

is it possible to not only print the name of the attachment but to also print the contents of the mail?是否可以不仅打印附件名称,还可以打印邮件内容? currently i'm using the following code that get's only the name of the attachment or (if their is none) the message content.目前我正在使用以下代码,这些代码仅获取附件名称或(如果没有)消息内容。

 Message[] message = folder.getMessages();
 //Display message.
 for (int i = 0; i < message.length; i++) {

     System.out.println("------------ Message " + (i + 1) + " ------------");

     System.out.println("SentDate : " + message[i].getSentDate());
     System.out.println("From : " + message[i].getFrom()[0]);
     System.out.println("Subject : " + message[i].getSubject());


     Multipart multipart = (Multipart) message[i].getContent();

     for (int x = 0; x < multipart.getCount(); x++) {
         BodyPart bodyPart = multipart.getBodyPart(x);

         String disposition = bodyPart.getDisposition();

         if (disposition != null && (disposition.equals(BodyPart.ATTACHMENT))) {

             DataHandler handler = bodyPart.getDataHandler();
             System.out.println("Attachment : " + handler.getName());

         } else {
             System.out.println("Content: " + bodyPart.getContent());
         }
     }

Yes its possible.是的,它可能。 Try this example试试这个例子

Multipart mp = (Multipart)message.getContent();

for (int i=0, n=multipart.getCount(); i<n; i++) {
  Part part = multipart.getBodyPart(i));

  String disposition = part.getDisposition();

  if ((disposition != null) && 
      ((disposition.equals(Part.ATTACHMENT) || 
       (disposition.equals(Part.INLINE))) {
    saveFile(part.getFileName(), part.getInputStream());
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM