⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chapter_2_2.cpp

📁 日本人开发的邮件解析 参考价值还是有的 很不错的资源
💻 CPP
字号:
#include    "emailkit.h"

void    AnalyzeEntity( IMimeEntity* itfME );

int     main( int argc, char* argv[] )
{
    MimeMessage*    pMM;   // MimeMessage僆僽僕僃僋僩
    AddressList*    pFRM;  // AddressList僐儗僋僔儑儞僆僽僕僃僋僩
    AddressList*    pTO;   // AddressList僐儗僋僔儑儞僆僽僕僃僋僩
    MimeText*       pSJT;  // MimeText僆僽僕僃僋僩
    IAddress*       pIA;   // IAddress僀儞僞僼僃乕僗
    MailAddress*    pMA;   // MailAddress僆僽僕僃僋僩
    AddressGroup*   pAG;   // AddressGroup僐儗僋僔儑儞僆僽僕僃僋僩
    TextEncoder*    pTE;   // TextEncoder僆僽僕僃僋僩

    CUTF16Text   utVal;
    int          num   = 0;
    int          num2  = 0;
    int          ii;
    int          jj;
    bool         ret;


    // 僆僽僕僃僋僩偺惗惉
    pMM = new MimeMessage;
    pTE = new TextEncoder;

    // 儊僢僙乕僕偺峔憿偺夝愅
    pTE->CharsetDecode( argv[1], strlen( argv[1] ), "", true, utVal );
    pMM->DecodeFile( utVal );

    pMM->ExtractUUParts( &ret );

    // -- From:偺庢摼 -----------------
    // From: 偺奿擺愭傪扵偡
    pMM->get_From( &pFRM );

    // 梫慺悢傪庢摼
    pFRM->get_Count( &num );

    printf( "-- From: -------------\n" );

    for( ii = 0; ii < num; ii++ )
    {
        // 梫慺傪庢傝弌偡
        pFRM->Item( ii, &pIA );

        // 嶲徠愭偺敾掕
        pIA->IsMailAddress( &ret );

        if( ret == true )
        { // MailAddress僆僽僕僃僋僩

            pMA = ( MailAddress* )pIA;

            // From丗偺庢摼
            pMA->get_Value( utVal );

            wprintf( L"%s\n", utVal);
        }
        else
        { // AddressGroup僆僽僕僃僋僩

            pAG = ( AddressGroup* )pIA;

            // 梫慺悢傪庢摼
            pAG->get_Count( &num2 );

            for( jj = 0; jj < num2; jj++ )
            {
                // 梫慺傪庢傝弌偡
                pAG->Item( jj, &pMA );

                // From丗偺庢摼
                pMA->get_Value( utVal );

                wprintf( L"%s\n", utVal);
            }
        }
    }

    // -- To:偺庢摼 -----------------
    // To: 偺奿擺愭傪扵偡
    pMM->get_To( &pTO );

    // 梫慺悢傪庢摼
    pTO->get_Count( &num );

    printf( "\n-- To: ---------------\n" );

    for( ii = 0; ii < num; ii++ )
    {
        // 梫慺傪庢傝弌偡
        pTO->Item( ii, &pIA );

        // 嶲徠愭偺敾掕
        pIA->IsMailAddress( &ret );

        if( ret == true )
        { // MailAddress僆僽僕僃僋僩

            pMA = ( MailAddress* )pIA;

            // To丗偺庢摼
            pMA->get_Value( utVal );

            wprintf( L"%s\n", utVal);
        }
        else
        {
            // AddressGroup僆僽僕僃僋僩
            pAG = ( AddressGroup* )pIA;
            pAG->get_Count( &num2 );

            for( jj = 0; jj < num; jj++ )
            {
                // 梫慺傪庢傝弌偡
                pAG->Item( jj, &pMA );

                // To丗偺庢摼
                pMA->get_Value( utVal );

                wprintf( L"%s\n", utVal);
            }
        }
    }

    // -- Subject:偺庢摼 -----------------
    // Subject丗偺奿擺愭傪扵偡
    pMM->get_Subject( &pSJT );

    // Subject丗偺庢摼
    pSJT->get_Value( utVal );

    printf( "\n-- Subject: ----------\n" );
    wprintf( L"%s\n\n", utVal);

    AnalyzeEntity( ( IMimeEntity* )pMM );

    // MimeMessage僆僽僕僃僋僩偺奐曻
    delete pMM;

    Sleep(10*1000);
    return 0;
}


void    AnalyzeEntity( IMimeEntity* itfME )
{

    MimeBodyPart*        pMB;    // MimeBodyPart僆僽僕僃僋僩
    MultipartBody*       pMPB;   // MultipartBody僆僽僕僃僋僩
    ContentDisposition*  pCD;    // ContentDisposition僆僽僕僃僋僩
    ContentType*         pCT;    // ContentType僆僽僕僃僋僩
    FieldParameters*     pFPS;   // FieldParameters僐儗僋僔儑儞僆僽僕僃僋僩
    FieldParameter*      pFP;    // FieldParameter僆僽僕僃僋僩

    CUTF16Text      utVal;
    CMultiByteText  mtVal;
    int             idx;
    int             ii;
    int             num;
    bool            ret;


    // -- 杮暥偲揧晅僼傽僀儖柤偺嶲徠 -----------------
    itfME->IsMultipart( &ret );

    if( ret == true )
    { // 儅儖僠僷乕僩

        itfME->get_MultipartBody( &pMPB );
        // MimeBodyPart僆僽僕僃僋僩偺屄悢傪庢摼
        pMPB->get_Count( &num );

        for( ii = 0; ii < num ; ii++ )
        {
            // 梫慺傪庢摼
            pMPB->Item( ii, &pMB );
            AnalyzeEntity( ( IMimeEntity* )pMB );
        }
    }
    else
    { // 旕儅儖僠僷乕僩

        itfME->IsPlainText( &ret );
        itfME->get_ContentDisposition( &pCD );
        pCD->get_Value( mtVal );

        // text/plain偱丄Content-Disposition偑側偄偐"inline"
        if( ( ret == true ) &&
             ( ( strcmp( mtVal, "" ) == 0 ) || ( strcmp( mtVal, "inline" ) == 0 ) ) )
        {
            // 杮暥偺庢摼
            itfME->GetText( utVal );

            printf( "-- Text: -------------\n" );
            wprintf( L"%s\n\n", utVal);
        }
        else
        {
            itfME->get_ContentType( &pCT );
            pCT->get_Value( mtVal );

            if( strcmp( mtVal, "application/applefile" ) != 0 )
            { // 堎側傞応崌

                // Content-Disposition丗偺filename傪専嶕
                itfME->get_ContentDisposition( &pCD );
                pCD->get_Parameters( &pFPS );
                pFPS->Find( "filename", 0, true, &idx );

                if( idx >= 0 )
                {
                    pFPS->Item( idx, &pFP );
                    // filename庢摼
                    pFP->get_Value( utVal );
                }
                else
                {
                    // Content-Type:偺name=傪専嶕
                    pCT->get_Parameters( &pFPS );
                    pFPS->Find( "name", 0, true, &idx );

                    if( idx >= 0 )
                    {
                        pFPS->Item( idx, &pFP );
                        // name庢摼
                        pFP->get_Value( utVal );
                    }
                }
            }

            printf( "-- Attachment: -------\n" );
            wprintf( L"%s\n\n", utVal);
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -