2014-04-04 1 views
0

나는 javax를 사용하여 일부 메일을 읽었습니다.
그런 다음 메시지의 내용을 저장하려고합니다.전체 소스 코드없이 메일 내용을 얻는 방법은 무엇입니까?

예를 들어, 간단한 내용이 By: Test 인 메일을 읽었습니다.

<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii"> 
<meta name="Generator" content="Microsoft Word 14 (filtered medium)"> 
<style><!-- 
/* Font Definitions */ 
@font-face 
    {font-family:Calibri; 
    panose-1:2 15 5 2 2 2 4 3 2 4;} 
/* Style Definitions */ 
p.MsoNormal, li.MsoNormal, div.MsoNormal 
    {margin:0cm; 
    margin-bottom:.0001pt; 
    font-size:11.0pt; 
    font-family:"Calibri","sans-serif"; 
    mso-fareast-language:EN-US;} 
a:link, span.MsoHyperlink 
    {mso-style-priority:99; 
    color:blue; 
    text-decoration:underline;} 
a:visited, span.MsoHyperlinkFollowed 
    {mso-style-priority:99; 
    color:purple; 
    text-decoration:underline;} 
span.E-MailFormatvorlage17 
    {mso-style-type:personal-compose; 
    font-family:"Arial","sans-serif"; 
    color:windowtext;} 
.MsoChpDefault 
    {mso-style-type:export-only; 
    font-family:"Calibri","sans-serif"; 
    mso-fareast-language:EN-US;} 
@page WordSection1 
    {size:612.0pt 792.0pt; 
    margin:70.85pt 70.85pt 2.0cm 70.85pt;} 
div.WordSection1 
    {page:WordSection1;} 
--></style><!--[if gte mso 9]><xml> 
<o:shapedefaults v:ext="edit" spidmax="1026" /> 
</xml><![endif]--><!--[if gte mso 9]><xml> 
<o:shapelayout v:ext="edit"> 
<o:idmap v:ext="edit" data="1" /> 
</o:shapelayout></xml><![endif]--> 
</head> 
<body lang="DE-CH" link="blue" vlink="purple"> 
<div class="WordSection1"> 
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:&quot;Arial&quot;,&quot;sans-serif&quot;">By: Test<o:p></o:p></span></p> 
</div> 
</body> 
</html> 
: IS, By: Test의 간단한 전자 메일 내용은 메시지의 전체 전망 소스 코드가 표시됩니다

Object body = message.getContent(); 
String content = ((body instanceof String) ? (String) body : "NO STRING CONTENT"); 

그러나 문제는 여기 :
는 지금은 .getContent() 방법으로 내용을 읽을

그래서 전체 메일 소스 코드를받지 않고 메일 내용을 읽을 수 있습니까?

+1

HTML *은 내용입니다. 보낸 사람이 Outlook과 같이 나쁜 시민 일 경우 일반 텍스트 버전을 포함하지 않고 렌더링하거나 적어도 구문 분석해야합니다. – chrylis

답변

1

먼저 String<body> 섹션에서 콘텐츠를 추출합니다. 그 후에는 원하는대로 다르지만 모든 HTML 태그를 제거 할 수는 있지만 형식 (줄 바꿈!) 코드는 사라지고 큰 텍스트 만 얻을 수 있습니다.

+0

그래서 콘텐츠 만 얻을 기회가 없습니까? 전체 HTML 코드를 구문 분석해야합니까? 메일의 형식은 중요하지 않습니다. 메일이 자동으로 생성되어 표준화됩니다. –

+0

물론 '<' and '>'을 포함하는 모든 항목을 모두 삭제하면 모든 HTML 태그를 제거하고 순수한 콘텐츠 만 가져올 수 있습니다. – Smutje

0

나는 간단하고 나은 방법을 기억합니다. 전자 메일의 일반/텍스트 부분 만 가져올 수 있습니다.

String content = getPlainText((Part)message); 

private String getPlainText(Part p) throws MessagingException, IOException { 
    if (p.isMimeType("text/plain")) { 
     return (String) p.getContent(); 
    } else if (p.isMimeType("multipart/*")) { 
     Multipart mp = (Multipart) p.getContent(); 
     for (int i = 0; i < mp.getCount(); i++) { 
      String s = getPlainText(mp.getBodyPart(i)); 
      if (s != null) return s; 
     } 
    } 
    return null; 
}