Inicio Quickstart

Quickstart: tu primer archivo MSG en C++

Este guía crea un ejemplo completo y corriente - componiendo un correo electrónico, guardándolo como un archivo MSG, cargándole de nuevo y convertidéndolo en EML- utilizando sólo la biblioteca estándar C++ y Aspose.Email FOSS para C ++ .


Prediciones

RequisitosDetalles
CompilerC++17 o más tarde (GCC 9+, Clang 10+, MSVC 2019+)
Construye sistemaCMake 3.26 o más tarde
BibliotecaClonado de GitHub (ver Instalación)
DependenciasNinguno

Paso 1: Configura tu CMakeLists.txt

cmake_minimum_required(VERSION 3.26)
project(email_quickstart)

add_subdirectory(Aspose.Email-FOSS-for-Cpp)

add_executable(quickstart main.cpp)
target_link_libraries(quickstart PRIVATE AsposeEmailFoss::AsposeEmailFoss)
set_target_properties(quickstart PROPERTIES CXX_STANDARD 17)

Paso 2: Crear y guardar un archivo MSG

Crea un nuevo archivo MSG con sujeto, cuerpo, enviador, destinatario y anexo:

#include <fstream>
#include <vector>
#include "aspose/email/foss/msg/mapi_message.hpp"

int main()
{
    // Compose the message
    auto message = aspose::email::foss::msg::mapi_message::create(
        "Hello from Aspose.Email FOSS",
        "This message was created programmatically — no Outlook required.");

    message.set_sender_name("Alice");
    message.set_sender_email_address("alice@example.com");
    message.add_recipient("bob@example.com", "Bob");
    message.add_attachment(
        "readme.txt",
        std::vector<std::uint8_t>{'H', 'e', 'l', 'l', 'o', '\n'},
        "text/plain");

    // Save to MSG file
    std::ofstream out("hello.msg", std::ios::binary);
    message.save(out);

    return 0;
}

Construir y correr:

cmake -B build -DCMAKE_BUILD_TYPE=Release
cmake --build build
./build/quickstart

El archivo hello.msg se crea en el directorio de trabajo. abre en Outlook para verificar, O seguir adelante a la etapa 3 para leerlo de nuevo programadamente.


Paso 3: Leer el archivo MSG de vuelta

Cargue el archivo que acaba de crear y imprime sus propiedades:

#include <fstream>
#include <iostream>
#include "aspose/email/foss/msg/mapi_message.hpp"

int main()
{
    std::ifstream input("hello.msg", std::ios::binary);
    const auto message = aspose::email::foss::msg::mapi_message::from_stream(input);

    std::cout << "Subject:     " << message.subject() << '\n';
    std::cout << "From:        " << message.sender_name()
              << " <" << message.sender_email_address() << ">\n";
    std::cout << "Body:        " << message.body() << '\n';
    std::cout << "Recipients:  " << message.recipients().size() << '\n';
    std::cout << "Attachments: " << message.attachments().size() << '\n';

    for (const auto& a : message.attachments())
        std::cout << "  - " << a.filename << " (" << a.data.size() << " bytes)\n";

    return 0;
}

Producción esperada:

Subject:     Hello from Aspose.Email FOSS
From:        Alice <alice@example.com>
Body:        This message was created programmatically  no Outlook required.
Recipients:  1
Attachments: 1
  - readme.txt (6 bytes)

Paso 4: Convertir en EML

Convertir el archivo MSG en formato RFC 5322 / MIME con save_to_eml():

#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("hello.msg"));

    message.save_to_eml(std::filesystem::path("hello.eml"));
    std::cout << "Saved hello.eml\n";

    return 0;
}

El sujeto, el cuerpo, los enviados, recibientes y la anexión se conservan todos en la salida de EML.


Siguiente pasos

  • Instalación - Planificación completa del proyecto CMake y plataformas apoyadas
  • Características — referencia completa con ejemplos de C++
  • Guía de desarrollador — MSG, EML, CFB y MAPI propiedades caminos
  • FAQ - Licencia, soporte de formato, limitaciones conocidas

Ver también

 Español