2010-01-11 28 views
5

Tôi không biết lý do tại sao tôi nhận được ngoại lệ sau khi đọc một mail với file đính kèm từ mail server:Thiếu đầu ranh giới ngoại lệ khi đọc tin nhắn với file đính kèm

Exception in thread "main" javax.mail.MessagingException: Missing start boundary 

     at javax.mail.internet.MimeMultipart.parsebm<MimeMultipart.java:872) 
     at javax.mail.internet.MimeMultipart.parse<MimeMultipart.java:493) 
     at javax.mail.internet.MimeMultipart.getCount<MimeMultipart.java:240) 
     at GetParts.handleMultipart(GetParts.java:57) 
     at GetParts.main(GetParts.java:42) 

Các tập tin mà tôi đang sử dụng để đọc những tin nhắn đó là:

import java.io.*; 
import java.util.Properties; 
import javax.mail.*; 
import javax.mail.internet.*; 

public class GetParts { 
    public static void main (String args[]) 
     throws Exception { 
    String host = args[0]; 
    String username = args[1]; 
    String password = args[2]; 

    // Get session 
    Properties props=new Properties(); 
    props.put("mail.mime.multipart.ignoremissingboundaryparamete",true); 
    Session session = Session.getInstance(
     props, null); 
ContentType ct=new ContentType(); 
    // Get the store 
    Store store = session.getStore("pop3"); 
    store.connect(host, username, password); 

    // Get folder 
    Folder folder = store.getFolder("INBOX"); 
    folder.open(Folder.READ_ONLY); 

    BufferedReader reader = new BufferedReader (
     new InputStreamReader(System.in)); 

    // Get directory 
    Message message[] = folder.getMessages(); 
    for (int i=0, n=message.length; i<n; i++) { 
     System.out.println(i + ": " 
     + message[i].getFrom()[0] 
     + "\t" + message[i].getSubject()); 
      //message[i].setHeader("Content-Type","multipart/mixed"); 
     System.out.println("Do you want to get the content? [YES to read/QUIT to end]"); 
     String line = reader.readLine(); 
     if ("YES".equals(line)) { 
     Object content = message[i].getContent(); 
     if (content instanceof Multipart) { 
      handleMultipart((Multipart)content); 
     } else { 
      handlePart(message[i]); 
     } 
     } else if ("QUIT".equals(line)) { 
     break; 
     } 
    } 

    // Close connection 
    folder.close(false); 
    store.close(); 
    } 
    public static void handleMultipart(Multipart multipart) 
     throws MessagingException, IOException { 
     System.out.println(multipart.getCount()); 
    for (int i=0, n=multipart.getCount(); i<n; i++) { 
     handlePart(multipart.getBodyPart(i)); 
    } 
    } 
    public static void handlePart(Part part) 
     throws MessagingException, IOException { 
    String disposition = part.getDisposition(); 
    System.out.println("Disposition "+disposition); 
    String contentType = part.getContentType(); 
    System.out.println("contentType "+contentType); 
    if (disposition == null) { // When just body 
     System.out.println("Null: " + contentType); 
     // Check if plain 
     if ((contentType.length() >= 10) && 
      (contentType.toLowerCase().substring(
      0, 10).equals("text/plain"))) { 
     part.writeTo(System.out); 
     } else { // Don't think this will happen 
     System.out.println("Other body: " + contentType); 
     part.writeTo(System.out); 
     } 
    } else if (disposition.equalsIgnoreCase(Part.ATTACHMENT)) { 
     System.out.println("Attachment: " + part.getFileName() + 
     " : " + contentType); 
     saveFile(part.getFileName(), part.getInputStream()); 
    } else if (disposition.equalsIgnoreCase(Part.INLINE)) { 
     System.out.println("Inline: " + 
     part.getFileName() + 
     " : " + contentType); 
     saveFile(part.getFileName(), part.getInputStream()); 
    } else { // Should never happen 
     System.out.println("Other: " + disposition); 
    } 
    } 
    public static void saveFile(String filename, 
     InputStream input) throws IOException { 
    if (filename == null) { 
     filename = File.createTempFile("xx", ".out").getName(); 
    } 
    // Do no overwrite existing file 
    File file = new File(filename); 
    for (int i=0; file.exists(); i++) { 
     file = new File(filename+i); 
    } 
    FileOutputStream fos = new FileOutputStream(file); 
    BufferedOutputStream bos = new BufferedOutputStream(fos); 

    BufferedInputStream bis = new BufferedInputStream(input); 
    int aByte; 
    while ((aByte = bis.read()) != -1) { 
     bos.write(aByte); 
    } 
    bos.flush(); 
    bos.close(); 
    bis.close(); 
    } 
} 
+0

Đăng dòng thông báo đầu vào đang gây ra sự cố (ít nhất là đến dấu phân cách một phần) –

+1

Tôi không nghĩ nó quan trọng nhưng bạn đã bỏ lỡ tham số "r" trong dòng 'props.put (" mail .mime.multipart.ignoremissingboundaryparamete ", true);' – Edd

Trả lời

7

Tôi vừa gặp vấn đề tương tự. Ranh giới được xác định trong Multipart Content-Type. Bạn có thể tìm thêm thông tin trong số source này. Bạn cũng có thể xem một trong các Tin nhắn hiện tại của mình bằng cách sử dụng chức năng getContentType(). Trong trường hợp của tôi, tôi nhận được kết quả này:

multipart/mixed; boundary=--boundary_25_2d74d02b-d0d6-4f28-a311-4d1b7d107417 

Chức năng getCount() sử dụng ranh giới này để phân tách tất cả các phần soạn nhiều phần. Có vẻ như có trường hợp ranh giới này bị hỏng.

Thuộc tính hệ thống mail.mime.multipart.ignoreexistingboundaryparameter thể được thiết lập là true để gây ra bất kỳ ranh giới sẽ bị loại bỏ và thay vào đó tìm kiếm một đường ranh giới trong thông điệp như với mail.mime.multipart.ignoremissingboundaryparameter.

Tôi đã làm theo hướng dẫn này và mọi thứ hoạt động tốt. Tôi đã thêm mã bên dưới:

System.setProperty("mail.mime.multipart.ignoreexistingboundaryparameter", "true"); 

Hy vọng điều đó sẽ hữu ích!

0

cố gắng đặt chế độ trên multipartEntityBuilder ví dụ: multipartEntityBuilder.setMode (HttpMultipartMode.RFC6532);

Các vấn đề liên quan