本来想继续谈谈AdviseSink,但是近来有些朋友问到关于收件人的问题,所以顺便先提一下这块。
发件人和收件人是邮件和消息很常用的几个属性之一,关于发件人的设置和获取是很简单的,只需要处理PR_SENDER_EMAIL_ADDRESS属性即可,下面主要讲述的收件人的设置和获取。
MAPI收件人结构如图(摘自MSDN):
每一个Entry代表了一个收件人信息组,每个信息组又可以有多项信息组成,举个例子,下面的代码代表了一个收件人的信息:
aEntries[0].rgPropVals[0].ulPropTag = PR_RECIPIENT_TYPE; //类型,MAPI_TO代表是设置到TO字段上的,相应的还有MAPI_CC和MAPI_BCC。
aEntries[0].rgPropVals[0].Value.ul = MAPI_TO;
aEntries[0].rgPropVals[1].ulPropTag = PR_ADDRTYPE; //设置地址类型,一般为SMTP
aEntries[0].rgPropVals[1].Value.LPSZ = _T("SMTP");
aEntries[0].rgPropVals[2].ulPropTag = PR_EMAIL_ADDRESS; //收件人地址
aEntries[0].rgPropVals[2].Value.LPSZ = _T("1234567");
设置收件人是通过IMessage:: ModifyRecipients来实现的,以下的代码举例说明了如何设置TO、CC和BCC属性:
INT nRecipientCount = 3; //表示有10个联系人信息
INT nListBufSize = CbNewADRLIST(nRecipientCount); //计算10个联系人需要的存储空间
LPADRLIST pAddressList = NULL;
MAPIAllocateBuffer(nListBufSize, (LPVOID FAR *)&pAddressList)); //分配空间
memset(pAddressList, 0, nBufSize);
pAddressList->cEntries = 3; //表明一共有3个联系人信息
//设置To
INT nCurIndex = 0;
MAPIAllocateBuffer(sizeof(SPropValue) * 3, (LPVOID FAR *)&pAddressList->aEntries[nCurIndex].rgPropVals)); //分配空间 memset(pAddressList->aEntries[nCurIndex].rgPropVals, 0, sizeof(SPropValue) * 3);
pAddressList->aEntries[nCurIndex].rgPropVals[0].ulPropTag = PR_RECIPIENT_TYPE;
pAddressList->aEntries[nCurIndex].rgPropVals[0].Value.ul = MAPI_TO; //表明是写到To
pAddressList->aEntries[nCurIndex].rgPropVals[1].ulPropTag = PR_ADDRTYPE;
pAddressList->aEntries[nCurIndex].rgPropVals[1].Value.LPSZ = _T("SMTP");
pAddressList->aEntries[nCurIndex].rgPropVals[2].ulPropTag = PR_EMAIL_ADDRESS;
pAddressList->aEntries[nCurIndex].rgPropVals[2].Value.LPSZ = _T("1234567");
pAddressList->aEntries[nCurIndex].cValues = 3; //表明改联系人有3个属性要设置
//同上,现在设置CC
nCurIndex = 1;
MAPIAllocateBuffer(sizeof(SPropValue) * 3, (LPVOID FAR *)&pAddressList->aEntries[nCurIndex].rgPropVals)); //分配空间 memset(pAddressList->aEntries[nCurIndex].rgPropVals, 0, sizeof(SPropValue) * 3);
pAddressList->aEntries[nCurIndex].rgPropVals[0].ulPropTag = PR_RECIPIENT_TYPE;
pAddressList->aEntries[nCurIndex].rgPropVals[0].Value.ul = MAPI_CC; //表明是写到CC
pAddressList->aEntries[nCurIndex].rgPropVals[1].ulPropTag = PR_ADDRTYPE;
pAddressList->aEntries[nCurIndex].rgPropVals[1].Value.LPSZ = _T("SMTP");
pAddressList->aEntries[nCurIndex].rgPropVals[2].ulPropTag = PR_EMAIL_ADDRESS;
pAddressList->aEntries[nCurIndex].rgPropVals[2].Value.LPSZ = _T("7654321");
pAddressList->aEntries[nCurIndex].cValues = 3; //表明改联系人有3个属性要设置
//同上,现在设置BCC
nCurIndex = 2;
MAPIAllocateBuffer(sizeof(SPropValue) * 3, (LPVOID FAR *)&pAddressList->aEntries[nCurIndex].rgPropVals)); //分配空间 memset(pAddressList->aEntries[nCurIndex].rgPropVals, 0, sizeof(SPropValue) * 3);
pAddressList->aEntries[nCurIndex].rgPropVals[0].ulPropTag = PR_RECIPIENT_TYPE;
pAddressList->aEntries[nCurIndex].rgPropVals[0].Value.ul = MAPI_BCC; //表明是写到CC
pAddressList->aEntries[nCurIndex].rgPropVals[1].ulPropTag = PR_ADDRTYPE;
pAddressList->aEntries[nCurIndex].rgPropVals[1].Value.LPSZ = _T("SMTP");
pAddressList->aEntries[nCurIndex].rgPropVals[2].ulPropTag = PR_EMAIL_ADDRESS;
pAddressList->aEntries[nCurIndex].rgPropVals[2].Value.LPSZ = _T("88888888");
pAddressList->aEntries[nCurIndex].cValues = 3; //表明改联系人有3个属性要设置
//调用ModifyRecipients添加联系人,完了记的释放申请的内存,pMsg为你想操作的Message的对象实例,关于如何获取可以参考以前的文章。 pMsg->ModifyRecipients(MODRECIP_ADD, pAddressList)
for(INT i = 0; i < nRecipientCount; i++)
MAPIFreeBuffer(pAddressList->aEntries[i].rgPropVals);
MAPIFreeBuffer(pAddressList);
OK,先讲完设置,以后有空将会继续讲解如何获取收件人信息。
无聊客
yzx0023@gmail.com