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