解析字符串C
我正在尝试从电子邮件中获取正文,但我不知道如何获取。正文与标题之间用空格分隔。您能给我一些例子吗?
谢谢。
消息如下所示(带有标题和正文):
From username@localhost Fri May 13 12:28:30 2010
Return-Path: <username@localhost>
X-Original-To: recipe@localhost
Delivered-To: recipe@localhost
Received: from cristi?localhost (localhost [127.0.0.1])
by Notebook (Postfix) with SMTP id 50F6F809E0
for <test@localhost>; Fri, 13 May 2010 12:28:30 +0300 (EEST)
Message-Id: <20110513092830.50F6F809E0@Cristi-Notebook>
Date: Fri, 13 May 2010 12:28:30 +0300 (EEST)
From: username@localhost
To: undisclosed-recipients:;
Text Body
.
到目前为止:
while ( buffer_recieved[begin]){
if ( buffer_recieved[begin] == '\r' && buffer_recieved[begin+1] == '\n' ) {
body[end++]=buffer_recieved[begin];
}
begin++;
}
body[end]=0;
I'm trying to get the body text from an email , but i don't know how. The body is separated with a space from header.Could you give me some examples ?
Thanks.
A message looks like this ( with header and body):
From username@localhost Fri May 13 12:28:30 2010
Return-Path: <username@localhost>
X-Original-To: recipe@localhost
Delivered-To: recipe@localhost
Received: from cristi?localhost (localhost [127.0.0.1])
by Notebook (Postfix) with SMTP id 50F6F809E0
for <test@localhost>; Fri, 13 May 2010 12:28:30 +0300 (EEST)
Message-Id: <20110513092830.50F6F809E0@Cristi-Notebook>
Date: Fri, 13 May 2010 12:28:30 +0300 (EEST)
From: username@localhost
To: undisclosed-recipients:;
Text Body
.
So far :
while ( buffer_recieved[begin]){
if ( buffer_recieved[begin] == '\r' && buffer_recieved[begin+1] == '\n' ) {
body[end++]=buffer_recieved[begin];
}
begin++;
}
body[end]=0;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
找到 POP RFC。阅读规格。
我没读过POP,但我读过SMTP。在 SMTP 中,我记得标头以“\r\n \r\n”结尾,正文也以“\r\n \r\n”结尾。也许对于 POP 来说也是如此。
Find the POP RFC. Read the spec.
I've not read POP, but I've read SMTP. In SMTP I think I recall the header ends in "\r\n \r\n" and the body likewise. Maybe it's the same for POP.
你把这件事搞得太复杂了。你的身体开始
if(strnstr(buffer, "\r\n\r\n", sizeof(buffer)) != NULL)
。现在,您只需要对缓冲区在“\r\n\r\n”序列中分割的情况下存在的 4 个异常进行编码。另一种方法是进行行读取,直到读到“\r\n”,然后转到正文的缓冲区。这是更可取的,因为您可以更轻松地存储标头值,并且与较大缓冲区相比,执行行读取的开销可以忽略不计。
You're making this too complicated. Your body starts
if(strnstr(buffer, "\r\n\r\n", sizeof(buffer)) != NULL)
. Now you only need to code the 4 exceptions that exist where the buffer splits within the "\r\n\r\n" sequence.Another approach is to do line reads till you read "\r\n", then go to buffers for the body. This is preferable because you can store header values more easily and the overhead from doing line reads vs larger buffers is negligible.
如果我理解正确的话,正文由换行符分隔,因此我们可以保留一个变量来告诉我们是否已经遇到该换行符。在这种情况下,复制文本,否则检查我们是否已经到达它。
If I understood correctly, the body is separated by a newline, so we can keep a variable which tells us if we already met that newline. In that case copy the text, else check if we've reached it.