GMime Parser memory access violation

37 Views Asked by At

So basically I'm trying to extract the “From:”, “To:”, “Subject:” and body of an MIME encoded e-mail message and show it in the console window. I'm using the GMime library to do that. I have the email saved in a string receivedMailData and my code looks like this:

GMimeStream* stream = g_mime_stream_mem_new_with_buffer(receivedMailData.c_str(), strlen(receivedMailData.c_str()));

GMimeParser* parser = g_mime_parser_new_with_stream(stream);

GMimeParserOptions* options = g_mime_parser_options_get_default();

std::cout << receivedMailData.c_str() << std::endl;

GMimeMessage* message = g_mime_parser_construct_message(parser, options);


if (message != NULL) {
    
    InternetAddressList* from = g_mime_message_get_from(message);
    InternetAddressList* to = g_mime_message_get_to(message);
    const char* subject = g_mime_message_get_subject(message);
    GMimeObject* body = g_mime_message_get_body(message);

    
    gchar* fromText = internet_address_list_to_string(from, 0, FALSE);
    std::cout << "From: " << fromText << std::endl;
    g_free(fromText);

    gchar* toText = internet_address_list_to_string(to, 0, FALSE);
    std::cout << "To: " << toText << std::endl;
    g_free(toText);

    std::cout << "Subject: " << (subject ? subject : "N/A") << std::endl;

    if (GMIME_IS_TEXT_PART(body)) {
        GMimeTextPart* textPart = GMIME_TEXT_PART(body);
        const char* bodyText = g_mime_text_part_get_text(textPart);
        std::cout << "Body: " << (bodyText ? bodyText : "N/A") << std::endl;
    }
    else {
        std::cout << "Body is not a text part." << std::endl;
    }

    g_object_unref(message);
}
else {
    std::cerr << "Error constructing the message." << std::endl;
}

g_mime_shutdown();

The g_mime_parser_construct_message() function can also take a NULL as the second argument since I don't want to specify any options, but it doesn't help anyway.

The code compiles but when it tries to execute the g_mime_parser_construct_message() function it throws an exception in Visual Studio saying: Instruction at address 0x....(some memory address) called to memory at address 0x....(another memory address). Memory couldn't be read.

0

There are 0 best solutions below