2010-01-11 7 views
5

내가 메일 서버에서 첨부 파일이있는 메일을 읽을 때 나는 다음과 같은 예외를 받고 있어요 이유를 알고하지 않습니다누락 시작 경계 예외 첨부 파일과 함께 메시지를 읽을 때

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) 

파일있는 I 해당 메시지를 읽으려면 다음을 사용하십시오.

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

문제의 원인이되는 입력 메시지 스트림 게시 (적어도 부분 구분 기호까지) –

+1

문제는 아니지만 'r'매개 변수가'props.put ("mail .mime.multipart.ignoremissingboundaryparamete ", true);' – Edd

답변

7

방금 ​​전에도 같은 문제가있었습니다. 경계는 Multipart Content-Type 내에서 지정됩니다. 자세한 내용은 source에서 확인할 수 있습니다. getContentType() 기능을 사용하여 현재 메시지 중 하나를 볼 수도 있습니다.

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

는 그래서 getCount() 기능은 여러 부분을 구성하는 모든 부분을 분리하는이 경계를 사용하여 내 경우에는 내가이 결과를 얻었다. 이 경계가 손상 될 수있는 것처럼 보입니다.

mail.mime.multipart.ignoreexistingboundaryparameter 시스템 속성

는 무시 어떤 경계를 일으킬 대신 mail.mime.multipart.ignoremissingboundaryparameter와 같은 메시지의 경계선를 검색 할 true로 설정 될 수 있습니다.

나는이 지침을 따르고 모든 것이 올바르게 작동합니다. 아래 코드를 추가했습니다.

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

희망이 있습니다.

0

모드를 multipartEntityBuilder에 설정하려고 시도하십시오. 예 : multipartEntityBuilder.setMode (HttpMultipartMode.RFC6532);

관련 문제