재귀적 메소드를 사용하여 이메일 메시지 본문의 일반 텍스트 및 HTML 콘텐츠 가져오기
일반 텍스트 및 HTML을 비롯한 이메일 메시지 본문의 모든 부분을 가져올 재귀적 메소드를 만듭니다.
- 필요한 클래스와 인터페이스를 가져옵니다.
import net.rim.blackberry.api.mail.MimeBodyPart;
import net.rim.blackberry.api.mail.Multipart;
import net.rim.blackberry.api.mail.SupportedAttachmentPart;
import net.rim.blackberry.api.mail.TextBodyPart;
import net.rim.blackberry.api.mail.UnsupportedAttachmentPart;
- 재귀적 메소드에 필요한 메소드 서명을 만듭니다.
void findEmailBody(Object obj) {...}
- BlackBerry Attachment Service가 메시지 첨부 파일 유형을 지원하는지 여부를 나타내는 지역 변수를 만듭니다.
boolean _hasSupportedAttachment;
boolean _hasUnsupportedAttachment;
- 지역 변수를 초기화합니다.
_hasSupportedAttachment = false;
_hasUnsupportedAttachment = false;
- 메소드 매개 변수가 Multipart 객체인 경우 해당 객체는 여러 개의 BodyPart 객체를 갖습니다. 이메일 메시지 본문 전체를 검색하는 재귀적 메소드를 각 BodyPart 객체에 대해 호출합니다.
if(obj instanceof Multipart)
{
_Multipart mp = (Multipart)obj;
//Extract all of the parts within the Multipart message.
for(int count=0; count < mp.getCount(); ++count)
{
findEmailBody(mp.getBodyPart(count));
}
}
- BodyPart 객체가 TextBodyPart이면 메시지의 일반 텍스트 본문을 가져옵니다.
else if (obj instanceof TextBodyPart)
{
//This message only has a text body.
TextBodyPart tbp = (TextBodyPart) obj;
readEmailBody(tbp);
}
- BodyPart 객체가 MimeBodyPart인지 확인합니다.
else if (obj instanceof MimeBodyPart) {...}
- BodyPart 객체가 MimeBodyPart인 경우 다음 작업을 수행합니다.
- BodyPart 객체를 MimeBodyPart로 캐스팅합니다.
MimeBodyPart mbp = (MimeBodyPart) obj;
- MimeBodyPart 객체에 첨부 파일이 포함되어 있지 않으면 MimeBodyPart 객체를 매개 변수로 사용하여 메시지 본문을 가져옵니다.
if (mbp.getContentType().indexOf(ContentType.TYPE_TEXT_HTML_STRING) != -1)
{
readEmailBody(mbp);
}
- MimeBodyPart 객체에 첨부 파일이 포함되어 있으면 메시지 본문을 가져오는 메소드를 호출합니다.
else if
(mbp.getContentType().equals(ContentType.TYPE_MULTIPART_MIXED_STRING) ||
mbp.getContentType().equals(ContentType.TYPE_MULTIPART_ALTERNATIVE_STRING))
{
findEmailBody(mbp.getContent());
}
- BodyPart가 BlackBerry Attachment Service에서 지원하는 첨부 파일이면 해당 지역 변수를 true로 변경합니다.
else if (obj instanceof SupportedAttachmentPart)
{
_hasSupportedAttachment = true;
}
- BodyPart가 BlackBerry Attachment Service에서 지원하지 않는 첨부 파일이면 해당 지역 변수를 true로 변경합니다.
else if (obj instanceof UnsupportedAttachmentPart)
{
_hasUnsupportedAttachment = true;
}
코드 샘플: 이메일 메시지의 콘텐츠 가져오기
private void findEmailBody(Object obj)
{
//Reset the attachment flags.
_hasSupportedAttachment = false;
_hasUnsupportedAttachment = false;
if(obj instanceof Multipart)
{
Multipart mp = (Multipart)obj;
for(int count=0; count < mp.getCount(); ++count)
{
findEmailBody(mp.getBodyPart(count));
}
}
else if (obj instanceof TextBodyPart)
{
TextBodyPart tbp = (TextBodyPart) obj;
readEmailBody(tbp);
}
else if (obj instanceof MimeBodyPart)
{
MimeBodyPart mbp = (MimeBodyPart)obj;
if (mbp.getContentType().indexOf(ContentType.TYPE_TEXT_HTML_STRING) != -1)
{
readEmailBody(mbp);
}
}
else if (mbp.getContentType().equals(ContentType.TYPE_MULTIPART_MIXED_STRING) ||
mbp.getContentType().equals(ContentType.TYPE_MULTIPART_ALTERNATIVE_STRING))
{
//The message has attachments or we are at the top level of the message.
//Extract all of the parts within the MimeBodyPart message.
findEmailBody(mbp.getContent());
}
else if (obj instanceof SupportedAttachmentPart)
{
_hasSupportedAttachment = true;
}
else if (obj instanceof UnsupportedAttachmentPart)
{
_hasUnsupportedAttachment = true;
}
}
이 정보가 도움이 되었습니까? 의견을 보내 주십시오.