Hi Prasad,
I'm not in front of my Linux machine, so the following code snippet might not work, but this is roughly how you'd do what you want: int extract_pdfs_from_message (const char *filename) { GMimeContentType *content_type; GMimeDataWrapper *content; GMimeMessage *message; GMimeStream *stream; GMimeParser *parser; GMimePartIter *iter; GMimeObject *part; const char *name; int pdfs = 0; int fd; /* open the message file */ if ((fd = open (filename, O_RDONLY)) == -1) return -1; /* create a parser for the message stream */ stream = g_mime_stream_fs_new (fd); parser = g_mime_parser_new_with_stream (stream); g_object_unref (stream); /* parse the message */ message = g_mime_parser_construct_message (parser); g_object_unref (parser); /* iterate over each part to find all application/pdf's */ iter = g_mime_part_iter_new ((GMimeObject *) message); g_object_unref (message); do { part = g_mime_part_iter_get_current (iter); content_type = g_mime_object_get_content_type (part); if (GMIME_IS_PART (part) && g_mime_content_type_is_type (content_type, "application", "pdf")) { /* save the pdf file */ name = g_mime_part_get_filename ((GMimePart *) part); if ((fd = open (name, O_CREAT | O_WRONLY, 0666)) != -1) { content = g_mime_part_get_content_object ((GMimePart *) part); stream = g_mime_stream_fs_new (fd); /* this decodes the raw content stream and writes it to another stream */ if (g_mime_data_wrapper_write_to_stream (stream) != -1) { g_mime_stream_flush (stream); } else { /* error writing to the stream... */ } g_object_unref (stream); } else { /* error opening the output file */ } pdfs++; } else { /* these are not the drones you are looking for... */ } } while (g_mime_part_iter_next (iter)); g_object_unref (iter); return pdfs; } On 1/24/2013 11:05 AM, prasad antony wrote:
|