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"에서 멈추고 메시지 텍스트에는 도달하지 않습니다.
'log.info ("이 콘텐츠 유형은"+ b.getContentType()) "입니까? – ToYonos
"이 콘텐츠 유형은 다중/대체입니다." 작동 할 때 "이 콘텐츠 유형은 text/html, charset = ISO-8859-1" –
때마다 각 루프마다 차례가됩니까? – ToYonos