基于gmime2.6库的邮件解码程序

libgmime 是一个用于处理 MIME 数据的 C 语言库。它提供了对 MIME 消息的解析、生成和操作等功能。
下载:https://download.csdn.net/download/farsight_2098/88660821?spm=1001.2014.3001.5503
获取去网上下载:https://mirror.tuna.tsinghua.edu.cn/sourceware/cygwin/x86_64/release/gmime2.6/?C=S&O=A
编译 ./configure;make;
会报错
est-pkcs7.c:36:10: fatal error: gpg-error.h: 没有那个文件或目录
   36 | #include <gpg-error.h>
      |          ^~~~~~~~~~~~~
不影响,

gmime2.6-2.6.23-2.src/gmime-2.6.23$ find ./ -name "lib*.so"
./gmime/.libs/libgmime-2.6.so

已经生成了


简单演示一个宏GMIME_IS_PART的用法,
宏GMIME_IS_PART用于检查一个对象是否是 GMimePart 类型。GMimePart 是 libgmime 中用于表示 MIME 消息或消息部分的类。
在使用 libgmime 库处理 MIME 消息,可能会遇到需要检查一个对象是否是 GMimePart 的情况。这时,可以使用 GMIME_IS_PART 宏来方便地进行类型检查。

#include <gmime/gmime.h>
void part_bodystructure (GMimeObject *part)
{
	GMimeParam *param;
	if (GMIME_IS_MULTIPART (part)) {
		GMimeMultipart *multipart = (GMimeMultipart *) part;
		GMimeObject *subpart;
		int i, n;
		n = g_mime_multipart_get_count (multipart);
		for (i = 0; i < n; i++) {
			subpart = g_mime_multipart_get_part (multipart, i);
			part_bodystructure (subpart);
		}
	} else if (GMIME_IS_MESSAGE_PART (part)) {
		GMimeMessage *message;
		const char *str;
		char *nstring;
		message = GMIME_MESSAGE_PART (part)->message;
		nstring = g_mime_message_get_date_as_string (message);
		printf ( "[%d] %s\n", __LINE__,nstring);
		g_free (nstring);
		if ((str = g_mime_object_get_header ((GMimeObject *) message, "Subject")))
			printf ("[%d] %s\n", __LINE__,str);

		if ((str = g_mime_object_get_header ((GMimeObject *) message, "From")))
			printf ("[%d] %s\n", __LINE__,str);

		if ((str = g_mime_object_get_header ((GMimeObject *) message, "Sender")))
			printf ("[%d] %s\n", __LINE__,str);

		if ((str = g_mime_object_get_header ((GMimeObject *) message, "Reply-To")))
			printf ("[%d] %s\n", __LINE__,str);

		if ((str = g_mime_object_get_header ((GMimeObject *) message, "To")))
			printf ("[%d] %s\n", __LINE__,str);

		if ((str = g_mime_object_get_header ((GMimeObject *) message, "Cc")))
			printf ("[%d] %s\n", __LINE__,str);

		if ((str = g_mime_object_get_header ((GMimeObject *) message, "Bcc")))
			printf ("[%d] %s\n", __LINE__,str);

		if ((str = g_mime_object_get_header ((GMimeObject *) message, "In-Reply-To")))
			printf ("[%d] %s\n", __LINE__,str);

		if ((str = g_mime_message_get_message_id (message)))
			printf ("[%d] %s\n", __LINE__,str);
		/* print body */
		part_bodystructure ((GMimeObject *) message->mime_part);
	} else if (GMIME_IS_PART (part)) {
		printf("------[%d]---- %d -------------\n",__LINE__,GMIME_PART (part)->encoding);
	}

}


int main(int argc,char *argv[]) {
	GMimeMessage *message;
	GMimeStream *stream;
	GMimeParser *parser;
	GMimePart *part;
	GMimeDataWrapper *wrapper;
	GMimeStream *data_stream;

	/* 初始化GMime库 */
	g_mime_init(GMIME_ENABLE_RFC2047_WORKAROUNDS);

	/* 读取eml文件 */
	stream = g_mime_stream_file_new (fopen(argv[1], "r"));

	/* 创建邮件解析器 */
	parser = g_mime_parser_new_with_stream(stream);

	/* 解析邮件 */
	message = g_mime_parser_construct_message(parser);

	/* 打印邮件信息 */
	printf("Subject: %s\n", g_mime_message_get_subject(message));
	printf("From: %s\n", g_mime_message_get_sender(message));

	/* 获取邮件的主体部分 */
	part = g_mime_message_get_mime_part(message);

	/* 如果邮件有主体部分,打印主体部分的内容 */
	if (part) {
		part_bodystructure (part);

	}

	/* 释放资源 */
	g_object_unref(message);
	g_object_unref(parser);
	g_object_unref(stream);

	/* 清理GMime库 */
	g_mime_shutdown();

	return 0;
}


运行结果:


gmime2.6-2.6.23-2.src/gmime-2.6.23/examples$ ./a.out 3.eml
Subject: Fw: Re: Links SQL Mod Suggestion
From: Alex Krohn <alex@gossamer-threads.com>
------[51]---- 1 -------------
[20] Thu, 14 Sep 2000 12:21:51 -0700
[23] Re: Links SQL Mod Suggestion
[26] "\(\)" <pacific@home.com>
[35] "Alex Krohn" <alex@gossamer-threads.com>
[47] 006b01c01e81$0ab01420$5c357118@crdva1.bc.wave.home.com
------[51]---- 1 -------------
gmime2.6-2.6.23-2.src/gmime-2.6.23/examples$ 


更多例子目录下
gmime-2.6.23/tests/
gmime-2.6.23/examples/
邮件文件3.eml在gmime-2.6.23/tests/目录下
更多参考
http://www.noobyard.com/article/p-uyavyftq-cz.html