eml 文件头解析

先简单说明下 eml 的格式:

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

eml 模板:

Date: Fri, 25 Oct 2013 12:36:37 +0800
From: "kitty" <kitty@eyou.net>
To: dbliu@eyou.net
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;
}

转载于:https://my.oschina.net/shaxunyeman/blog/172340

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Rust 中解析 .eml(电子邮件)文件通常涉及到读取二进制数据并使用库来解析电子邮件的结构。Rust 有一个名为 `EmailParser` 的库,它可以帮助你处理这个任务。以下是一个简单的步骤指南: 1. **添加依赖库**: 首先,你需要在你的 `Cargo.toml` 文件中添加 `email-lexer` 和 `email-parser` 作为依赖。例如: ```toml [dependencies] email-lexer = "0.16" email-parser = "0.16" ``` 2. **导入和使用库**: 在你的 Rust 代码中,引入这些库并创建一个 `EmailParser` 的实例: ```rust extern crate email; extern crate email_lexical; use std::fs::File; use std::io::{BufReader, BufRead}; use email::parser::Parser; use email_lexical::reader::Reader; ``` 3. **读取和解析文件**: 打开并读取 .eml 文件的内容,然后用 `EmailParser` 解析: ```rust async fn parse_email(file_path: &str) -> Result<email::Message, Box<dyn std::error::Error>> { let file = File::open(file_path)?; let reader = BufReader::new(file); let mut email_reader = Reader::new(reader); Parser::parse_message(email_reader) .await .map(|msg| msg.unwrap()) .map_err(|err| Box::new(err)) // 将错误转换为 `Box<dyn std::error::Error>` } ``` 4. **处理解析结果**: 获取到 `email::Message` 对象后,你可以遍历邮件和正文,提取所需的信息: ```rust async fn main() { let file_path = "path/to/your/email.eml"; let message = parse_email(&file_path).await?; // ... 进行邮件内容的处理,如打印信息或正文 println!("Subject: {}", message.headers().get("Subject").unwrap_or_default()); println!("Body: {}", message.body_text().unwrap_or_default()); } ``` **相关问题--:** 1. 如何处理解析 .eml 文件时可能出现的错误? 2. `email::Message` 结构体提供了哪些常用的方法来访问邮件内容? 3. Rust 中如何处理异步 I/O,以便在解析文件时避免阻塞?

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值