为什么自己写的smartphone发送短信程序,发送中文会是乱码?
代码如下:
ICEMAPISession * mapiSession = NULL;
IMAPITable * msgTable = NULL;
IMsgStore * msgStore = NULL;
LPMAPITABLE pTable = NULL;
SRowSet * pRows = NULL;
HRESULT hr;
if (MAPILogonEx(0,NULL,NULL,0,(LPMAPISESSION *)&mapiSession) != S_OK)
{
MessageBox(_T( "Failed to logon "),_T( "Error "));
}
else
{
SizedSPropTagArray(2, PropTagArr) = {2,{PR_DISPLAY_NAME,PR_ENTRYID}};
hr = mapiSession-> GetMsgStoresTable(MAPI_UNICODE,&msgTable);
hr = msgTable-> SetColumns((LPSPropTagArray)&PropTagArr, 0);
if (!hr)
{
do
{
hr = msgTable-> QueryRows(1,0,&pRows);
LPSPropValue lpProp;
lpProp = &pRows-> aRow[0].lpProps[0];
if(_tcscmp( lpProp-> Value.LPSZ, _T( "SMS ") ) == 0 )
{
break;
}
FreeProws(pRows);
pRows = NULL;
}while (!hr);
}
hr = mapiSession-> OpenMsgStore(0,
pRows-> aRow[0].lpProps[1].Value.bin.cb,
(ENTRYID*)pRows-> aRow[0].lpProps[1].Value.bin.lpb,
NULL, MDB_NO_DIALOG | MAPI_BEST_ACCESS,
&msgStore);
//////////////////////////////////////////////////////////////
IMAPIFolder* pFolder;
LPSPropValue rgprops = NULL;
SizedSPropTagArray(1, rgTags) = { 1, {PR_CE_IPM_DRAFTS_ENTRYID} }; //PR_CE_IPM_DRAFTS_ENTRYID PR_CE_IPM_INBOX_ENTRYID
ULONG ulValues = 0;
int nMsg=0;
msgStore-> GetProps((LPSPropTagArray)&rgTags, MAPI_UNICODE, &ulValues, &rgprops);
hr=msgStore-> OpenEntry(rgprops[0].Value.bin.cb, (LPENTRYID)rgprops[0].Value.bin.lpb, NULL, MAPI_MODIFY, NULL, (LPUNKNOWN*)&pFolder );
LPMESSAGE lpMsg=NULL;
SPropValue Data[3];
TCHAR szNum[12];
char szBody[80];
TCHAR* pszSubjectW = NULL;
hr=pFolder-> CreateMessage(NULL,0,&lpMsg);
swprintf(szNum,_T( "13560185253 "));
sprintf(szBody, "Why not?~ ");
pszSubjectW = ToUTF8(szBody);
Data[0].ulPropTag = PR_SUBJECT;
Data[0].Value.lpszW =pszSubjectW;//
Data[1].ulPropTag = PR_MSG_STATUS;
Data[1].Value.ul = MSGSTATUS_RECTYPE_SMS;//_T( "13286873736 ");
Data[2].ulPropTag = PR_MESSAGE_FLAGS;
Data[2].Value.ul = MSGFLAG_UNSENT|MSGFLAG_FROMME;
hr=lpMsg-> SetProps(sizeof(Data)/sizeof(Data[0]),(LPSPropValue)Data,NULL);
///
ADRLIST adrList;
SPropValue val[3];
ZeroMemory(&val, sizeof(val));
adrList.cEntries = 1;
adrList.aEntries[0].cValues = 3;
adrList.aEntries[0].rgPropVals = val;
val[0].ulPropTag = PR_EMAIL_ADDRESS;
val[0].Value.lpszW = szNum;
val[1].ulPropTag = PR_DISPLAY_NAME;
val[1].Value.lpszW = _T( " ");
val[2].ulPropTag = PR_ADDRTYPE;
val[2].Value.lpszW = _T( "SMS ");
lpMsg-> ModifyRecipients(MODRECIP_ADD,&adrList);
if(lpMsg-> SubmitMessage(0) == S_OK)
{
MessageBox(_T( "发送成功~ "),_T( "提示: "),MB_OK);
}
else
{
MessageBox(_T( "发送失败~ "),_T( "提示: "),MB_OK);
}
lpMsg-> Release();
if(pszSubjectW != NULL)
{
delete [] pszSubjectW;
}
::ReleaseDC(m_hWnd,hdc);
pFolder-> Release();
msgStore-> Release();
发送英文正常, 但发送中文,对方收到的会是????
那位知道是什么原因么~
[解决办法]
发送端真的使用了UNICODE?那接收端能把LOG打出来吗?能的话写到文件里看看。另外,还会不会是显示的问题呢,查查看。我很久前写过,收和发都没这样的问题的。
[解决办法]
去我的blog看看就知道
CEMAPI发送短信乱码的解决办法
http://blog.csdn.net/luandao2000
[解决办法]
对于发中文短信,有一个秘密的诀窍。就是短信的内容要申请一段比较长的字符串。我上次试验过。如TCHAR szSubject[5050];如果等于5050,短信会出错,大于5050就可以看到中文了,小于5050中文字符就编程了????号 了。
[解决办法]
这个问题 所有用CEMAPI发送短信的同志都会碰到,问题的根本在于 保存时sms的内容没有按照unicode类型保存。设置成unicode的类型就搞定了。
代码如下:
IMAPIFolder* pCurFolder = GetMsgFolder(ID_DRAFTS);
SPropValue Data[3];
SPropValue propVals[1];
MAPINAMEID mapiNameId;
ZeroMemory(&mapiNameId, sizeof(mapiNameId));
GUID PS_MAPI = {0x00020328, 0, 0, 0xC0,0,0,0,0,0,0,0x46};
mapiNameId.lpguid = (LPGUID)&PS_MAPI;
mapiNameId.ulKind = MNID_STRING;
mapiNameId.Kind.lpwstrName = L "SMS:Unicode ";
LPMAPINAMEID pmapiNameId = &mapiNameId;
LPSPropTagArray psPropArr = NULL;
hr=pCurFolder-> CreateMessage(NULL,0,&pMessage);
if(FAILED(hr))
goto Exit;
hr = pMessage-> GetIDsFromNames(1, &pmapiNameId, MAPI_CREATE, &psPropArr);
if (FAILED(hr))
{
if (NULL != psPropArr)
{
MAPIFreeBuffer(psPropArr);
psPropArr = NULL;
}
pMessage-> Release();
pCurFolder-> Release();
return hr;
}
if (NULL != psPropArr)
{
propVals[0].ulPropTag = CHANGE_PROP_TYPE(psPropArr[0].aulPropTag[0], PT_BOOLEAN);
propVals[0].Value.b = TRUE;
hr = pMessage-> SetProps(1, propVals, NULL);
ASSERT(SUCCEEDED(hr));
if (FAILED(hr))
{
MAPIFreeBuffer(psPropArr);
psPropArr = NULL;
pMessage-> Release();
pCurFolder-> Release();
return hr;
}
MAPIFreeBuffer(psPropArr);
}
//Data[0].ulPropTag = PR_SUBJECT;
//Data[0].Value.lpszW =pSms-> szSubject;
Data[0].ulPropTag = PR_SUBJECT;
Data[0].Value.lpszW =pSms-> szSubject;
Data[1].ulPropTag = PR_MSG_STATUS;
Data[1].Value.ul = MSGSTATUS_RECTYPE_SMS;
Data[2].ulPropTag = PR_MESSAGE_FLAGS;
Data[2].Value.ul = MSGFLAG_UNSENT|MSGFLAG_FROMME;
pMessage-> SetProps(sizeof(Data)/sizeof(Data[0]),(LPSPropValue)Data,NULL);
SetMsgRecipient(pMessage, pSms);
hr = pMessage-> SubmitMessage(0);
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1667751