eml 文件头解析

先简单说明下 eml 的格式:

邮件是由邮件头和邮件体构成,邮件体又可能由文本、超文本和附件构成,他们之间是通过 boundary 隔开,在每个 boundary 区域,可以看做由 header + value 组成。很显然,可以使用递归解析一个eml文件。

eml 模板:

Date: Fri, 25 Oct 2013 12:36:37 +0800
From: "kitty" <kitty@eyou.net>
To: [email protected]
Subject: =?gb2312?B?eXJkeQ==?=
X-mailer: cnpcmail 1.0.0.5 fix3_2013.10.17 [CN]
Mime-Version: 1.0
Content-Type: multipart/mixed;
	boundary="4664686832757102513123637"

This is a multi-part message in MIME format.

--4664686832757102513123637
Content-Type: multipart/alternative;
	boundary="7781842894210/25/131236372380515350"


--7781842894210/25/131236372380515350
Content-Type: multipart/alternative;
	boundary="19629187561393110/25/131236372462616118"


--19629187561393110/25/131236372462616118
Content-Type: text/plain;
	charset="gb2312"
Content-Transfer-Encoding: base64

ZGY=

--19629187561393110/25/131236372462616118
Content-Type: text/html;
	charset="gb2312"
Content-Transfer-Encoding: base64

PCFET0NUWVBFIEhUTUwgUFVCTElDICItLy9XM0MvL0RURCBIVE1MIDQuMCBUcmFuc2l0aW9uYWwv
L0VOIj4NCjxIVE1MPjxIRUFEPjxUSVRMRT48L1RJVExFPg0KPE1FVEEgY29udGVudD0idGV4dC9o
dG1sOyBjaGFyc2V0PWdiMjMxMiIgaHR0cC1lcXVpdj1Db250ZW50LVR5cGU+DQo8TUVUQSBuYW1l
PUdFTkVSQVRPUiBjb250ZW50PSJNU0hUTUwgMTAuMDAuOTIwMC4xNjcyMSI+PC9IRUFEPg0KPEJP
RFk+DQo8RElWPjxGT05UIHNpemU9MiBmYWNlPc6iyO3RxbraPmRmPC9GT05UPjwvRElWPjwvQk9E
WT48L0hUTUw+DQo=

--19629187561393110/25/131236372462616118--

--7781842894210/25/131236372380515350--

--4664686832757102513123637
Content-Type: application/mixed; name="=?gb2312?B?U0hPUlRDVVQuRVhF?="
Content-Transfer-Encoding: base64
Content-Disposition: attachment;  filename="=?gb2312?B?U0hPUlRDVVQuRVhF?="

TVqQAAMAAAAEAAAA//8AALgAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAgAAAAA4fug4AtAnNIbgBTM0hVGhpcyBwcm9ncmFtIGNhbm5vdCBiZSBydW4gaW4gRE9TIG1v



--4664686832757102513123637--

eml 文件解析源码:

#include <string>
#include <fstream>
#include <iostream>

using namespace std;

int EmailHeader(ifstream &ifile,string &header)
{
	//get header
	while (ifile.eof() == false)
	{
		char outbuffer[1024] = {0};
		ifile.getline(outbuffer,1024,'\n');
		//std::cout << outbuffer << std::endl;
		header += outbuffer;
		header += "\n";
		if (strcmp(outbuffer,"\r") == 0)
			break;
	}

	return 0;
}

int ProcessHeader(const string &header)
{
	std::cout << header << std::endl;

	return 0;
}

int ExstractBoundaryFrom(const string &header,string &boundary)
{
	size_t pos = header.find("boundary",0);
	if (pos == string::npos)
		return 1;

	boundary = header.substr(pos);
	pos = boundary.find("=",0);
	boundary = boundary.substr(pos + 1);
	//remove \" symbol
	size_t s1 = boundary.find("\"",0);
	size_t s2 = string::npos;
	if (s1 != string::npos)
		s2 = boundary.find("\"",s1 + 1);

	if (s1 != string::npos && s2 != string::npos)
		boundary = boundary.substr(s1 + 1,s2 - s1 - 1);

	return 0;
}

bool hasBoundary(const string &header)
{
	bool bHas = (header.find("boundary") == string::npos) ? false : true;
	return bHas;
}

/*
note: boundary dosen't include any '\r','\n' and '\r\n'
*/
bool isBoundaryEnd(const string &boundary,const string &contentline)
{
	bool bEnd = false;
	string temp = "--";
	temp += boundary;
	temp += "--"; 
	if (contentline.find(temp) != string::npos)
		bEnd = true;
	return bEnd;
}

/*
note: boundary dosen't include any '\r','\n' and '\r\n'
*/
bool isBoundaryBegin(const string &boundary,const string &contentline)
{
	bool bBegin = false;
	string temp = "--";
	temp += boundary;
	if (contentline.find(temp) != string::npos)
		bBegin = true;
	return bBegin;
}

int parseBoundaryZone(const string &boundary,ifstream &ifile)
{
	if (boundary.empty())
		return 1;

	while(ifile.eof() == false)
	{
		char line[4096] = {0};
		ifile.getline(line,4096,'\n');
		if (isBoundaryBegin(boundary,line))
		{
			string header;
			EmailHeader(ifile,header);

			ProcessHeader(header);

			if (hasBoundary(header))
			{
				string bound;
				if (0 == ExstractBoundaryFrom(header,bound))
					parseBoundaryZone(bound,ifile);
			}
		}
		
		if (isBoundaryEnd(boundary,line))
			break;
	}
	return 0;
}

string parseEmailFrom(ifstream &ifile)
{
	//get header
	string emailheader;
	EmailHeader(ifile,emailheader);
	std::cout << emailheader << std::endl;
	
	if (hasBoundary(emailheader))
	{
		string boundary;
		ExstractBoundaryFrom(emailheader,boundary);
		std::cout << boundary << std::endl;

		std::cout << "begin parse boudary zone ......" << std::endl;

		parseBoundaryZone(boundary,ifile);
	}
	else
	{
		//print body text/plan
	}

	return "";
}

int _tmain(int argc, _TCHAR* argv[])
{
	ifstream infile("C:\\Users\\db liu\\Desktop\\new3.txt",std::ifstream::binary);
	parseEmailFrom(infile);
	infile.close();
	return 0;
}

你可能感兴趣的:(eml)