如何将电子邮件与之前的答案分开?我只想要最新的回复
例如:
但是当我使用短语GetBodyAsText收集这3封邮件时,我会收到每封邮件的整个对话 - 包含1,2和3。
我只想要邮件3内容。怎么办呢?
我尝试了几个方法,例如消息部分,但它也是这样,并检索邮件1,2和3.
您是否有任何链接可以阅读更多相关内容或代码示例? 必须说我使用的是OpenPop.Mine。
这是我的代码:
Models.Helpdesk.Email obj = new Models.Helpdesk.Email();
obj.MessageNumber = i;
obj.MessageId = message.Headers.MessageId;
obj.From = message.Headers.From.Address;
obj.Subject = message.Headers.Subject;
obj.Reference = message.Headers.References;
OpenPop.Mime.MessagePart plaintxt = message.FindFirstPlainTextVersion();
if ((plaintxt != null))
{
obj.Body = plaintxt.GetBodyAsText();
}
else
{
if ((htmltxt != null))
{
obj.Body = htmltxt.GetBodyAsText();
}
else
{
obj.Body = "";
}
}
obj.DateSent = message.Headers.DateSent;
obj.Recvd = message.Headers.Date;
这是我的电子邮件输出:
Just chillin, you?
Yours sincerely
*Testperson*
Developer
*Companyname *
*emailA@gmail.com
<http://goog_1337018965/> Facebook <http://www.facebook.com>
<http://goog_1337018972/> Twitter <https://twitter.com/#!>
D 28. januar 2015 kl. 09.16 wrote Test Test <emailA@gmail.com>:
> What are you doing Dan
>
> D 28. januar 2015 kl. 09.12 wrote Test Test <emailA@gmail.com>:
>
> Hey Steve
>>
>> D 28. januar 2015 kl. 09.09 wrote Test Test <emailA@gmail.com>:
>>
>> Hey Dan
>>>
>>> Yours sincerely
>>>
>>>
>>>
>>> *Testperson*
>>>
>>> Developer
>>>
>>> *Companyname *
>>>
>>>
>>> <http://goog_1337018965/> Facebook <http://www.facebook.com>
>>> <http://goog_1337018972/> Twitter
>>> <https://twitter.com/#!>
>>>
>>>
>>
>
答案 0 :(得分:0)
执行此操作的一种方法是在电子邮件正文中的响应之间查找公共分隔符。通常,不同的响应将由&#34; From:&#34;,&#34; --- Original Message ---&#34;等组成。
如果是这种情况,并且最近的响应位于顶部,您可以抓住消息的第一部分直到分隔符。例如,这是一种可以做到这一点的方法:
static string GetLastResponse(string message, string delimeter = "From:")
{
int delimeterPosition = message.IndexOf(delimeter);
return (delimeterPosition > -1)
? message.Substring(0, delimeterPosition)
: message;
}
然后你可以这样称呼它:
obj.Body =
@"Thanks, can't wait to see you!
Thanks,
Dan
From: Jack Smith
Sent: Tuesday, January 27, 2015 1:15 PM
To: Dan Smith; Ron Smith; Betty Smith
Cc: Jane Doe; John Doe
Subject: RE: Family Reunion
+ Dan
Dan, if you can go, I can go!
Thanks,
Jack
From: Ron Smith
Sent: Tuesday, January 27, 2015 1:15 PM
To: Jack Smith; Betty Smith
Cc: Jane Doe; John Doe
Subject: RE: Family Reunion
What do you guys think about getting together for drinks on Saturday?
Thanks,
Ron";
string lastResponse = GetLastResponse(obj.Body);
Console.WriteLine(lastResponse);
输出是:
谢谢,不能等着见你!
谢谢,Dan
<强>更新强>
如果你没有一个完全相同的分隔符的完整字符串,你也可以拆分一个共同的模式。例如,如果您知道分隔符始终以某些文本开头,包含一些文本,和/或以某些文本结尾,则可以使用以下方法。请注意,分隔符参数不是必需的,因此如果没有常见的&#39; startsWith&#39;,&#39; endsWith&#39;或者&#39,则可以将null传递给其中的一个或多个。 ;含有&#39;字符串:
private static string GetLastResponse(string message, string delimStartsWith,
string delimContains, string delimEndsWith,
StringComparison comparison = StringComparison.Ordinal)
{
if (string.IsNullOrWhiteSpace(message)) return message;
var lastResponse = new StringBuilder();
var lines = message.Split(new[] {"\r\n", "\n"}, StringSplitOptions.None);
foreach (var line in lines)
{
// Check to see if this line starts with, contains,
// and ends with the specified text
if ((delimStartsWith == null || line.StartsWith(delimStartsWith, comparison)) &&
(delimContains == null || line.IndexOf(delimContains, comparison) >= 0) &&
(delimEndsWith == null ||line.EndsWith(delimEndsWith, comparison)))
{
// If we get here it means all our conditions were met, so this
// line is a delimeter and we should break out of this loop.
break;
}
// Haven't reached a delimeter yet, so add this line to our lastResponse
lastResponse.AppendLine(line);
}
return lastResponse.ToString();
}
在您的示例消息中,看起来分隔符始终以&#34; D&#34;开头,包含单词&#34;写了&#34;,并以字符串&#34; .com&gt;结束:&#34;。如果是这种情况,那么您可以使用上述功能:
string lastResponse = GetLastResponse(conversation, "D ", " wrote ", ".com>:");
Console.WriteLine(lastResponse);
输出是:
只是chillin,你呢?
您诚挚的
Testperson
开发
*公司名称*
* emailA@gmail.com
http://goog_1337018965/ Facebook http://www.facebook.com http://goog_1337018972/ Twitter https://twitter.com/#!
答案 1 :(得分:0)
SigParser的API可以像您要求的英语,德语和西班牙语一样拆分电子邮件。向其提供电子邮件的正文,它将为您提供数组中每个电子邮件分段的正文。
在“ SigParser尝试”页面上,用于预览电子邮件解析的方式,这是先前答案中电子邮件的结果。
这是电子邮件属性的API规范。