7,656
社区成员
发帖
与我相关
我的任务
分享
// CreateInboxSMS.h
#include <windows.h>
#include <cemapi.h>
#pragma comment(lib,"cemapi.lib")
#include <mapiutil.h>
// CreateInboxSMS.cpp
#include "CreateInboxSMS.h"
HRESULT GetSMSMsgStore( ICEMAPISession * fpSession, IMsgStore * * fppMsgStore );
HRESULT GetSMSInboxFolder( IMsgStore * fpMsgStore, IMAPIFolder * * fppInboxFolder );
HRESULT CreateSMS( IMAPIFolder * fpInboxFolder, IMessage * * fppMsg );
HRESULT SetRecipient( IMessage * fpMsg );
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
HRESULT hr = S_OK;
ICEMAPISession * pSession = NULL;
IMsgStore * pMsgStore = NULL;
IMAPIFolder * pInboxFolder = NULL;
LPMESSAGE pMsg = NULL;
CoInitializeEx(NULL, COINIT_MULTITHREADED);
MAPIInitialize(NULL);
hr = MAPILogonEx(NULL, NULL, NULL, NULL, (LPMAPISESSION *)&pSession);
if( S_OK != hr )
{
hr = E_FAIL;
goto TheEnd;
}
hr = GetSMSMsgStore( pSession, &pMsgStore );
if( S_OK != hr )
{
hr = E_FAIL;
goto TheEnd;
}
hr = GetSMSInboxFolder( pMsgStore, &pInboxFolder );
if( S_OK != hr )
{
hr = E_FAIL;
goto TheEnd;
}
hr = CreateSMS( pInboxFolder, &pMsg );
if( S_OK != hr )
{
hr = E_FAIL;
goto TheEnd;
}
hr = SetRecipient( pMsg );
if( S_OK != hr )
{
hr = E_FAIL;
goto TheEnd;
}
TheEnd:
if(NULL != pSession)
{
pSession->Logoff(0, 0, 0);
pSession->Release();
pSession = NULL;
}
MAPIUninitialize();
CoUninitialize();
return hr == S_OK ? TRUE : FALSE ;
}
HRESULT GetSMSMsgStore( ICEMAPISession * fpSession, IMsgStore * * fppMsgStore )
{
ASSERT( fpSession && fppMsgStore );
HRESULT hr = E_FAIL;
IMAPITable* pMAPITable = NULL;
SRowSet *pRows = NULL;
ULONG ulMessageType = 0;
fpSession->GetMsgStoresTable(NULL,&pMAPITable);
if(NULL == pMAPITable)
{
goto TheEnd;
}
else
{
const ULONG ulNumCols = 2;
SizedSPropTagArray(ulNumCols,Columns)={
ulNumCols ,
PR_ENTRYID,
PR_DISPLAY_NAME
};
if ( FAILED( pMAPITable-> SetColumns((LPSPropTagArray)&Columns, 0) ) )
{
goto TheEnd;
}
BOOL bFindStore = FALSE;
while(SUCCEEDED(pMAPITable-> QueryRows(1, 0, &pRows)) && bFindStore == FALSE )
{
if (NULL == pRows || pRows-> cRows != 1)
{
goto TheEnd;
}
if (_tcsicmp(pRows->aRow[0].lpProps[1].Value.lpszW, TEXT("SMS")) == 0)
{
ULONG ulMesageType;
fpSession->OpenEntry(pRows->aRow[0].lpProps[0].Value.bin.cb,
(LPENTRYID)pRows->aRow[0].lpProps[0].Value.bin.lpb,
NULL,
MAPI_BEST_ACCESS,
&ulMesageType,
(LPUNKNOWN*)fppMsgStore);
bFindStore = TRUE;
}
FreeProws( pRows );
pRows = NULL;
}
delete pMAPITable;
pMAPITable = NULL;
}
if( *fppMsgStore == NULL )
goto TheEnd;
hr = S_OK;
TheEnd:
if(pRows)
{
FreeProws(pRows);
pRows = NULL;
}
if(pMAPITable)
{
delete pMAPITable;
pMAPITable = NULL;
}
return hr;
}
HRESULT GetSMSInboxFolder( IMsgStore * fpMsgStore, IMAPIFolder * * fppInboxFolder )
{
ASSERT( fpMsgStore && fppInboxFolder );
HRESULT hr = S_OK;
LPSPropValue rgprops = NULL;
ULONG ulValues = 0;
ULONG rgTags[] = {1,PR_CE_IPM_INBOX_ENTRYID};
if(FAILED(fpMsgStore-> GetProps((LPSPropTagArray)rgTags,MAPI_UNICODE,&ulValues,&rgprops)))
{
hr = E_FAIL;
goto TheEnd;
}
if(FAILED(fpMsgStore-> OpenEntry(rgprops[0].Value.bin.cb,(LPENTRYID)rgprops[0].Value.bin.lpb,NULL,MAPI_MODIFY,NULL,(LPUNKNOWN*)fppInboxFolder)))
{
hr = E_FAIL;
goto TheEnd;
}
TheEnd:
if(rgprops)
{
MAPIFreeBuffer(rgprops);
}
return hr;
}
HRESULT CreateSMS ( IMAPIFolder * fpInboxFolder, IMessage * * fppMsg )
{
ASSERT( fpInboxFolder && fppMsg );
HRESULT hr = S_OK;
SYSTEMTIME st;
ADRLIST adlst = {0};
SPropValue lpPropVal[5];
ZeroMemory(&lpPropVal,sizeof(lpPropVal));
//短信的内容(邮件的内容是放在 PR_BODY 里的,由于短信没有主题,而且可以在短信列表中显示出来,所以内容放在了这个字段)
lpPropVal[0].ulPropTag = PR_SUBJECT;
lpPropVal[0].Value.lpszW = L"This is a SMS Message";
lpPropVal[1].ulPropTag = PR_SENDER_EMAIL_ADDRESS; // 发件人地址
lpPropVal[1].Value.lpszW = L"13800138000";
lpPropVal[2].ulPropTag = PR_MESSAGE_CLASS; // 消息类型,邮件为(LPWSTR)L"IPM.Note"
lpPropVal[2].Value.lpszW = (LPWSTR)L"IPM.SMStext";
lpPropVal[3].ulPropTag = PR_MESSAGE_DELIVERY_TIME;// 发送时间 这里 应该和 PR_LAST_MODIFICATION_TIME 区别一下
GetSystemTime(&st);
SystemTimeToFileTime(&st, &(lpPropVal[3].Value.ft));
lpPropVal[4].ulPropTag = PR_MESSAGE_FLAGS;
lpPropVal[4].Value.ul = 0; //MSGFLAG_FROMME, MSGFLAG_READ, MSGFLAG_UNSENT 如果设置为 MSGFLAG_READ 为已读
if( FAILED( fpInboxFolder->CreateMessage(NULL,0,fppMsg) ) )
{
hr = E_FAIL;
goto TheEnd;
}
if( FAILED( ( *fppMsg )->SetProps(5,lpPropVal,NULL) ) )
{
hr = E_FAIL;
goto TheEnd;
}
TheEnd:
return hr;
}
HRESULT SetRecipient(IMessage * fpMsg)
{
ASSERT( fpMsg );
HRESULT hr = S_OK;
ULONG cRecipients = 1; // Send a message to one recipient.
LPWSTR pszTo = L"13912345678"; // 收件人地址
ULONG cRecipientProperties = 3; // Set three properties for the recipient.
LPADRLIST pRecipientList = NULL;
ULONG cbRecipientList = 0;
int cbRecipientProperties = 0;
cbRecipientList = sizeof(ADRLIST) +
cRecipients * (sizeof(ADRENTRY) +
cRecipientProperties * (sizeof(SPropValue) +
(wcslen(pszTo)+3) * sizeof(WCHAR)));
hr = MAPIAllocateBuffer(cbRecipientList, (LPVOID FAR *)&pRecipientList);
if ( S_OK != hr )
{
goto TheEnd;
}
memset(pRecipientList, 0, cbRecipientList);
pRecipientList->cEntries = cRecipients;
pRecipientList->aEntries[0].cValues = cRecipientProperties;
cbRecipientProperties = cRecipientProperties * sizeof(SPropValue);
hr = MAPIAllocateBuffer(cbRecipientProperties, (LPVOID FAR *)&pRecipientList->aEntries[0].rgPropVals);
if ( S_OK != hr )
{
goto TheEnd;
}
memset(pRecipientList->aEntries[0].rgPropVals, 0, sizeof(SPropValue) * 3);
pRecipientList->aEntries[0].rgPropVals[0].ulPropTag = PR_RECIPIENT_TYPE;
pRecipientList->aEntries[0].rgPropVals[0].Value.ul = MAPI_TO;//收件人类型 为 TO, 与此区别的还有 CC,BCC
pRecipientList->aEntries[0].rgPropVals[1].ulPropTag = PR_ADDRTYPE;//这个只有这一个唯一的值
pRecipientList->aEntries[0].rgPropVals[1].Value.lpszW = L"SMTP";
pRecipientList->aEntries[0].rgPropVals[2].ulPropTag = PR_EMAIL_ADDRESS;//收件人地址(如果邮件是自己收到的,就是自己的地址)
pRecipientList->aEntries[0].rgPropVals[2].Value.lpszW = pszTo;
hr = fpMsg->ModifyRecipients(MODRECIP_ADD, pRecipientList);
if ( S_OK != hr )
{
goto TheEnd;
}
TheEnd:
if(pRecipientList->aEntries[0].rgPropVals)
{
MAPIFreeBuffer(pRecipientList->aEntries[0].rgPropVals);
}
if(pRecipientList)
{
MAPIFreeBuffer(pRecipientList);
}
return hr;
}