这是本人在Windows mobile上开发邮件系统时,封装出来的邮件内容解析的类,供大家参考
目前可以解析大部分的邮件,测试邮件基本上是常用的邮件客户端或一些Webmail系统上发出。
如果任何问题请留言告之,谢谢...
以下代码是邮件解析核心代码部分
BOOL CXNParseMailMsg::ParseBody()
{
char
szTmp[
512
]
=
""
;
GetHeaderItem(szTmp, MAIL_MESSAGE_CONTENT_TYPE, strlen(MAIL_MESSAGE_CONTENT_TYPE));
char
*
p1
=
strstr(szTmp, MAIL_MESSAGE_CONTENT_TYPE_BOUNDARY);
if
(p1)
{
int
nPos
=
p1
-
szTmp
+
strlen(MAIL_MESSAGE_CONTENT_TYPE_BOUNDARY)
+
1
;
char
*
p2
=
strstr(szTmp
+
nPos,
"
\
""
);
if
(p2)
{
int
nSize
=
p2
-
p1
-
strlen(MAIL_MESSAGE_CONTENT_TYPE_BOUNDARY)
-
1
;
char
*
pBoundary
=
new
char
[
2
+
nSize
+
1
];
if
(
!
pBoundary)
return
FALSE;
memset(pBoundary,
0x00
,
2
+
nSize
+
1
);
memcpy(pBoundary,
"
--
"
,
2
);
memcpy(pBoundary
+
2
, p1
+
strlen(MAIL_MESSAGE_CONTENT_TYPE_BOUNDARY)
+
1
, nSize);
ParseMsgBody(pBoundary, nSize);
FREE_Memory(pBoundary);
}
}
return
TRUE;
}
BOOL CXNParseMailMsg::ParseMsgBody(
const
char
*
pBoudary,
int
nLength)
{
if
(m_pBody
==
NULL)
return
FALSE;
BOOL bRet
=
TRUE;
char
*
pTmpBoudary
=
new
char
[nLength
+
1
];
if
(
!
pTmpBoudary)
return
FALSE;
while
(strlen(m_pBody)
>
0
)
{
char
*
p
=
strstr(m_pBody, pBoudary)
+
strlen(pBoudary);
m_pBody
=
p;
if
(
0
==
memcmp(m_pBody
+
2
, MAIL_MESSAGE_CONTENT_TYPE, strlen(MAIL_MESSAGE_CONTENT_TYPE)) )
{
memset(pTmpBoudary,
0x00
, nLength
+
1
);
int
nTmp
=
GetBoudary(pTmpBoudary, m_pBody);
if
(nTmp
>
0
)
{
m_pBody
+=
nTmp;
ParseMsgBody(pTmpBoudary, nLength);
}
else
{
//
TO DO: read data
p
=
strstr(m_pBody
+
1
, pBoudary);
if
(
!
p)
{
bRet
=
FALSE;
break
;
}
m_pBody
+=
2
;
//
去掉最前面的 "\r\n"
int
nSize
=
p
-
m_pBody
+
2
;
char
szTmpPlain[
128
]
=
""
;
char
szTmpHtml[
128
]
=
""
;
sprintf(szTmpPlain,
"
%s: %s;
"
, MAIL_MESSAGE_CONTENT_TYPE, MAIL_TEXT_PLAIN);
sprintf(szTmpHtml,
"
%s: %s;
"
, MAIL_MESSAGE_CONTENT_TYPE, MAIL_TEXT_HTML);
if
(
0
==
memcmp(m_pBody, szTmpPlain, strlen(szTmpPlain)))
{
//
text/plain
FREE_Memory(m_pBodyText);
m_pBodyText
=
new
char
[nSize
+
1
];
if
(
!
m_pBodyText)
return
FALSE;
memset(m_pBodyText,
0x00
, nSize
+
1
);
memcpy(m_pBodyText, m_pBody
-
2
, nSize);
TransferPart(m_pBodyText, nSize);
}
else
if
(
0
==
memcmp(m_pBody, szTmpHtml, strlen(szTmpHtml)))
{
//
text/html
FREE_Memory(m_pBodyHtml);
m_pBodyHtml
=
new
char
[nSize
+
1
];
if
(
!
m_pBodyHtml)
return
FALSE;
memset(m_pBodyHtml,
0x00
, nSize
+
1
);
memcpy(m_pBodyHtml, m_pBody
-
2
, nSize);
TransferPart(m_pBodyHtml, nSize);
}
else
{
//
others
ParseFiles(nSize);
m_nFileCount
++
;
}
p
+=
strlen(pBoudary);
if
(
0
==
memcmp(p,
"
--
"
,
2
))
{
m_pBody
=
p
+
4
;
break
;
}
}
}
}
FREE_Memory(pTmpBoudary);
return
TRUE;
}
posted on 2006-05-12 15:03
双鱼座的程序员 阅读(1663)
评论(10) 编辑 收藏 引用