目標の設定を始める. C++ のE-Mail FOSS
C++ 的 FOSS 是一个用于读取和写入 Outlook 文件,消息和 CFB 容器的库.它没有第三方依赖性,只需要C ++ 17编译器和CMake .
没有任何其他方法.
步骤1 克隆并添加到您的CMake项目
git clone https://github.com/aspose-email-foss/Aspose.Email-FOSS-for-Cpp.git将库添加为子目录,并将其链接到您的CMakeLists.txt 中:
add_subdirectory(Aspose.Email-FOSS-for-Cpp)
target_link_libraries(your_target PRIVATE AsposeEmailFoss::AsposeEmailFoss)该库仅使用C++标准图书馆,没有第三方依赖.GCC 9+,Clang 10+和MSVC 2019+在README项目中被支持.
步骤2 阅读一个 MSG 文件
包含"mapi_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);
std::cout << "Subject: " << message.subject() << '\n';
std::cout << "From: " << message.sender_email_address() << '\n';
std::cout << "Body: " << message.body() << '\n';
for (const auto& recipient : message.recipients())
std::cout << "To: " << recipient.email_address << '\n';
for (const auto& attachment : message.attachments())
std::cout << "Attachment: " << attachment.filename
<< " (" << attachment.mime_type << ")\n";
}mapi_message::from_stream()解析了 MSG文件的 CFB容器,并通过类型访问方法暴露所有 MAPI属性.不需要安装 Microsoft Outlook.
步骤3 创建一个新的MSG文件
使用 `mapi_message::create()⌒在内存中构建消息,设置发送者和接收者的字段,并使用 ヽsave (()) 』将其保存到磁盘上:
#include <fstream>
#include "aspose/email/foss/msg/mapi_message.hpp"
int main()
{
auto message = aspose::email::foss::msg::mapi_message::create(
"Meeting Notes", "Please find the agenda attached.");
message.set_sender_name("Alice");
message.set_sender_email_address("alice@example.com");
message.add_recipient("bob@example.com", "Bob");
message.add_attachment(
"agenda.txt",
std::vector<std::uint8_t>{'I', 't', 'e', 'm', ' ', '1'},
"text/plain");
std::ofstream output("meeting_notes.msg", std::ios::binary);
message.save(output);
}mapi_message::create()⌒生成内存消息. ヽsave (())接受文件路径,一个▽std::ostream或没有参数 (返回 a゚std::vector< \ PLOCHANE HOLE D3>).
步骤4将EML转换为 MSG (回头)
运载一个EML文件,然后序列化为 MSG 用了 ();) `.回程保存主题,纯文本体,HTML体属性,发送者,接收者和附件:
#include <filesystem>
#include <iostream>
#include "aspose/email/foss/msg/mapi_message.hpp"
int main()
{
// EML → MSG
auto message = aspose::email::foss::msg::mapi_message::load_from_eml(
std::filesystem::path("message.eml"));
std::cout << "Subject: " << message.subject() << '\n';
message.save(std::filesystem::path("converted.msg"));
// MSG → EML round-trip
auto loaded = aspose::email::foss::msg::mapi_message::from_file(
std::filesystem::path("converted.msg"));
loaded.save_to_eml(std::filesystem::path("roundtrip.eml"));
std::cout << "Saved converted.msg and roundtrip.eml\n";
}没有任何其他方法.
常见问题和解决方案
流中没有设置二进制模式.打开一个文件,其中有 () “file.msg”) ■的输入而无:binary`可能会导致Windows线尾翻译损坏CFB容器.通过 (PLACEHOLDER_2)::binaryinfor input and output both streams.
链接错误: AsposeEmailFoss::AsposeEmailFoss没有找到. 请验证在您的 CMakeLists.txt`中,如果附加子目录 (((Aspose.Email-FOSS-for-Cpp) “) 在目标链路库 (?? …) 前出现,并且子名与克隆文件完全相匹配.
C++17未启用. 库需要C++ 17功能. 如果编译器报告结构绑定或std::filesystem的错误,请在您的 CMakeLists.txt 的顶部添加⌒set(CMAKE_CXX_STANDARD 17)゚之前加入▽add_subdirectoryヽ调用 .
读取后空置主题或体.一些MSG文件存储文本在Unicode属性,而其他使用ANSI代码页面属性的. message.subject()和 message.body (()⌒从任何存放处返回字符串;检查如果 message .html_(body))ヽ是空的.
load_from_eml将错误的 EML 抛出.验证EML文件使用了 RFC 5322行结尾 (CRLF) 并具有有效的MIME 頭.图书馆不会默默地跳过错誤的头,
常见问题
库是否需要Microsoft Outlook或任何COM组件?
没有.Aspose.Email FOSS for C++使用自己的CFB和 MAPI解析器,并且无第三方依赖程序. 它在Linux,macOS和Windows上工作,而未安装Outlook.
我能读写哪些格式?
支持读取和写入. TNEF (winmail.dat), IMAP,SMTP 和 POP3 都不支持.
如何将消息保存到字节中,而不是文件?
呼叫 (`) 或 (゚) ,没有参数.两者都返回 (⌒std::vectorstd::uint8_t;可以写到任何缓冲器或流量中.
需要哪些C++标准?
C++17.图书馆使用了std::filesystem,` std::optional⌒和其他C ++17标准库功能.GCC 9+,Clang 10+和MSVC 2019+在README项目中列为支持的.
有没有包管理版本 (vcpkg,Conan)?
版本0.1.0仅通过源发行.克隆存储库并使用上图所示的"add_subdirectory”.包管理程序表可能会在未来发布中添加.
查看更多
- 常见问题 (FAQ) Aspose.Slides C++ 的 FOSS
- 如何读取C++中的MSG文件
- 在C++中如何将EML转换为 MSG?
- 在GitHub上使用C++的FOSS.