存储邮件信息的结构:struct ContentHeader{
                        int         nType;//=(1)0 text/plain, =(1)1 text/html, =(1)2 maybe attach
                        AnsiString  strTransferEncoding;
                        AnsiString  strContent;
                        AnsiString  strFileName;//for attachment
                    };处理每一行的函数,处理完成存入结构体ContehtHeader,并存入链表CHList( TList* ) void __fastcall TRecvMailThread::DealLine(char* line, int len)
{
        int pos;
    AnsiString strLine( line );
    AnsiString strTemp( line );
    strLine = strLine.LowerCase();
    switch( nItemKind )
    {
    case 0 :
        if( strLine == "" && nItems >= 1 )//第一部分结束
        {
            nItemKind = 1;//进入下一部分
            break;
        }
        if( ( pos = strLine.AnsiPos( "from:" ) ) == 1 )
        {
            if( strFrom == "" )
            {
                strFrom = strTemp.SubString( pos + 5, len - pos - 4 );
                if( strFrom[ 1 ] == 0x20 )
                    strFrom = strFrom.SubString( 2, strFrom.Length() - 1 );
                nItems ++;
            }
        }
        else if( ( pos = strLine.AnsiPos( "to:" ) ) == 1 )
        {
            if( strTo  == "" )
            {
                strTo = strTemp.SubString( pos + 3, len - pos - 2 );
                if( strTo[ 1 ] == 0x20 )
                    strTo = strTo.SubString( 2, strTo.Length() - 1 );
                nItems ++;
            }
        }
        else if( ( pos = strLine.AnsiPos( "subject:" ) ) == 1 )
        {
            if( strSubject == "" )
            {
                strSubject = strTemp.SubString( pos + 8, len - pos - 7 );
                if( strSubject[ 1 ] == 0x20 )
                    strSubject = strSubject.SubString( 2, strSubject.Length() - 1 );
                nItems ++;
            }
        }
        else if( ( pos = strLine.AnsiPos( "content-type: multipart" ) ) != 0 )
        {
            if( ( pos = strLine.AnsiPos( "boundary=" ) ) != 0 )
            {
                strNormalBoundary = strTemp.SubString( pos + 9, len - pos - 8 );
                if( strNormalBoundary[ 1 ] == '\"' )
                    strNormalBoundary = strNormalBoundary.SubString( 2, strNormalBoundary.Length() - 2 );
                bBoundary = true;
            }
        }
        else if( ( pos = strLine.AnsiPos( "boundary=" ) ) != 0 )
        {
            strNormalBoundary = strTemp.SubString( pos + 9, len - pos - 8 );
            if( strNormalBoundary[ 1 ] == '\"' )
                strNormalBoundary = strNormalBoundary.SubString( 2, strNormalBoundary.Length() - 2 );
            bBoundary = true;
        }
        else if( ( pos = strLine.AnsiPos( "content-type: text/plain" ) ) != 0 )
        {
            nTextKind = 0;
            pCH->nType = 0;
        }
        else if( ( pos = strLine.AnsiPos( "content-type: text/html" ) ) != 0 )
        {
            nTextKind = 1;
            pCH->nType = 1;
        }
        else if( ( pos = strLine.AnsiPos( "content-transfer-encoding: " ) ) != 0 )
        {
            strTempEncode = strLine.SubString( pos + 27, len - pos - 26 );
            pCH->strTransferEncoding = strTempEncode;
        }
    break;
    case 1 :
        if( bBoundary )
        {
            if( strTemp.AnsiPos( strNormalBoundary ) != 0 )
            {
                file://找到边界,
                nItemKind = 2;
                nItems = 0;
                bBoundary = false;
            }
        }
        else
        {
            file://没有边界,以下为邮件正文部分,至 "." 结束
            pCH->strContent = strTemp;
            nItemKind = 111;
        }
    break;
    case 111 :
        if( strLine != "." )
        {
            pCH->strContent += strTemp;
            pCH->strContent += "\r\n";
        }
        else
        {
            CHList->Add( pCH );
            nItemKind = ALL_READY;
        }
    break;
    case 2 :
        file://在此查找Content-Type, Content-Transfer-Encoding, boundary, Content-Disposition等
        //.........
        if( strLine == "" && nItems >= 1 )
        {
            file://进入下一部分
            nItemKind = 3;
            nItems = 0;
            break;
        }
        if( ( pos = strLine.AnsiPos( "content-type:" ) ) != 0 )
        {
            nItems ++;
            if( strLine.AnsiPos( "multipart/" ) != 0 )
            {
                if( ( pos = strLine.AnsiPos( "boundary=" ) ) != 0 )
                {
                    strExpandBoundary = strTemp.SubString( pos + 9, len - pos - 8 );
                    if( strExpandBoundary[ 1 ] == '\"' )
                        strExpandBoundary = strExpandBoundary.SubString( 2, strExpandBoundary.Length() - 2 );
                    bBoundary = true;
                }
                nItemKind = 222;
                break;
            }
            if( strLine.AnsiPos( "text/plain" ) != 0 )
                nTextKind = 0;
            else if( strLine.AnsiPos( "text/html" ) != 0 )
                nTextKind = 1;
            else
                nTextKind = 2;
            pCH->nType = nTextKind;
        }
        else if( ( pos = strLine.AnsiPos( "content-transfer-encoding:" ) ) != 0 )
        {
            nItems ++;
            pCH->strTransferEncoding = strLine.SubString( pos + 27, len - pos - 26 );
        }
        else if( strLine.AnsiPos( "content-disposition:" ) != 0 )
        {
            if( ( pos = strLine.AnsiPos( "filename=\"" ) ) != 0 )
            {
                nItems ++;
                pCH->strFileName = strTemp.SubString( pos + 10, len - pos - 10 );
                bAttach = true;
            }
        }
        else if( ( pos = strLine.AnsiPos( "filename=\"" ) ) != 0 )
        {
            nItems ++;
            pCH->strFileName = strTemp.SubString( pos + 10, len - pos - 10 );
            bAttach = true;
        }
    break;
    

解决方案 »

  1.   

    存储邮件信息的结构:struct ContentHeader{
                            int         nType;//=(1)0 text/plain, =(1)1 text/html, =(1)2 maybe attach
                            AnsiString  strTransferEncoding;
                            AnsiString  strContent;
                            AnsiString  strFileName;//for attachment
                        };处理每一行的函数,处理完成存入结构体ContehtHeader,并存入链表CHList( TList* ) void __fastcall TRecvMailThread::DealLine(char* line, int len)
    {
            int pos;
        AnsiString strLine( line );
        AnsiString strTemp( line );
        strLine = strLine.LowerCase();
        switch( nItemKind )
        {
        case 0 :
            if( strLine == "" && nItems >= 1 )//第一部分结束
            {
                nItemKind = 1;//进入下一部分
                break;
            }
            if( ( pos = strLine.AnsiPos( "from:" ) ) == 1 )
            {
                if( strFrom == "" )
                {
                    strFrom = strTemp.SubString( pos + 5, len - pos - 4 );
                    if( strFrom[ 1 ] == 0x20 )
                        strFrom = strFrom.SubString( 2, strFrom.Length() - 1 );
                    nItems ++;
                }
            }
            else if( ( pos = strLine.AnsiPos( "to:" ) ) == 1 )
            {
                if( strTo  == "" )
                {
                    strTo = strTemp.SubString( pos + 3, len - pos - 2 );
                    if( strTo[ 1 ] == 0x20 )
                        strTo = strTo.SubString( 2, strTo.Length() - 1 );
                    nItems ++;
                }
            }
            else if( ( pos = strLine.AnsiPos( "subject:" ) ) == 1 )
            {
                if( strSubject == "" )
                {
                    strSubject = strTemp.SubString( pos + 8, len - pos - 7 );
                    if( strSubject[ 1 ] == 0x20 )
                        strSubject = strSubject.SubString( 2, strSubject.Length() - 1 );
                    nItems ++;
                }
            }
            else if( ( pos = strLine.AnsiPos( "content-type: multipart" ) ) != 0 )
            {
                if( ( pos = strLine.AnsiPos( "boundary=" ) ) != 0 )
                {
                    strNormalBoundary = strTemp.SubString( pos + 9, len - pos - 8 );
                    if( strNormalBoundary[ 1 ] == '\"' )
                        strNormalBoundary = strNormalBoundary.SubString( 2, strNormalBoundary.Length() - 2 );
                    bBoundary = true;
                }
            }
            else if( ( pos = strLine.AnsiPos( "boundary=" ) ) != 0 )
            {
                strNormalBoundary = strTemp.SubString( pos + 9, len - pos - 8 );
                if( strNormalBoundary[ 1 ] == '\"' )
                    strNormalBoundary = strNormalBoundary.SubString( 2, strNormalBoundary.Length() - 2 );
                bBoundary = true;
            }
            else if( ( pos = strLine.AnsiPos( "content-type: text/plain" ) ) != 0 )
            {
                nTextKind = 0;
                pCH->nType = 0;
            }
            else if( ( pos = strLine.AnsiPos( "content-type: text/html" ) ) != 0 )
            {
                nTextKind = 1;
                pCH->nType = 1;
            }
            else if( ( pos = strLine.AnsiPos( "content-transfer-encoding: " ) ) != 0 )
            {
                strTempEncode = strLine.SubString( pos + 27, len - pos - 26 );
                pCH->strTransferEncoding = strTempEncode;
            }
        break;
        case 1 :
            if( bBoundary )
            {
                if( strTemp.AnsiPos( strNormalBoundary ) != 0 )
                {
                    file://找到边界,
                    nItemKind = 2;
                    nItems = 0;
                    bBoundary = false;
                }
            }
            else
            {
                file://没有边界,以下为邮件正文部分,至 "." 结束
                pCH->strContent = strTemp;
                nItemKind = 111;
            }
        break;
        case 111 :
            if( strLine != "." )
            {
                pCH->strContent += strTemp;
                pCH->strContent += "\r\n";
            }
            else
            {
                CHList->Add( pCH );
                nItemKind = ALL_READY;
            }
        break;
        case 2 :
            file://在此查找Content-Type, Content-Transfer-Encoding, boundary, Content-Disposition等
            //.........
            if( strLine == "" && nItems >= 1 )
            {
                file://进入下一部分
                nItemKind = 3;
                nItems = 0;
                break;
            }
            if( ( pos = strLine.AnsiPos( "content-type:" ) ) != 0 )
            {
                nItems ++;
                if( strLine.AnsiPos( "multipart/" ) != 0 )
                {
                    if( ( pos = strLine.AnsiPos( "boundary=" ) ) != 0 )
                    {
                        strExpandBoundary = strTemp.SubString( pos + 9, len - pos - 8 );
                        if( strExpandBoundary[ 1 ] == '\"' )
                            strExpandBoundary = strExpandBoundary.SubString( 2, strExpandBoundary.Length() - 2 );
                        bBoundary = true;
                    }
                    nItemKind = 222;
                    break;
                }
                if( strLine.AnsiPos( "text/plain" ) != 0 )
                    nTextKind = 0;
                else if( strLine.AnsiPos( "text/html" ) != 0 )
                    nTextKind = 1;
                else
                    nTextKind = 2;
                pCH->nType = nTextKind;
            }
            else if( ( pos = strLine.AnsiPos( "content-transfer-encoding:" ) ) != 0 )
            {
                nItems ++;
                pCH->strTransferEncoding = strLine.SubString( pos + 27, len - pos - 26 );
            }
            else if( strLine.AnsiPos( "content-disposition:" ) != 0 )
            {
                if( ( pos = strLine.AnsiPos( "filename=\"" ) ) != 0 )
                {
                    nItems ++;
                    pCH->strFileName = strTemp.SubString( pos + 10, len - pos - 10 );
                    bAttach = true;
                }
            }
            else if( ( pos = strLine.AnsiPos( "filename=\"" ) ) != 0 )
            {
                nItems ++;
                pCH->strFileName = strTemp.SubString( pos + 10, len - pos - 10 );
                bAttach = true;
            }
        break;
        
      

  2.   

    感谢这位大哥的答复BUT你的这个是在.Net中使用的吧!我要的是在Java中使用的。
      

  3.   

    找本书看看>Schildt 的 the art of java
      

  4.   

    http://blog.iyi.cn/user/david/archives/2004/12/169.html
    看看这个可有帮助
      

  5.   

    http://tag.csdn.net/tag/JavaMail/20.html