2014-12-09 8 views
6

multipart/mixed 메시지 내에 중첩/대체 메시지를 중첩 한 클라이언트로부터 전자 메일을 받고 있습니다. 메시지 본문을 가져 오면 실제로 multipart/alternative 레벨에 반환되는 text/html 부분 만 실제로 원하는 경우 multipart/대체 수준을 반환합니다.Java에서 Multipart/Mixed/Multipart/Alternative 본문 구문 분석

javax.mail에 대한 javadocs를 살펴본 결과, 자체 multipart 또는 두 번째 multipart/mixed 부분을 건너 뛰고 multipart/mixed 부분으로 이동하는 본문 부분의 본문을 가져 오는 간단한 방법을 찾을 수 없습니다. 대체 본문 텍스트/HTML 및 텍스트/일반 조각을 읽을 수 있습니다.

은 이메일 구조는 다음과 같습니다

Message [] found = fldr.search(searchCondition);   
for (int i = 0; i < found.length; i++) { 
    Message m = found[i]; 
    Object o = m.getContent(); 
    if (o instanceof Multipart) { 
     log.info("**This is a Multipart Message. "); 
     Multipart mp = (Multipart)o; 
     log.info("The Multipart message has " + mp.getCount() + " parts."); 
     for (int j = 0; j < mp.getCount(); j++) { 
      BodyPart b = mp.getBodyPart(j); 

      // Loop if the content type is multipart then get the content that is in that part, 
      // make it the new container and restart the loop in that part of the message. 
      if (b.getContentType().contains("multipart")) { 
       mp = (Multipart)b.getContent(); 
       j = 0; 
       continue; 
      } 

      log.info("This content type is " + b.getContentType()); 

      if(!b.getContentType().contains("text/html")) { 
       continue; 
      } 

      Object o2 = b.getContent(); 
      if (o2 instanceof String) { 
       <do things with content here> 
      } 
     } 
    } 
} 

두 번째 경계에서 중지 및 구문 분석하지 계속 나타납니다

... 
Content-Type: multipart/mixed; 
    boundary="----=_Part_19487_1145362154.1418138792683" 

------=_Part_19487_1145362154.1418138792683 
Content-Type: multipart/alternative; 
    boundary="----=_Part_19486_1391901275.1418138792683" 

------=_Part_19486_1391901275.1418138792683 
Content-Transfer-Encoding: 7bit 
Content-Type: text/plain; charset=ISO-8859-1 

... 

------=_Part_19486_1391901275.1418138792683 
Content-Transfer-Encoding: 7bit 
Content-Type: text/html; charset=ISO-8859-1 

... 

------=_Part_19486_1391901275.1418138792683-- 

------=_Part_19487_1145362154.1418138792683-- 

이것은 이메일을 구문 분석하는 데 사용되는 코드의 개요입니다 아무것도 더. 위의 메시지의 경우에는 boundary = "---- = _ Part_19486_1391901275.1418138792683"에서 멈추고 메시지 텍스트에는 도달하지 않습니다.

+1

'log.info ("이 콘텐츠 유형은"+ b.getContentType()) "입니까? – ToYonos

+1

"이 콘텐츠 유형은 다중/대체입니다." 작동 할 때 "이 콘텐츠 유형은 text/html, charset = ISO-8859-1" –

+1

때마다 각 루프마다 차례가됩니까? – ToYonos

답변

2

는 :

if (b.getContentType().contains("multipart")) 
{ 
    mp = (Multipart)b.getContent(); 
    j = 0; 
    continue; 
} 

당신은 0-j를 설정하고 제로에서 다시 시작됩니다 기대하고, 계속 루프를 부탁드립니다. 그러나 증가 작업 j++이 먼저 나오고 루프가 0이 아닌 1에서 시작합니다.

문제를 해결하려면 j을 -1로 설정하십시오.

if (b.getContentType().contains("multipart")) 
{ 
    mp = (Multipart)b.getContent(); 
    j = -1; 
    continue; 
} 
+1

@Psycho_Penguin : 도움이 되었습니까? – ToYonos

+0

죄송합니다. 지난 며칠간 휴가 중이 었습니다. 지금까지는 테스트 환경에서는 작동하지만 프로덕션에서는 작동하지 않습니다. –

+0

이상한. 두 환경 모두에서 실패하기 전에? – ToYonos

1

나는 당신의 코드를 테스트 한 결과 실패했다.

내 경우에 b.getContentType()은 모두 대문자를 반환합니다 (예 : "TEXT/HTML; charset = UTF-8"). 그래서 저는 이것을 소문자로 변환했습니다. 이 블록에서

String contentType=b.getContentType().toLowerCase(Locale.ENGLISH); 

if(!contentType.contains("text/html")) { 
    continue; 
} 
+1

콘텐츠 유형을 로그에 인쇄 할 때 항상 소문자로 표시되는 이유는 무엇입니까? –

+1

소문자라면 문제 없습니다. 나는 그것이 당신이 사용하는 이메일 클라이언트 라이브러리와 관련이 있다고 생각한다. 일부 전자 메일 클라이언트는 대문자를 반환합니다 (필자는 sun의 IMAP 구현을 gmail로 테스트했습니다). –