기본적으로받은 편지함에서 이메일을 읽는 응용 프로그램을 작성했습니다. 나는 항상 Gmail에서 보낸 전자 메일로 응용 프로그램을 테스트했습니다. 그러나 이제는 Outlook에서 보낸 전자 메일을 읽으려고 할 때 다시 내용을 얻지 못합니다. Gmail이 작동하는 동안 JavaMail을 사용하여 Outlook 메일을 읽을 수 없습니다.
나는 모두 전자 메일에서 contenttypes 기록 : Gmail은 반환multipart/alternative; boundary=047d7b342bf2b6847f04d11df78a
전망 반환 :
text/html; charset=iso-8859-1
참고 :이 그냥 다른 메일 클라이언트에서 보낸 같은 전자 메일입니다.
Gmail의 메일은 Multipart의 인스턴스입니다. Outlook 전자 메일은 String의 인스턴스입니다.
내 코드 :
메시지가 여러 부분 또는 문자열 instanceof를 인 경우 확인 방법. 메시지가없는이 직접 호출 될 경우
public void handleMultipart(Multipart multipart)
throws MessagingException, IOException, Exception {
mpMessage = getText(multipart.getBodyPart(0));
for (int z = 1, n = multipart.getCount(); z < n; z++) {
handlePart(multipart.getBodyPart(z));
}
}
:
public void getContent(Message msg) throws IOException, Exception {
Object contt = msg.getContent();
System.out.println("Contenttype: " + msg.getContentType());
if (contt instanceof Multipart) {
checkDisposition = true;
handleMultipart((Multipart) contt);
} else if (contt instanceof String) {
handlePart((Part) msg);
}
prepareEmail(mpMessage);
}
메시지가이 메소드가 호출됩니다 다중 경우
public void handlePart(Part part)
throws MessagingException, IOException, Exception {
Object con = messageCopy.getContent();
String disposition = part.getDisposition();
String contentType = part.getContentType();
if (checkDisposition) {
if (disposition == null) {
System.out.println("Disposition is null");
} else if (disposition.equalsIgnoreCase(Part.ATTACHMENT)) {
System.out.println("Attachment: " + part.getFileName()
+ " : " + contentType);
input = part.getInputStream();
bytes = IOUtils.toByteArray(input);
} else if (disposition.equalsIgnoreCase(Part.INLINE)) {
System.out.println("Inline: "
+ part.getFileName()
+ " : " + contentType);
} else {
System.out.println("Other: " + disposition);
}
}else{
mpMessage = part.getContent().toString(); //returns nothing
System.out.println("mpMessage handlePart "+mpMessage); //returns nothing
System.out.println("mpMessage handlePart "+part.getLineCount()); //returns 0
System.out.println("mpMessage handlePart "+part.getContentType()); //returns text/html chartset=iso-8859-1
System.out.println("mpMessage handlePart "+part.getSize()); // returns 22334
part.writeTo(System.out); //See below
}
}
각 부분으로부터 텍스트를 돌려주는 메소드 :
,451,515,private String getText(Part p) throws
MessagingException, IOException {
System.out.println("getText contentType "+p.getContentType());
//This part gets called if trying to read an Outlook mail, its not clear for me how to retrieve the text from the part. Since `p.getContent()` returns nothing
if (p.isMimeType("text/*")) {
String s = (String) p.getContent();
System.out.println();
return String.valueOf(s);
}
if (p.isMimeType("multipart/alternative")) {
Multipart mp = (Multipart) p.getContent();
String text = null;
for (int i = 0; i < mp.getCount(); i++) {
Part bp = mp.getBodyPart(i);
if (bp.isMimeType("text/plain")) {
String s = getText(bp);
if (s != null) {
return s;
}
}
}
return text;
}
return null;
}
part.writeTo(System.out)
반환 :
수신 :에서 AMSPRD0710HT005.eurprd07.prod.outlook.com 서버 (TLS) ID 00000; Thu, 2012 년 12 월 20 일 09:28:23 +0000 수신 : AMSPRD0710MB354.eurprd07.prod.outlook.com (00.000.0000) by AMSPRD0710HT005.eurprd07.prod.outlook.com ([00.000.0000]) mapi id와 함께 14.16.0245.002; 2012년 (목) 12월 20일 9시 28분 5초 0000 에서 : 시험 사람 : 지원 제목 : Verwerkingsverslag Kenmerk : 0,824,496 스레드 주제 : Verwerkingsverslag Kenmerk : 0,824,496 스레드 지수 : Ac3elFC2qYsSo + SOT2ii4HnbCCqgVw == 날짜 : 목 , 2012 년 12 월 20 일 10:28:05 +0100 메시지 ID : ...
기타 등등.
메시지 자체의 내용은 일반적인 텍스트가 아닌 HTML 코드로 반환됩니다.
HTML 코드 대신 Outlook 전자 메일에서 일반 텍스트를 검색하려면 어떻게합니까? 또는 handlePart에서 파트의 내용을 검색하려면 어떻게해야합니까?
도움이 되셨다면
감사합니다.
의견을 보내 주셔서 감사합니다. 매우 도움이됩니다. 문제는 특정 단어를 '스캔'해야하는 전자 메일의 내용이 필요하다는 것입니다. 이 방법을 어떻게 제안합니까? 그 라인에 대해서, 나는 Gmail 이메일로 테스트 해 보았지만 정상적으로 작동하지만 Outlook 이메일과 같은 문제를 안고있다. 모든 메일을 Outlook 메일 클라이언트로 전달하도록 Gmail 계정을 설정하는 방법을 고려 중입니다. – Jef
@Jef : 위의 편집을 참조하십시오. 그 점이 모두 불분명한지 알려주세요. – ig0774
고마워요! 대단히 도와 줬어. – Jef