C++에서 MSG 파일을 읽는 방법
Aspose.Email FOSS for C++ lets you open and parse Outlook MSG files with a single 기능 전화. mapi_message::from_file() 그리고 mapi_message::from_stream() 를 부하라 전체 메시지 - 주제, 몸, 발송자, 수신자 및 첨부 파일 - 기억에 포함된 메모리로 Outlook 또는 COM 의존성이 없는 객체.
단계 1 - 프로젝트 설정
저장소를 클론하고 당신의 CMakeLists.txt:
git clone https://github.com/aspose-email-foss/Aspose.Email-FOSS-for-Cpp.gitadd_subdirectory(Aspose.Email-FOSS-for-Cpp)
target_link_libraries(your_target PRIVATE AsposeEmailFoss::AsposeEmailFoss)단계 2 - 헤더를 포함
#include "aspose/email/foss/msg/mapi_message.hpp"이 단일 헤드셋은 제공합니다. mapi_message 그리고 모든 지원자들에 대한 aspose::email::foss::msg 이름이 니다.
단계 3 - 파일 경로에서 메시지를 업로드합니다.
사용하기 mapi_message::from_file() 당신이 하나를 가지고 있다면 std::filesystem::path:
#include <filesystem>
#include <iostream>
#include "aspose/email/foss/msg/mapi_message.hpp"
int main()
{
const auto message = aspose::email::foss::msg::mapi_message::from_file(
std::filesystem::path("sample.msg"));
std::cout << "Subject: " << message.subject() << '\n';
std::cout << "From: " << message.sender_name()
<< " <" << message.sender_email_address() << ">\n";
std::cout << "Body: " << message.body() << '\n';
}단계 4 - 흐름에서 메시지를 업로드합니다.
사용하기 mapi_message::from_stream() 어떤 것에서 벗어나기 위해서 std::istream - 유용한 것에 대하여 MSG 데이터를 네트워크 또는 메모리 뷔퍼에서 수신하는 방법:
#include <fstream>
#include <iostream>
#include "aspose/email/foss/msg/mapi_message.hpp"
int main()
{
std::ifstream input("sample.msg", std::ios::binary);
auto message = aspose::email::foss::msg::mapi_message::from_stream(input);
std::cout << "Subject: " << message.subject() << '\n';
}항상 흐름을 열어주고 std::ios::binary Windows 라인 엔딩을 방지하기 위해서 CFB 컨테이너를 파괴하는 것에서 번역.
단계 5 - 이테라트 수용체
message.recipients() 돌아오는 A std::vector 각각의 물건, 각자와 함께 display_name, email_address,그리고, 그리고 recipient_type 필드 :
#include <fstream>
#include <iostream>
#include "aspose/email/foss/msg/mapi_message.hpp"
int main()
{
std::ifstream input("sample.msg", std::ios::binary);
auto message = aspose::email::foss::msg::mapi_message::from_stream(input);
for (std::size_t i = 0; i < message.recipients().size(); ++i)
{
const auto& r = message.recipients()[i];
std::cout << "[" << (i + 1) << "] "
<< r.display_name << " <" << r.email_address << ">"
<< " type=" << r.recipient_type << '\n';
}
}단계 6 - 이테라이트 접착제
message.attachments() 돌아오는 A std::vector 각각의 첨부물, 각 납품 전시회 filename, mime_type, data (그러나 그들은 잔인한 바이트를 가지고 있었습니다), content_id,그리고 그들의 is_embedded_message() 예언하는 것 :
#include <fstream>
#include <iostream>
#include "aspose/email/foss/msg/mapi_message.hpp"
int main()
{
std::ifstream input("sample.msg", std::ios::binary);
auto message = aspose::email::foss::msg::mapi_message::from_stream(input);
for (std::size_t i = 0; i < message.attachments().size(); ++i)
{
const auto& a = message.attachments()[i];
std::cout << "[" << (i + 1) << "] "
<< a.filename << " mime=" << a.mime_type
<< " bytes=" << a.data.size()
<< " embedded=" << (a.is_embedded_message() ? "yes" : "no")
<< '\n';
}
}일반적인 문제와 해결책
빈자리 body() 충전 후에. 일부 메시지는 HTML 몸에만 텍스트를 저장합니다. 부동산 - 확인하기 message.html_body() 언제 message.body() 빈 줄을 반환합니다.
흐름이 열리지 않고 std::ios::binary. Windows에서 텍스트 모드 스트림 번역 CRLF 추적, 조용히 바이너리 CFB 데이터를 파괴. 항상 MSG 파일을 열어 std::ios::binary.
msg_exception 유효한 파일을 위해 던져진다. 파일이 진짜 MSG인지 확인하세요. 일부 수출 도구에 의해 저장된 파일은 깔끔한 텍스트 또는 MIME 및 을 빼앗아갈 수 없음 mapi_message::from_file().
recipients() 빈 벡터를 반환합니다. 오래된 MSG 파일은 때때로 수신자를 놓치고 있습니다. 테이블 그러나 스토어는 수신자를 표시합니다. display_to MAPI 소유권.이를 통해 접근할 수 있습니다 message.get_property_value() 와 함께 common_message_property_id::display_to 그리고 property_type_code::ptyp_string.
메모리에 있는 큰 접착 데이터. 는mapi_message::from_file() 그리고 from_stream() 모든 첨부 바이트를 포함하여 전체 메시지를 업로드합니다.대형 파일의 경우, 열기 컨테이너를 포함하여 msg_reader 그리고 완전하지 않고 접착제 스트림을 검사합니다. 그들을 물질화하는 것이다.
자주 묻는 질문들
그 사이의 차이점은 무엇인가 from_file() 그리고 from_stream()?
둘 다 동일한 것을 만들어 낸다. mapi_message 객체를 위한. from_file() A를 받아들인다 std::filesystem::path 파일을 내부로 열어보세요. from_stream() 읽기에서 어떤 것들 std::istream 제공하는 경우 - MSG 데이터가 이미 메모리 또는 소켓 위에 도착합니다.
는 하는 from_file() 파일을 충전 후에 열려 있습니까?
파일이 열리고, 완전히 메모리에 퍼져 있고, 전에 닫습니다. from_file() 귀하가 전화를 받은 후 즉시 원본 파일을 이동하거나 삭제할 수 있습니다.
macOS 또는 Linux에서 Outlook에 의해 만들어진 MSG 파일을 읽을 수 있습니까?
MSG 형식(OLE Compound File + MAPI 속성)은 전송 가능하다. 운영 체제 - 모든 .msg Outlook의 모든 버전에서 생성된 파일이 열릴 수 있습니다. 와 함께 mapi_message::from_file().
어떻게 HTML 몸을 읽을 수 있습니까?
전화기 message.html_body().만약 메시지가 단 하나의 깔끔한 글자로만 만들어졌다면, 이것은 빈 줄을 반환합니다. message.body() 스테크를 다시 한 번 반환합니다.
엄격한 모드는 기본적인 모드입니까?
mapi_message::from_file(path, strict) 그리고 from_stream(stream, strict) 받아들여서 하나 선택적 strict - 불필요한 것은 false,덜 저항하는 구조적 실제 MSG 파일의 변동.