כיצד לקרוא קבצים MSG ב- C++
Aspose.Email FOSS for C++ lets you open and parse Outlook MSG files with a single תגית: Function call. mapi_message::from_file() ו mapi_message::from_stream() להעלות את הודעה מלאה – נושא, גוף, שליח, קבלנים ותוספים – לתוך זיכרון לא נפרד מבחוץ או מבחינת ה-Outlook.
שלב 1 – הגדרת הפרויקט
הקלד את המאגר ולהוסיף אותו ל- Your 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() כאשר יש לך A 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 Line-End תרגום מתוך שחיתות של ה- CFB.
שלב 5 - מתכונים עם Iterate Recipients
message.recipients() חוזר א 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() חוזר א std::vector כל תוספת - כל תיקון תצוגה filename, mime_type, data (החומר המזויף של הברזל), content_id,ועם זאת, The 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 • גישה למכירה באמצעות 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() ניתן להעביר או למחוק את קובץ המקור מיד לאחר השיחה.
האם אני יכול לקרוא את קבצי MSG שנוצרו על ידי Outlook ב- macOS או Linux?
כן.הפורמט MSG (OLE Compound File + MAPI properties) הוא נייד בין מערכת הפעלה - כל דבר .msg ניתן לפתוח קובץ שנוצר על ידי כל גרסה של Outlook. עם mapi_message::from_file().
איך אני קורא את הגוף HTML?
שיחת טלפון message.html_body().אם ההודעה נוצרה רק עם גוף טקסט שטוח, זה מחזיר רצועה ריקה. message.body() החזיר את הגוף של טקסט שטוח.
האם המצב הקפדני הוא הונחה?
mapi_message::from_file(path, strict) ו from_stream(stream, strict) קבלת A אופציונלי strict דה בול – הפסד הוא false,מי שמסוגל לסבול את המבנה הקטן. התפרצויות בקבצים MSG בעולם האמיתי.