📄 mail.cpp
字号:
// Mail.cpp: implementation of the CMail class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "Mail.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CMail::CMail()
{
}
CMail::~CMail()
{
}
HRESULT CMail:: MailSend( LPWSTR MailTo , LPWSTR MailCc , LPWSTR MailSubject , LPWSTR MailBody)// , CStringList Attachment )
{
HRESULT hr ;
IMAPISession *pSession = NULL ;
IMAPITable *pTable = NULL ;
SRowSet *psrs = NULL ;
IMsgStore *pStore = NULL ;
ULONG rgTags[] = { 1 , PR_CE_IPM_DRAFTS_ENTRYID } ;
ULONG rgMsgTags[] = { 1 , PR_ENTRYID } ;
ULONG cValues = 0 ;
SPropValue *rgprops = NULL ;
SPropValue *rgpropsMsg = NULL ;
LPMAPIFOLDER pfldrDrafts = NULL ;
// IMessage
pmsg = NULL ;
// LPCTSTR pAttachment ;
// POSITION temppos ;
CString tempstr ;
// First logon to the store.
hr = MAPILogonEx( NULL , NULL , NULL , NULL , &pSession ) ;
EXIT_ON_FAILED( hr ) ;
// Get the message stores table
hr = pSession->GetMsgStoresTable( MAPI_UNICODE , &pTable ) ;
EXIT_ON_FAILED( hr ) ;
while ( 1 )
{
// Get a row
hr = pTable->QueryRows( 1 , 0 , &psrs ) ;
EXIT_ON_FAILED( hr ) ;
// Did we hit the end of the table?
if( psrs->cRows != 1 )
{
break;
}
// Make sure we got the props we need
if( ( psrs->aRow[0].cValues < 1 ) ||
( psrs->aRow[0].lpProps[0].ulPropTag != PR_ENTRYID ) )
{
// MessageBox( NULL, L"Store missing PR_ENTRYID!", L"Doh", MB_OK ) ;
hr = E_FAIL ;
break;
}
// Open this message store
hr = pSession->OpenMsgStore( NULL ,
psrs->aRow[0].lpProps[0].Value.bin.cb ,
(ENTRYID *)psrs->aRow[0].lpProps[0].Value.bin.lpb ,
NULL , 0 , &pStore ) ;
EXIT_ON_FAILED( hr ) ;
// Now get the Drafts folder.
hr = pStore->GetProps( (SPropTagArray *)rgTags , MAPI_UNICODE ,
&cValues , &rgprops ) ;
EXIT_ON_FAILED( hr ) ;
ASSERT( rgprops ) ;
ASSERT( rgprops[0].ulPropTag == PR_CE_IPM_DRAFTS_ENTRYID ) ;
hr = pStore->OpenEntry( rgprops[0].Value.bin.cb ,
(LPENTRYID)rgprops[0].Value.bin.lpb ,
NULL ,
0,//MAPI_MODIFY ,
NULL ,
(IUnknown **)&pfldrDrafts ) ;
EXIT_ON_FAILED( hr ) ;
ASSERT( pfldrDrafts ) ;
// Now create a message...
hr = pfldrDrafts->CreateMessage( NULL , 0 , &pmsg ) ;
EXIT_ON_FAILED( hr ) ;
/* for ( int tempi = 0 ; tempi < Attachment.GetCount() ; tempi++ )
{
temppos = Attachment.FindIndex( tempi ) ;
tempstr = Attachment.GetAt( temppos ) ;
pAttachment = (LPCTSTR)tempstr ;
AddAttachment( pAttachment ) ;
}
ASSERT( pmsg ) ;
*/
SetMessageProps( pmsg , MailTo , MailCc , MailSubject , MailBody ) ;
// Now send the message
hr = pmsg->SubmitMessage( 0 ) ;
EXIT_ON_FAILED(hr) ;
// Clean up
MAPIFreeBuffer( rgprops ) ;
MAPIFreeBuffer( rgpropsMsg ) ;
FreeProws( psrs );
rgprops = NULL;
rgpropsMsg = NULL;
psrs = NULL;
RELEASE_OBJ (pmsg);
RELEASE_OBJ (pfldrDrafts);
RELEASE_OBJ (pStore);
} // while (1)
FuncExit:
MAPIFreeBuffer(rgprops) ;
MAPIFreeBuffer(rgpropsMsg) ;
FreeProws(psrs) ;
RELEASE_OBJ(pmsg);
RELEASE_OBJ(pfldrDrafts);
RELEASE_OBJ(pStore);
RELEASE_OBJ(pTable);
RELEASE_OBJ(pSession);
return hr;
}
LRESULT CMail::SetMessageProps(LPMESSAGE pmsg, LPWSTR MailTo, LPWSTR MailCc, LPWSTR MailSubject, LPWSTR MailBody)
{
SPropValue rgprops[3] = {0} ;
ULONG cProps = 0 ;
HRESULT hr ;
LPWSTR pszSubject = MailSubject ; // L"Subject" ;
LPWSTR pszBody = MailBody ; // L"Body." ;
LPSTREAM pstm = NULL ;
// Set the recipients up.
// hr = CEMAPI_SetMessageRecipients( pmsg ) ;
hr = SetMessageRecipients( pmsg , MailTo , MailCc ) ;
EXIT_ON_FAILED(hr) ;
// Set the flags and a subject if they exist.
rgprops[cProps].ulPropTag = PR_MESSAGE_FLAGS ;
rgprops[cProps].Value.ul = MSGFLAG_FROMME | MSGFLAG_UNSENT ;
++cProps ;
rgprops[cProps].ulPropTag = PR_MSG_STATUS ;
rgprops[cProps].Value.ul = MSGSTATUS_RECTYPE_SMS ;
++cProps ;
if( pszSubject != NULL )
{
rgprops[cProps].ulPropTag = PR_SUBJECT ;
rgprops[cProps].Value.lpszW = pszSubject ;
++cProps ;
}
hr = pmsg->SetProps( cProps , rgprops , NULL ) ;
EXIT_ON_FAILED( hr ) ;
// Stream the body in...
hr = pmsg->OpenProperty( PR_BODY , NULL , 0 , MAPI_MODIFY | MAPI_CREATE ,
(LPUNKNOWN*)&pstm ) ;
EXIT_ON_FAILED(hr) ;
pstm->Write( pszBody , ( wcslen(pszBody) + 1 ) * sizeof (WCHAR) , NULL ) ;
pstm->Release() ;
FuncExit:
return hr ;
}
HRESULT CMail::SetMessageRecipients( LPMESSAGE pmsg , LPWSTR MailTo , LPWSTR MailCc )
{
ULONG cRecipients = 0 ;
ULONG cchTotalRecips = 0 ;
LPWSTR pszTo = MailTo ; // L"user@bogus.host.com" ;
LPWSTR pszCc = MailCc ; // L"" ;
ADRLIST* plst = NULL ;
LPSPropValue pval = NULL ;
BYTE* pb = NULL ;
LPWSTR pszLocalRecips ;
HRESULT hr ;
if( _tcslen(pszTo)>0 )
{
cRecipients += CountRecipsInString( pszTo ) ;
cchTotalRecips += wcslen( pszTo ) + 3 ;
}
if( _tcslen(pszCc)>0 )
{
cRecipients += CountRecipsInString( pszCc ) ;
cchTotalRecips += wcslen(pszCc) + 3 ;
}
//
// Allocate one big block of memory to hold all the strings.
// The block is arranged as follows:
//
// ADRLIST SPropValue's Copy of addresses
// |---------------------|--------------------|-------------------|
//
DWORD cb = sizeof(ADRLIST) + ( ( sizeof(ADRENTRY) + sizeof(SPropValue) * 3) * cRecipients ) ;
pb = new BYTE[cb + ( cchTotalRecips * sizeof(WCHAR) ) ] ;
if( pb == NULL ) return E_OUTOFMEMORY ;
ZeroMemory( pb , cb + ( cchTotalRecips * sizeof(WCHAR) ) ) ;
plst = (ADRLIST*)pb ;
pszLocalRecips = (LPWSTR)( pb + cb ) ;
pb += sizeof(ADRLIST) + ( sizeof(ADRENTRY) * cRecipients ) ;
pval = (SPropValue*)pb ;
AddRecipients( pszTo , MAPI_TO , plst , pszLocalRecips , pval ) ;
AddRecipients( pszCc , MAPI_CC , plst , pszLocalRecips , pval ) ;
hr = pmsg->ModifyRecipients( MODRECIP_ADD , plst ) ;
delete[] (BYTE*)plst ;
return hr;
}
ULONG CMail::CountRecipsInString(LPCWSTR psz)
{
ULONG cRecips = 0 ;
if( psz != NULL )
{
if( *psz != '\0' ) ++cRecips ;
while( *psz != L'\0' )
{
if( *psz == L';' ) ++cRecips ;
++psz ;
} // while( *psz != L'\0' )
} // if( psz != NULL )
return cRecips ;
}
void CMail::AddRecipients(LPWSTR pszSrc, ULONG ulType, ADRLIST *plst, LPWSTR& pszLocalRecips , LPSPropValue& pval)
{
if( pszSrc == NULL ) return ;
LPWSTR psz = pszSrc ;
while( *psz != L'\0' )
{
while( *psz == L' ' && *psz != '\0' ) ++psz ;
if( *psz == L'\0' ) break ;
LPWSTR pszEnd = psz ;
while( *pszEnd != L' ' && *pszEnd != ';' && *pszEnd != '\0' ) ++pszEnd ;
int cch = pszEnd - psz ;
wcsncpy( pszLocalRecips , psz , cch ) ;
*(pszLocalRecips + cch) = L'\0' ;
plst->aEntries[plst->cEntries].cValues = 3 ;
plst->aEntries[plst->cEntries].rgPropVals = pval ;
// Set the type (To, Cc, Bcc)...
pval->ulPropTag = PR_RECIPIENT_TYPE ;
pval->Value.ul = ulType ;
++pval ;
// Set the address type (we only do SMTP)...
pval->ulPropTag = PR_ADDRTYPE ;
pval->Value.lpszW = szSMS ;
++pval ;
// Set the address...
pval->ulPropTag = PR_EMAIL_ADDRESS ;
pval->Value.lpszW = pszLocalRecips ;
++pval ;
++plst->cEntries ;
pszLocalRecips += wcslen(pszLocalRecips) + 1 ;
if( *pszEnd != L'\0' ) ++pszEnd ;
psz = pszEnd ;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -