MSG 파일 로드, 보기 및 구문 분석
이 항목에서는 Microsoft Outlook 메시지 파일 (*.msg)을 로드하는 방법을 설명합니다. MapiMessage 클래스는 MSG 파일을 로드하는데 사용되며 다양한 시나리오를 위한 여러 정적 로드 함수를 제공합니다. 다음 코드 스니펫은 파일 또는 스트림에서 MSG 파일을 로드하는 방법을 보여줍니다.
MSG 파일 로드
다음 코드 스니펫은 MSG 파일을 로드하는 방법을 보여줍니다.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// The path to the File directory.
String dataDir = "outlook/";
// Create an instance of MapiMessage from file
MapiMessage msg = MapiMessage.load(dataDir + "message.msg");
// Get subject
System.out.println("Subject:" + msg.getSubject());
// Get from address
System.out.println("From:" + msg.getSenderEmailAddress());
// Get body
System.out.println("Body" + msg.getBody());
// Get recipients information
System.out.println("Recipient: " + msg.getRecipients());
// Get attachments
for (MapiAttachment att : msg.getAttachments())
{
System.out.println("Attachment Name: " + att.getFileName());
System.out.println("Attachment Display Name: " + att.getDisplayName());
}
다음 코드 예제는 MailMessage를 사용하여 MSG 형식의 메시지를 로드하는 방법을 보여줍니다.
MailMessage eml = MailMessage.load("message.msg");
결과 메시지는 임베디드 메시지 첨부 파일을 포함하여 EML 형식으로 변환됩니다. 원본 메시지의 특정 msg 형식 속성을 보존하려면 이 로드 방법을 사용하지 마십시오.
임베디드 메시지 첨부 파일의 원본 형식을 보존하려면 다음을 사용하십시오 MsgLoadOptions.PreserveEmbeddedMessageFormat 속성.
MsgLoadOptions msgLoadOptions = new MsgLoadOptions();
msgLoadOptions.setPreserveEmbeddedMessageFormat(true);
MailMessage msg = MailMessage.load(stream, msgLoadOptions);
스트림에서 로드
다음 코드 스니펫은 스트림에서 파일을 로드하는 방법을 보여줍니다.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// Create an instance of MapiMessage from file
try (FileInputStream stream = new FileInputStream(dataDir + "message.msg"))
{
// Create an instance of MapiMessage from file
MapiMessage msg = MapiMessage.load(stream);
// Get subject
System.out.println("Subject:" + msg.getSubject());
// Get from address
System.out.println("From:" + msg.getSenderEmailAddress());
// Get body
System.out.println("Body" + msg.getBody());
}
첨부 파일 내용을 읽지 않고 MSG 파일 로드
기본적으로, MapiMessage.load 첨부 파일의 모든 내용을 메시지와 함께 메모리로 읽어들입니다. 큰 첨부 파일이 있는 메시지를 열 경우, 헤더나 수신자, 첨부 이름만 필요하더라도 메모리 사용량이 첨부 파일 크기만큼 증가합니다.
지연 로딩을 사용하면 첨부 파일 내용이 소스에 남아 있으며 접근할 때만 읽어들입니다. 다음 속성을 사용해 활성화합니다 MsgLoadOptions 클래스:
setDeferAttachmentContent(boolean)- 언제true, 첨부 파일 내용은 필요에 따라 소스에서 읽어들여집니다. 기본값은false.setDeferralThreshold(int)- 지연 로딩이 적용되는 최소 콘텐츠 크기(바이트)입니다. 작은 첨부 파일은 항상 메시지와 함께 로드됩니다. 기본값은 4096입니다.
지연 로딩은 바이너리 첨부 파일 콘텐츠에 적용됩니다. OLE 개체나 포함된 메시지로 저장된 첨부 파일은 메시지가 로드될 때 추출됩니다.
다음 코드 조각은 첨부 파일을 읽지 않고 메시지를 검사합니다:
MsgLoadOptions options = new MsgLoadOptions();
options.setDeferAttachmentContent(true);
// The file stays open until the message is disposed
try (MapiMessage msg = MapiMessage.load("large.msg", options)) {
System.out.println("Subject: " + msg.getSubject());
System.out.println("Recipients: " + msg.getRecipients().size());
for (MapiAttachment attachment : msg.getAttachments()) {
// Names and sizes are available without reading the content
System.out.println(" " + attachment.getLongFileName() + " : " + attachment.getContentLength() + " bytes");
}
}
다음 코드 조각은 메시지에서 PDF 첨부 파일만 추출합니다. 다른 첨부 파일의 내용은 파일에서 전혀 읽지 않습니다:
MsgLoadOptions options = new MsgLoadOptions();
options.setDeferAttachmentContent(true);
try (MapiMessage msg = MapiMessage.load("large.msg", options)) {
for (MapiAttachment attachment : msg.getAttachments()) {
if (!attachment.getLongFileName().endsWith(".pdf")) {
continue;
}
try (InputStream source = attachment.openRead()) {
Files.copy(source, Paths.get("out", attachment.getLongFileName()), StandardCopyOption.REPLACE_EXISTING);
}
}
}
다음 코드 조각은 호출자가 소유한 스트림에서 메시지를 로드합니다:
MsgLoadOptions options = new MsgLoadOptions();
options.setDeferAttachmentContent(true);
// Keep the stream open while the message is in use
try (InputStream source = Files.newInputStream(Paths.get("large.msg"))) {
MapiMessage msg = MapiMessage.load(source, options);
byte[] data = msg.getAttachments().get(0).getBinaryData();
System.out.println("Read " + data.length + " bytes");
}
첨부 파일 내용을 스트림으로 읽고 메모리에 로드하지 않은 채 큰 첨부 파일을 추가하려면 다음을 참조하십시오 메시지 첨부 파일 작업.
임베디드 EML 형식을 보존하면서 EML을 MSG로 변환
EML 파일을 로드할 수 있습니다 MapiMessage 클래스를 인스턴스화하여 MailMessage 객체를 생성하고 이를 MapiMessage.fromMailMessage 메서드. EML 파일에 임베디드 EML 파일이 포함된 경우, 사용하십시오 MapiConversionOptions.setPreserveEmbeddedMessageFormat 임베디드 EML 파일의 형식을 유지합니다. 아래 코드 스니펫은 EML 파일을 로드하는 방법을 보여줍니다. MapiMessage 임베디드 EML 파일의 형식을 보존하면서.
사용해 보세요!
무료로 이메일 및 메시지 아카이브를 온라인에서 변환하세요 Aspose.Email 변환 앱.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
String dataDir = "email/";
MailMessage eml = MailMessage.load(dataDir + "sample.eml", new EmlLoadOptions());
MapiConversionOptions options = MapiConversionOptions.getUnicodeFormat();
//Preserve Embedded Message Format
options.setPreserveEmbeddedMessageFormat(true);
//Convert EML to MSG with Options
MapiMessage msg = MapiMessage.fromMailMessage(eml, options);