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

📄 smapi.cpp

📁 Wxpython Implemented on Windows CE, Source code
💻 CPP
📖 第 1 页 / 共 2 页
字号:
    }
    
    return bSuccess;
}

bool wxMapiSession::Resolve(const wxString& sName, void* lppRecip1)
{
    lpMapiRecipDesc* lppRecip = (lpMapiRecipDesc*) lppRecip1;
    
    wxASSERT(MapiInstalled()); //MAPI must be installed
    wxASSERT(m_data->m_lpfnMAPIResolveName); //Function pointer must be valid
    wxASSERT(LoggedOn()); //Must be logged on to MAPI
    wxASSERT(m_data->m_hSession); //MAPI session handle must be valid
    
    //Call the MAPIResolveName function
#ifndef UNICODE
    LPSTR lpszAsciiName = (LPSTR) sName.c_str();
#else
    wxCharBuffer cbName(1);
    cbName = sName.mb_str();
    LPSTR lpszAsciiName = cbName.data();
#endif
    ULONG nError = m_data->m_lpfnMAPIResolveName(m_data->m_hSession, 0, lpszAsciiName, 0, 0, lppRecip);
    if (nError != SUCCESS_SUCCESS)
    {
        wxLogDebug(_T("Failed to resolve the name: %s, Error:%ld\n"),
                   sName.c_str(), nError);
        m_data->m_nLastError = nError;
    }
    
    return (nError == SUCCESS_SUCCESS);
}

bool wxMapiSession::Send(wxMailMessage& message)
{
    wxASSERT(MapiInstalled()); //MAPI must be installed
    wxASSERT(m_data->m_lpfnMAPISendMail); //Function pointer must be valid
    wxASSERT(m_data->m_lpfnMAPIFreeBuffer); //Function pointer must be valid
    wxASSERT(LoggedOn()); //Must be logged on to MAPI
    wxASSERT(m_data->m_hSession); //MAPI session handle must be valid
    
    //Initialise the function return value
    bool bSuccess = FALSE;  
    
    //Create the MapiMessage structure to match the message parameter send into us
    MapiMessage mapiMessage;
    ZeroMemory(&mapiMessage, sizeof(mapiMessage));
#ifndef UNICODE
    mapiMessage.lpszSubject = (LPSTR) message.m_subject.c_str();
    mapiMessage.lpszNoteText = (LPSTR) message.m_body.c_str();
#else
    wxCharBuffer cbSubject(1),cbBody(1),cbOriginator(1);
    cbSubject = message.m_subject.mb_str();
    cbBody = message.m_body.mb_str();
    mapiMessage.lpszSubject = cbSubject.data();
    mapiMessage.lpszNoteText = cbBody.data();
#endif
    mapiMessage.nRecipCount = message.m_to.GetCount() + message.m_cc.GetCount() + message.m_bcc.GetCount();
    wxASSERT(mapiMessage.nRecipCount); //Must have at least 1 recipient!
    
    //Allocate the recipients array
    mapiMessage.lpRecips = new MapiRecipDesc[mapiMessage.nRecipCount];

    // If we have a 'From' field, use it
    if (!message.m_from.IsEmpty())
    {
        mapiMessage.lpOriginator = new MapiRecipDesc;
        ZeroMemory(mapiMessage.lpOriginator, sizeof(MapiRecipDesc));

        mapiMessage.lpOriginator->ulRecipClass = MAPI_ORIG;
        // TODO Do we have to call Resolve?
#ifndef UNICODE
        mapiMessage.lpOriginator->lpszName = (LPSTR) message.m_from.c_str();
#else
        cbOriginator = message.m_from.mb_str();
        mapiMessage.lpOriginator->lpszName = cbOriginator.data();
#endif
    }
    
    //Setup the "To" recipients
    int nRecipIndex = 0;
    int nToSize = message.m_to.GetCount();
    int i;
    for (i=0; i<nToSize; i++)
    {
        MapiRecipDesc& recip = mapiMessage.lpRecips[nRecipIndex];
        ZeroMemory(&recip, sizeof(MapiRecipDesc));
        recip.ulRecipClass = MAPI_TO;
        wxString& sName = message.m_to[i];

        //Try to resolve the name
        lpMapiRecipDesc lpTempRecip;  
        if (Resolve(sName, (void*) &lpTempRecip))
        {
            //Resolve worked, put the resolved name back into the sName
            sName = wxString(lpTempRecip->lpszName,*wxConvCurrent);
            
            //Don't forget to free up the memory MAPI allocated for us
            m_data->m_lpfnMAPIFreeBuffer(lpTempRecip);
        }
#ifndef UNICODE
        recip.lpszName = (LPSTR) sName.c_str();
#else
        recip.lpszName = sName.mb_str().release();
#endif
        
        ++nRecipIndex;
    }
    
    //Setup the "CC" recipients
    int nCCSize = message.m_cc.GetCount();
    for (i=0; i<nCCSize; i++)
    {
        MapiRecipDesc& recip = mapiMessage.lpRecips[nRecipIndex];
        ZeroMemory(&recip, sizeof(MapiRecipDesc));
        recip.ulRecipClass = MAPI_CC;
        wxString& sName = message.m_cc[i];

        //Try to resolve the name
        lpMapiRecipDesc lpTempRecip;  
        if (Resolve(sName, (void*) &lpTempRecip))
        {
            //Resolve worked, put the resolved name back into the sName
            sName = wxString(lpTempRecip->lpszName,*wxConvCurrent);
            
            //Don't forget to free up the memory MAPI allocated for us
            m_data->m_lpfnMAPIFreeBuffer(lpTempRecip);
        }
#ifndef UNICODE
        recip.lpszName = (LPSTR) sName.c_str();
#else
        recip.lpszName = sName.mb_str().release();
#endif
        
        ++nRecipIndex;
    }
    
    //Setup the "BCC" recipients
    int nBCCSize = message.m_bcc.GetCount();
    for (i=0; i<nBCCSize; i++)
    {
        MapiRecipDesc& recip = mapiMessage.lpRecips[nRecipIndex];
        ZeroMemory(&recip, sizeof(MapiRecipDesc));
        recip.ulRecipClass = MAPI_BCC;
        wxString& sName = message.m_bcc[i];

        //Try to resolve the name
        lpMapiRecipDesc lpTempRecip;  
        if (Resolve(sName, (void*) &lpTempRecip))
        {
            //Resolve worked, put the resolved name back into the sName
            sName = wxString(lpTempRecip->lpszName,wxConvCurrent);
            
            //Don't forget to free up the memory MAPI allocated for us
            m_data->m_lpfnMAPIFreeBuffer(lpTempRecip);
        }
#ifndef UNICODE
        recip.lpszName = (LPSTR) sName.c_str();
#else
        recip.lpszName = sName.mb_str().release();
#endif
        
        ++nRecipIndex;
    }
    
    //Setup the attachments 
    int nAttachmentSize = message.m_attachments.GetCount();
    int nTitleSize = message.m_attachmentTitles.GetCount();
    if (nTitleSize)
    { 
        wxASSERT(nTitleSize == nAttachmentSize); //If you are going to set the attachment titles then you must set 
        //the attachment title for each attachment
    }
    if (nAttachmentSize)
    {
        mapiMessage.nFileCount = nAttachmentSize;
        mapiMessage.lpFiles = new MapiFileDesc[nAttachmentSize];
        for (i=0; i<nAttachmentSize; i++)
        {
            MapiFileDesc& file = mapiMessage.lpFiles[i];
            ZeroMemory(&file, sizeof(MapiFileDesc));
            file.nPosition = 0xFFFFFFFF;
            wxString& sFilename = message.m_attachments[i];

#ifndef UNICODE
            file.lpszPathName = (LPSTR) sFilename.c_str();
#else
            file.lpszPathName = sFilename.mb_str().release();
#endif
            //file.lpszFileName = file.lpszPathName;
            file.lpszFileName = NULL;

            if (nTitleSize && !message.m_attachmentTitles[i].IsEmpty())
            {
                wxString& sTitle = message.m_attachmentTitles[i];
#ifndef UNICODE
                file.lpszFileName = (LPSTR) sTitle.c_str();
#else
                file.lpszFileName = sTitle.mb_str().release();
#endif
            }
        }
    }
    
    //Do the actual send using MAPISendMail
    ULONG nError = m_data->m_lpfnMAPISendMail(m_data->m_hSession, 0, &mapiMessage, MAPI_DIALOG, 0);
    if (nError == SUCCESS_SUCCESS)
    {
        bSuccess = TRUE;
        m_data->m_nLastError = SUCCESS_SUCCESS;
    }
    else
    {
        wxLogDebug(_T("Failed to send mail message, Error:%ld\n"), nError);
        m_data->m_nLastError = nError;
    }
    
    //Tidy up the Attachements
    if (nAttachmentSize)
    {
#ifdef UNICODE
        for (i = 0;i < nAttachmentSize;i++)
        {
            free(mapiMessage.lpFiles[i].lpszPathName);
            free(mapiMessage.lpFiles[i].lpszFileName);
        }
#endif
        delete [] mapiMessage.lpFiles;
    }
    
    //Free up the Recipients and Originator memory
#ifdef UNICODE
    for (i = 0;i < nRecipIndex;i++)
        free(mapiMessage.lpRecips[i].lpszName);
#endif
    delete [] mapiMessage.lpRecips;

    delete mapiMessage.lpOriginator;
    
    return bSuccess;
}

long wxMapiSession::GetLastError() const
{
    return m_data->m_nLastError;
}

#endif // __WXMSW__

⌨️ 快捷键说明

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