This discussion is archived
6 Replies Latest reply: Sep 12, 2012 4:34 PM by 960799 RSS

Java mail can't tell whether a mail has an attachment or not correctly?

960799 Newbie
Currently Being Moderated
i send mail through my progress with no attachment name, as a result, the 163 mail server rename the attachment to a bin file with no name property in the contenttype.
when i receive the mail and try to Part.getDisposition(), and i got null as the return. but as i see, the mail surely has an attachment and it's mimetype is application/octet-stream.
i was confused whether i coded wrong or not. and my code are as below:
     public Vector<Mail> receive(String folderName) throws MessagingException, IOException, InterruptedException {
          Vector<Mail> mail_v = new Vector<Mail>();
          
          MailParser mailParser;
          Mail mail;
          
          Session session = Session.getDefaultInstance(server.getMailServer(), user);
          Store store = session.getStore(this.urlName.getMailURLName());
          store.connect();
          Folder folder = store.getFolder(folderName);
          folder.open(Folder.READ_ONLY);
          
          Message message[] = folder.getMessages();
          int message_size = message.length;
          // System.out.println(message[310].getContentType());
          for(int i = 0; i < message_size; i++) {
               if(message.isMimeType("multipart/*")) {
                    System.out.println(i);
                    Multipart mu = (Multipart) message[i].getContent();
                    int mu_size = mu.getCount();
                    for(int j = 0; j < mu_size; j++) {
                         BodyPart mu_t = mu.getBodyPart(j);
                         if(! mu_t.isMimeType("multipart/*")) {
                         System.out.println("第" + i + "封邮件:" + message[i].getSubject() + " 第" + j + "个BodyPart的内容格式:" + mu_t.getContentType());
                              String dis = mu_t.getDisposition();
                              System.out.println(dis);
                         }
                         else {
                              // System.out.println("第" + i + "封邮件:" + message[i].getSubject() + " 第" + j + "个BodyPart的内容格式:" + mu_t.getContentType());
                         }
                    }
               }
               else {
                    // System.out.println("第" + i + "封邮件:" + message[i].getSubject() + " 的内容格式:" + message[i].getContentType());
               }
               Thread.sleep(150);
          }
          
          folder.close(true);
          store.close();
          
          return mail_v;
     }
Edited by: EJP on 8/09/2012 15:15: added {noformat}
{noformat} tags. Please use them.
  • 1. Re: Java mail can't tell whether a mail has an attachment or not correctly?
    EJP Guru
    Currently Being Moderated
                   if(message.isMimeType("multipart/*")) {
                        System.out.println(i);
                        Multipart mu = (Multipart) message[i].getContent();
    Object content = message.getContent();
    if (content implements Multipart) {
         System.out.println(i);
         Multipart mu = (Multipart) content;
                             if(! mu_t.isMimeType("multipart/*")) {
    Here it stops making sense. You are expecting a multipart within a multipart? At this point you already have a multipart, you are just looking at the contents of each part.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
  • 2. Re: Java mail can't tell whether a mail has an attachment or not correctly?
    960799 Newbie
    Currently Being Moderated
    thank you for answering my question.
    but i don't think you holding the right answer.
    you mean the content of a bodypart can't be a multipart?i don't think so, i have got a message whose content is multipart, while the first bodypart's contenttype is multipart.
    and i'm not sure you understand my intend of the code. what i do is getting the content after correcting it's contenttype.
  • 3. Re: Java mail can't tell whether a mail has an attachment or not correctly?
    bshannon Pro
    Currently Being Moderated
    Please read these FAQ entries:
    http://www.oracle.com/technetwork/java/javamail/faq/index.html#commonmistakes
    http://www.oracle.com/technetwork/java/javamail/faq/index.html#hasattach
    http://www.oracle.com/technetwork/java/javamail/faq/index.html#mainbody

    You can also use the msgshow.java demo program to dump out the structure of the message so you can see what to expect.
    And you can read the code of the msgshow.java demo program to see how to process complex messages.
  • 4. Re: Java mail can't tell whether a mail has an attachment or not correctly?
    960799 Newbie
    Currently Being Moderated
    thank you for answering my question.
    i finnaly get a little time for myself to check my code.
    and i find exectly how it happened.
    -----
              BodyPart bodyPart = new MimeBodyPart();
              FileDataSource fds = new FileDataSource(filePath);
              DataHandler dh = new DataHandler(fds);
              bodyPart.setDataHandler(dh);
              bodyPart.setFileName(MimeUtility.encodeWord(fileName));
              return bodyPart;
    -----
    when i Comments Off [*bodyPart.setFileName(MimeUtility.encodeWord(fileName));*],this will happen on 163 Mail Server.
    when i open the mail, i will find the attachment is renamed as ***.bin, when i get the contenttype, it will loss the name property, in the same time, the 163 mail server will show the attatchment as normal.
    before i open the mail in the mail "INBOX", the 163 server will not show that the mail has attachment as others who has attachment.
    i think what i do will not be happen when we send mails throgh the web pages or some other software like Outlook Express, this is only the mistake of my code.

    Edited by: 957796 on 2012-9-12 上午8:05
  • 5. Re: Java mail can't tell whether a mail has an attachment or not correctly?
    bshannon Pro
    Currently Being Moderated
    It sounds like the web user interface for the 163 mail server is using the "name" attribute
    to indicate that something is an attachment. That might be a good guess most of the time,
    but it's neither necessary nor sufficient to indicate an attachment. The correct way is to
    set the Content-Disposition for the body part, which is also missing from your code:
    bodyPart.setDisposition(Part.ATTACHMENT);
    Note that if you use the MimeBodyPart.attachFile() method, all of this is done for you:
    MimeBodyPart bodyPart = new MimeBodyPart();
    bodyPart.attachFile(filePath);
    return bodyPart;
  • 6. Re: Java mail can't tell whether a mail has an attachment or not correctly?
    960799 Newbie
    Currently Being Moderated
    thank you very much for your answer.
    i will use the method bodyPart.setDisposition(Part.ATTACHMENT);, i think it will do better when the code is detailed.
    i will also check the method bodyPart.attachFile(filePath);

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points