static MessageData *convert_message(GMimeMessage *message, guint content_option) { if (!message) return NULL; MessageData *md = new_message_data(); const gchar *message_id = g_mime_message_get_message_id(message); if (message_id) md->message_id = g_strdup(message_id); md->from = get_from_addresses(message); md->reply_to = get_reply_to_addresses(message); md->to = get_to_addresses(message); md->cc = get_cc_addresses(message); md->bcc = get_bcc_addresses(message); const gchar *subject = g_mime_message_get_subject(message); if (subject) md->subject = g_strdup(subject); md->date = g_mime_message_get_date_as_string(message); const gchar *in_reply_to = g_mime_object_get_header(GMIME_OBJECT (message), "In-Reply-To"); if (in_reply_to) { gchar *in_reply_to_str = g_mime_utils_header_decode_text(in_reply_to); md->in_reply_to = g_mime_references_decode(in_reply_to_str); g_free(in_reply_to_str); } const gchar *references = g_mime_object_get_header(GMIME_OBJECT (message), "References"); if (references) { gchar *references_str = g_mime_utils_header_decode_text(references); md->references = g_mime_references_decode(references_str); g_free(references_str); } if (content_option) { PartCollectorData *pc = collect_parts(message, content_option); if (pc->text_part) md->text = get_body(pc->text_part, (content_option != COLLECT_RAW_CONTENT), NULL); if (pc->html_part) md->html = get_body(pc->html_part, (content_option != COLLECT_RAW_CONTENT), pc->inlines); md->attachments = get_attachments(pc); free_part_collector_data(pc); } return md; }
int main() { int res = EXIT_SUCCESS; ews::set_up(); try { const auto env = ews::test::environment(); auto service = ews::service(env.server_uri, env.domain, env.username, env.password); auto search_expression = ews::is_equal_to(ews::item_property_path::has_attachments, true); ews::distinguished_folder_id drafts = ews::standard_folder::drafts; auto ids = service.find_item(drafts, search_expression); if (ids.empty()) { std::cout << "No messages with attachment found!\n"; } else { // Get and save attachments of first message we just found. We // assume these are PNG images auto msg = service.get_message(ids[0]); auto i = 0; auto attachments = msg.get_attachments(); for (const auto& attachment : attachments) { auto target_path = "test" + std::to_string(i++) + ".png"; const auto bytes_written = service.get_attachment(attachment.id()) .write_content_to_file(target_path); std::cout << bytes_written << std::endl; } } } catch (std::exception& exc) { std::cout << exc.what() << std::endl; res = EXIT_FAILURE; } ews::tear_down(); return res; }