C++에서 접속을 사용하는 방법

C++에서 접속을 사용하는 방법

Aspose.Email FOSS for C++ provides the mapi_attachment 클래스 첨부 파일 만들기 은 바이트 또는 스트림, 그리고 그 mapi_message 읽기 첨부 파일 목록 기존 MSG 파일에서 메시지를 작성할 때 첨부 파일을 추가하고 검사할 수 있습니다. 파일을 충전할 때, 첨부물 자체가 내장된 것인지 여부를 감지하는 것을 포함하여 MSG 메시지.

단계 1 - 프로젝트 설정

git clone https://github.com/aspose-email-foss/Aspose.Email-FOSS-for-Cpp.git
add_subdirectory(Aspose.Email-FOSS-for-Cpp)
target_link_libraries(your_target PRIVATE AsposeEmailFoss::AsposeEmailFoss)

단계 2 - 바이트에서 파일 첨부를 추가합니다.

사용하기 mapi_message::add_attachment() 메시지를 작성할 때 바이너리 데이터를 첨부합니다. 필레나임을 제공하십시오, 원료 바이트는 A로 std::vector<std::uint8_t>,그리고 MIME 콘텐츠 유형 :

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

int main()
{
    auto message = aspose::email::foss::msg::mapi_message::create(
        "Report", "Please review the attached report.");
    message.set_sender_name("Alice");
    message.set_sender_email_address("alice@example.com");
    message.add_recipient("bob@example.com", "Bob");

    // Attach a text file
    std::vector<std::uint8_t> text_data{'H', 'e', 'l', 'l', 'o'};
    message.add_attachment("notes.txt", text_data, "text/plain");

    // Attach a binary blob
    std::vector<std::uint8_t> bin_data{0x00, 0x01, 0x02, 0x03};
    message.add_attachment("data.bin", bin_data, "application/octet-stream");

    message.save(std::filesystem::path("report.msg"));
}

단계 3 - 디스크에 파일에서 첨부를 추가합니다.

파일을 바이트 벡터로 읽고 그 다음에 전송합니다. add_attachment():

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

int main()
{
    // Read a file into bytes
    std::ifstream file("document.pdf", std::ios::binary);
    std::vector<std::uint8_t> pdf_bytes(
        std::istreambuf_iterator<char>(file),
        std::istreambuf_iterator<char>{});

    auto message = aspose::email::foss::msg::mapi_message::create(
        "Document", "See attached PDF.");
    message.set_sender_name("Alice");
    message.set_sender_email_address("alice@example.com");
    message.add_recipient("bob@example.com", "Bob");
    message.add_attachment("document.pdf", pdf_bytes, "application/pdf");

    message.save(std::filesystem::path("with_pdf.msg"));
}

단계 4 - 기존 MSG 파일에서 연결을 읽으십시오.

message.attachments() 돌아오는 A const 첨부물의 벡터에 대한 참조. 각 전시회 filename, mime_type, data (그러나 그들은 잔디를 가지고 있음) 그리고 content_id:

#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 << "Attachments: " << message.attachments().size() << '\n';
    for (std::size_t i = 0; i < message.attachments().size(); ++i)
    {
        const auto& a = message.attachments()[i];
        std::cout << "[" << (i + 1) << "]"
                  << "  name="         << a.filename
                  << "  mime="         << a.mime_type
                  << "  bytes="        << a.data.size()
                  << "  content_id="   << a.content_id
                  << '\n';
    }
}

단계 5 - 삽입된 메시지를 발견하고 액세스합니다.

일부 MSG 파일에는 다른 MSF 파일이 첨부물 (전기 또는 모임 초대)으로 포함되어 있습니다. 사용하기 is_embedded_message() 이들을 발견하고 둥근 메시지를 통해 접근하십시오. embedded_message:

#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);

    for (const auto& attachment : message.attachments())
    {
        if (attachment.is_embedded_message() && attachment.embedded_message != nullptr)
        {
            const auto& inner = *attachment.embedded_message;
            std::cout << "Embedded subject: " << inner.subject() << '\n';
            std::cout << "Embedded from:    " << inner.sender_email_address() << '\n';

            // Save the embedded message as a standalone MSG
            inner.save(std::filesystem::path("embedded.msg"));
        }
        else
        {
            std::cout << "Regular attachment: " << attachment.filename
                      << " (" << attachment.data.size() << " bytes)\n";
        }
    }
}

단계 6 - 디스크에 연결 데이터를 저장

첨부 바이트를 추출하고 표준 C++ 파일 스트림을 사용하여 파일에 그들을 작성합니다.:

#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"));

    const std::filesystem::path out_dir("attachments");
    std::filesystem::create_directories(out_dir);

    for (const auto& a : message.attachments())
    {
        if (!a.is_embedded_message())
        {
            const auto out_path = out_dir / a.filename;
            std::ofstream out(out_path, std::ios::binary);
            out.write(reinterpret_cast<const char*>(a.data.data()),
                      static_cast<std::streamsize>(a.data.size()));
            std::cout << "Saved: " << out_path << '\n';
        }
    }
}

일반적인 문제와 해결책

attachment.data 넓은 지식에 대한 공허한 헌신이다. MSG 파일의 원본을 확인합니다. 실제로 첨부 파일 데이터를 포함합니다 (OLE 링크가 아닙니다). MSG 파일은 일부에서 수출됩니다. 이메일 클라이언트는 도서관이 추적 할 수없는 외부 참조로 첨부 파일을 저장할 수 있습니다.

is_embedded_message() 돌아오는 true 하지만 embedded_message 이건 nullptr. 그들의 첨부 헤드는 내장된 MSG를 나타내지만 데이터가 분해되지 않았습니다. MSG 출처는 유효하며 흔들리지 않습니다.

추출 된 첨부 파일을 저장하면 경로 오류가 발생하지 않습니다. 일부 필라테스 파일 목표 파일 시스템에 부정적인 문자를 포함합니다 (예 :,., :, ? 에 윈도우 : 건강 attachment.filename 출력 경로를 구축하기 전에.

MSG 파일은 메모리 사용량이 높습니다.mapi_message::from_file() 모든 부담 메모리에 첨부 데이터를 입력합니다.대형 파일의 경우, 기본 CFB 컨테이너를 열어보세요. cfb_reader 모든 첨부물을 완전히 물질화하지 않고 개별 스트림에 액세스 할 수 있습니다.

add_attachment() 컴파일 시간에 찾을 수 없습니다. 당신이 올바른 것을 사용하고 있는지 확인하십시오. 포함 : "aspose/email/foss/msg/mapi_message.hpp".이 방법은 회원의 일원이다. aspose::email::foss::msg::mapi_message.

자주 묻는 질문들

어떤 MIME 유형을 첨부 파일에 사용해야합니까?

파일 형식에 대한 표준 IANA MIME 유형을 사용하십시오 : "text/plain" 를 위해서 .txt, "application/pdf" 를 위해서 .pdf, "application/octet-stream" 일반적인 붕괴로 인해 이중 파일.MIME 유형은 저장된 형식으로 attach_mime_tag 지도의 재산.

동일한 파일을 여러 번 추가할 수 있습니까?

각각의 호출은 add_attachment() 독립적 인 첨부 파일 입력을 추가합니다.Duplicate filenames 허용; 그들은 첨부 목록의 위치에 따라 구별됩니다.

내 인라인 이미지에 대한 콘텐츠 ID를 어떻게 설정합니까?

콘텐츠 ID 라인을 네 번째 논쟁으로 통과하십시오. mapi_attachment::from_stream():

auto att = aspose::email::foss::msg::mapi_attachment::from_bytes(
    "logo.png", png_bytes, "image/png", "<logo-cid@example.com>");

content_id 그것은 그대로 표시된다 attachment.content_id 다시 읽을 때.

이건 attachment.data 복사본 또는 참조?

attachment.data A 는 std::vector<std::uint8_t> 가치에 의해 저장된 내부의 mapi_message 액세스하면 추가 I/O를 발생시키지 않습니다.

저축하기 전에 첨부 파일을 제거할 수 있습니까?

그들의 mapi_message API는 버전에서 직접 제거-결합 방법을 표시하지 않습니다. 0.1.0. Build a new message, copy the desired properties and attachments manually, 그리고 새로운 물체를 구하기 위해서다.

또한 보기

 한국어