קריאת קבצי MSG
קריאת קבצי MSG
mapi_message נקודת כניסה עבור העלאת ופירוק של קבצי Outlook MSG. קוראים ל from_file() או from_stream() קורא את המכולת המלאה של CFB, מוציא כל MAPI תכונות, ומציגים אותם באמצעות שיטות תוספים טופס – אין התקנת Outlook דרוש .
הורד דרך קובץ
שימוש 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';
}מחזור מהזרם
שימוש mapi_message::from_stream() כאשר נתונים MSG מגיעים מתוך רשת סוקט, ארכיון או כל דבר אחר std::istream:
#include <fstream>
#include <iostream>
#include "aspose/email/foss/msg/mapi_message.hpp"
int main()
{
std::ifstream input("sample.msg", std::ios::binary);
const auto message = aspose::email::foss::msg::mapi_message::from_stream(input);
std::cout << "Subject: " << message.subject() << '\n';
}תמיד פותחים את הקו עם std::ios::binary.טקסט-מוד זרם קו תרגום הסתיימו ב- Windows ופרקו את ה- CFB.
תכונות הודעות גישה
mapi_message הוא מפרט את המאפיינים הנפוצים ביותר של הודעה כגון גרטרים:
#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"));
// Core properties
std::cout << "Subject: " << message.subject() << '\n';
std::cout << "Sender name: " << message.sender_name() << '\n';
std::cout << "Sender email: " << message.sender_email_address() << '\n';
std::cout << "Internet msg-id: " << message.internet_message_id() << '\n';
std::cout << "Message class: " << message.message_class() << '\n';
// Body — check html_body() first; fall back to plain body()
const auto& body = message.html_body().empty()
? message.body()
: message.html_body();
std::cout << "Body length: " << body.size() << " chars\n";
}רכיבים איטראטיים
message.recipients() חוזר א std::vector חפצים מקבלים עם display_name, email_address, ו recipient_type:
#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"));
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';
}
}איטליה תוספים
message.attachments() חוזר א std::vector כל אחד מהם – כל חומר מופיע filename, mime_type, data, content_id, ו is_embedded_message():
#include <filesystem>
#include <fstream>
#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"));
for (const auto& a : message.attachments())
{
std::cout << a.filename
<< " " << a.mime_type
<< " " << a.data.size() << " bytes"
<< " embedded=" << (a.is_embedded_message() ? "yes" : "no")
<< '\n';
if (!a.is_embedded_message())
{
std::ofstream out("extracted_" + a.filename, std::ios::binary);
out.write(reinterpret_cast<const char*>(a.data.data()),
static_cast<std::streamsize>(a.data.size()));
}
}
}גישה לתקשורת מוטבעת
כאשר is_embedded_message() הוא true,הקובץ מכיל קובצי MSG נוספים. גישה דרך attachment.embedded_message:
#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"));
for (const auto& a : message.attachments())
{
if (a.is_embedded_message() && a.embedded_message != nullptr)
{
const auto& inner = *a.embedded_message;
std::cout << "Embedded subject: " << inner.subject() << '\n';
inner.save(std::filesystem::path("embedded.msg"));
}
}
}טיפים ושיטות טובות
- תמיד עובר
std::ios::binaryלstd::ifstreamכאשר אתה מחזיק קבצים MSG. - צפו
html_body()לפניbody()- הודעות HTML בלבד מאוחסנות בתוכן שלהם ב- HTML גוף נכסים וייצואbody()ריק. from_file()להעלות את כל נתוני הקבצים לתוך הזיכרון. עבור קבצי MSG גדולים, לשקול פתיחת המכולת הבסיסית עםmsg_readerלבדוק את הנתונים ללא תשלום מלא פיתוח ביטים של תוספות.- The
strictפרמטרים שלfrom_file(path, strict)וfrom_stream(stream, strict)הפסולת לfalse, סובלנות של הפרעות מבניות קטנות בקבצים MSG בעולם האמיתי. - חפצים מתאימים להציג
recipient_typeלהבדיל בין כניסות To, Cc ו- Bcc.
בעיות נפוצות
| נושא | סיבה | קבע |
|---|---|---|
msg_exception על קובץ נראה נכון | קובץ אינו מחסן OLE CFB אמיתי | בדוק עם cfb_reader::from_file() אם הוא גם שורד, הקובץ אינו MSG בתוקף. |
body() החזרה של רצועה ריקה | הודעות משתמשות רק בגוף HTML | שימוש html_body() במקום זאת |
recipients() ריק | MSG מבוגר ללא לוח קבלנים | גישה display_to דרך get_property_value() עם common_message_property_id::display_to |
| שחיתות על Windows | פתיחת המסלול ללא std::ios::binary | הוסף std::ios::binary דגל ל std::ifstream |
| שימוש בזיכרון גבוה עבור קבצים גדולים | כל הנתונים של הקבצים מחוברים למעלה | שימוש msg_reader גישה ברמה נמוכה ללא חומריזציה מלאה |
FAQ
האם from_file() שמור את הקובץ פתוח?
לא. הקובץ פתוח, מופרז לחלוטין וסגור לפני שיטת ההחזרה.אתה יכול להעביר או למחוק את קובצי המקור לאחר השיחה.
כיצד אני יכול לומר אם קובץ תוסף הוא קבצים או MSG מובנה?
שיחת טלפון attachment.is_embedded_message().חזרה → true עבור קבצי MSG מובנים; false עבור תוספים קבועים של קבצים. עבור הודעות מוטבע, גישה לאובייקט הותקף דרך attachment.embedded_message.
מה ההבדל בין from_file() ושימוש msg_reader?
mapi_message::from_file() הוא API ברמה גבוהה – הוא משקף את המסר המלא ואת מתארת את התכונות כשטחים מסומנים. msg_reader זהו הקורא ברמה נמוכה שניתן להגיש. גישה למבנה המכולת CFB הירוק וזה שימושי כאשר אתה צריך לבדוק את זה. להגדיר הודעות ללא הפרעה של כל המאפיינים MAPI.
האם קבצי Unicode ו- ANSI MSG תומכים בשניהם?
כן כן. mapi_message • ניהול שני Unicode (סוג נכסים) ptyp_string• אנסי (ptyp_string8• תכונות של רצועה. unicode_strings() חזרה true עבור הודעות מאוחסנים עם תכונות Unicode.
סיכום התייחסות API
| שיעור / שיטה | תיאור |
|---|---|
mapi_message::from_file(path) | להורדה של קובץ MSG מ- A std::filesystem::path |
mapi_message::from_file(path, strict) | אפשרות מצוינת עם אופציה קפדנית |
mapi_message::from_stream(stream) | להעלות את MSG מ- A std::istream |
mapi_message::from_stream(stream, strict) | להעלות מהזרם עם אפשרות מצב קפדנית |
mapi_message::subject() | החזרה למסגרת נושא הודעה |
mapi_message::body() | החזרה לגוף טקסט שטוח |
mapi_message::html_body() | החזרה לגוף HTML (חופשי אם לא נוכח) |
mapi_message::sender_name() | החזיר את שם המשלוח |
mapi_message::sender_email_address() | החזיר את כתובת הדואר האלקטרוני של השולח |
mapi_message::internet_message_id() | החזרה של כותרת הודעות RFC 2822 |
mapi_message::message_class() | החזרה לכיתה MAPI Message string |
mapi_message::recipients() | החזרה לרשימת המועמדים |
mapi_message::attachments() | החזיר את רשימת התוספים |
mapi_attachment::is_embedded_message() | נכון אם הקובץ הוא MSG מובנה |
mapi_message::unicode_strings() | נכון אם הודעה משתמשת בתכונות שורת Unicode |