Outlook MSG 파일에서 첨부 파일 관리
**Aspose.Email for C++**은 Microsoft Outlook MSG 파일 작업 시 첨부 파일에 접근, 저장, 제거 및 포함을 위한 풍부한 API를 제공합니다. 첨부 파일은 다음을 통해 처리됩니다: MapiMessage 클래스이며, 이를 사용하여 Attachments 속성으로, 다음을 노출합니다: MapiAttachmentCollection.
MSG 파일에서 첨부 파일 저장
MSG 파일에서 첨부 파일을 추출하고 저장하려면:
- 다음으로 메시지를 로드합니다: MapiMessage::Load.
- 다음에 대해 반복합니다 MapiAttachmentCollection.
- 각 첨부 파일을 다음을 사용하여 저장합니다: MapiAttachment::Save() 메서드.
// Create an instance of MapiMessage from file
System::SharedPtr<MapiMessage> message = MapiMessage::Load(fileName);
// Iterate through the attachments collection
{
auto attachment_enumerator = (message->get_Attachments())->GetEnumerator();
decltype(attachment_enumerator->get_Current()) attachment;
while (attachment_enumerator->MoveNext() && (attachment = attachment_enumerator->get_Current(), true))
{
// Save the individual attachment
attachment->Save(dataDir + attachment->get_FileName());
}
}
첨부 파일 제거
Aspose.Email for C++은 MSG 파일에서 첨부 파일을 제거하는 두 가지 방법을 제공합니다:
- Call the RemoveAttachments() method
이 메서드는 메시지 파일 경로를 매개변수로 받습니다. 정적(public static) 메서드로 구현되어 있어 객체를 인스턴스화할 필요가 없습니다. 이 정적 도우미 메서드는 메시지 파일에서 모든 첨부 파일을 제거합니다.
다음 코드 스니펫은 이 메서드 사용 방법을 보여줍니다.
- Call the DestoryAttachment() method
전체 MSG 파일을 파싱하지 않고 첨부 파일을 제거하므로 더 빠르게 작동합니다.
MSG 첨부 파일 추가
MSG 파일은 표준 또는 포함된 첨부 파일로 다른 MSG 파일을 포함할 수 있습니다. 오버로드된 Add 메서드가 포함된 MapiAttachmentCollection Outlook 메시지를 포함하기 위해.
다음 코드 샘플은 지정된 발신자, 수신자, 제목 및 본문으로 새 MAPI 메시지를 생성하고, 기존 MSG 파일을 포함된 메시지로 첨부한 뒤, 해당 메시지를 새로운 MSG 파일에 저장하는 방법을 보여줍니다.
System::SharedPtr<MapiMessage> message = System::MakeObject<MapiMessage>(L"from@test.com", L"to@test.com", L"Subj", L"This is a message body");
System::SharedPtr<MapiMessage> attachMsg = MapiMessage::Load(L"Message.msg");
message->get_Attachments()->Add(L"Weekly report.msg", attachMsg);
message->Save(dataDir + L"WithEmbeddedMsg_out.msg");