是否有.NET Framework方法来解析邮件(MIME)? [英] Are there .NET Framework methods to parse an email (MIME)?

查看:86
本文介绍了是否有.NET Framework方法来解析邮件(MIME)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$ p

我不是在寻找一个类或一组函数内置到.NET Framework(3.5+)来解析原始电子邮件(MIME文档)任何花哨或单独的图书馆,它需要内置。我将在一些单元测试中使用它,并且只需要抓取主体(To,From,Subject)以及主体(在这种情况下,它将始终是文本,因此没有MIME树或边界)。过去我写了几个MIME解析器,如果没有任何可用的东西,那么我只是把正则表达式的东西放在一起。这样做是非常棒的:

  MailMessage msg = MailMessage.Parse(text); 

想法?

解决方案

我知道你说没有外部图书馆,但是我有一个在codeplex上发布的图书馆:



https://bitbucket.org/otac0n/mailutilities

  MimeMessage msg = new MimeMessage(/ * string,stream或Byte [] * /); 

已经测试了超过40,000个真实邮件。



我对我的命名空间选择不太满意,但...我太懒了,改变它。





PS:



在内部,我的图书馆使用这些正则表达式作为解析器:

 内部静态字符串FullMessageMatch = 
@\A(?< header>(?:[^ \r\\\
] + \r\ N)*)(小于?header_term> \r\\\
)(小于?体> *)\z;
内部静态字符串HeadersMatch =
@^(?< header_key> [ - A-Za-z0-9] +)(?< seperator>:[\t] *) < header_value>([^ \r\\\
] | \r\\\
[\t] +)*)(?< terminator> \r\\\
);
内部静态字符串HeaderSeperator =
\r\\\
;
内部静态字符串KeyValueSeparator =
@\A:[\t] * \z;


Is there a class or set of functions built into the .NET Framework (3.5+) to parse raw emails (MIME documents)?

I am not looking for anything fancy or a separate library, it needs to be built-in. I'm going to be using this in some unit tests and need only grab the main headers of interest (To, From, Subject) along with the body (which in this case will always be text and therefore no MIME trees or boundaries). I've written several MIME parsers in the past and if there isn't anything readily available, I'll just put together something from regular expressions. It would be great to be able to do something like:

MailMessage msg = MailMessage.Parse(text);

Thoughts?

解决方案

I know you said no external libraries, but I have a library posted on codeplex:

https://bitbucket.org/otac0n/mailutilities

MimeMessage msg = new MimeMessage(/* string, stream, or Byte[] */);

It has been tested with over 40,000 real-world mail messages.

I'm not too happy with my namespace choice, but... I'm too lazy to change it.


PS:

Internally, my library uses these regexes as a parser:

internal static string FullMessageMatch =
    @"\A(?<header>(?:[^\r\n]+\r\n)*)(?<header_term>\r\n)(?<body>.*)\z";
internal static string HeadersMatch =
    @"^(?<header_key>[-A-Za-z0-9]+)(?<seperator>:[ \t]*)(?<header_value>([^\r\n]|\r\n[ \t]+)*)(?<terminator>\r\n)";
internal static string HeaderSeperator =
    "\r\n";
internal static string KeyValueSeparator =
    @"\A:[ \t]*\z";

这篇关于是否有.NET Framework方法来解析邮件(MIME)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆