Send SMS via cemapi on Windows Mobile 6

Source: Internet
Author: User

Send SMS via cemapi poom on Windows Mobile 6

Third-party software on winodws mobile generally uses
1) Call the smsmessagesend API function to send text messages.
Or pass
2) CreateProcess or shellexecuteex method, call the command (excluding angle brackets)
</Windows/tmail.exe-service "SMS"-to "SMS receiver number"-Body "SMS content">

In the following example, we call the SMS program (tmail.exe) that comes with Windows mobile, and edit a text message with the recipient 10086, the text message content cxye, and the query balance.
/Windows/tmail.exe-service "text message"-to "10086"-Body "ktip1000"
The final result is as follows.
<Place holder for SMS send UI>

1) and 2) Both methods have their own advantages and disadvantages.
Method 1: After the SMS is successfully sent, the sent sms cannot be found in the sent items provided by Microsoft. No message is displayed if the message fails to be sent. Of course, you can hardline the poom text message object to achieve this.
In the second method, the tmail.exe-service "SMS"-to ""-Body "method cannot automatically send text messages, but it is stuck on the editing page. You also need to manually click" send, or through a program.

The following describes how to enable the text message to appear in the sent mail box or automatically send it after editing the text message.
This is to send text messages through cemapi. This article has been published in www.codeproject.com for years. Please refer to http://www.codeproject.com/KB/mobile/SMS_CEMPI.aspx for the original article.

I used and practiced this code and found several problems.
1) if the text message fails to be sent, the text message will be sent to the draft box, but the text message cannot be sent again after you click "change.
<Place holder for screen shot>
At this time, the text message content is placed on the subject, which is different from the situation encountered during normal use. The text message content should be in the body.
2) When sending a text message, you need to set a parameter, that is, the local number. It is very troublesome to obtain the local number.
3) This sample is compiled in EVC and the. RC2 file is missing. Although the author has explained how to solve the problem.

However, I have rewritten the following and changed all definitions such as ccomptr <imapisession> to the imapisession recommended on wm6.
Corrected the return value of the hresult type returned by the sendsmsmessage function: Return s_ OK.

But 1) and 2) the problem still needs to be solved.
// --- Smshelper. CPP --- <br/> # include "log. H "<br/> # include" smshelper. H "<br/> ////////////////////////////////// //////////////////////////////////////// //////// // <br/> // This function is used to get the msgstore named SMS from msgstores on the <br/> // device. <br/> // I cocould have used just raw pointers but it is much easier and safer to just <br/> // use smart pointers. <br/> hresult gets Msmsgstore (imapisession * psession, imsgstore ** ppmsgstore) <br/> {<br/> // First we get the msgstores table from the session <br/> imapitable * ptable = NULL; <br/> hresult hR = psession-> getmsgstorestable (mapi_unicode, & ptable); <br/> If (failed (HR) & ptable! = NULL) <br/>{< br/> // MessageBox (null, l "failed: getmsgstorestable", l "getsmsgstore", mb_ OK ); <br/> writestring (L "failed: getmsgstorestable-getsmsgstore"); <br/> return false; <br/>}< br/> writestring (L "passed: getmsgstorestable-getsmsmsgstore "); <br/> // next we loop over the message stores opening each msgstore and <br/> // getting its name to see if the name matches SMS. <br/> // if it doe S then we break out of the loop <br/> while (true) <br/>{< br/> srowset * prowset = NULL; <br/> hR = ptable-> queryrows (1, 0, & prowset ); <br/> // if we failed to query the <br/> // rows then we need to break <br/> If (failed (HR) & prowset! = NULL) <br/>{< br/> // MessageBox (null, l "failed: queryrows", l "getsmsgstore", mb_ OK ); <br/> writestring (L "failed: queryrows-getsmsgstore"); <br/> break; <br/>}< br/> writestring (L "passed: queryrows-getsmsgstore "); <br/> // if we got no rows back then just exit the loop <br/> // remembering to set an error <br/> If (prowset-> crows = 1) <br/>{< br/> assert (prowset-> Arow [0]. lpprops-> ulproptag = Pr_entryid); <br/> sbinary & blob = prowset-> Arow [0]. lpprops-> value. bin; <br/> hR = psession-> openmsgstore (null, blob. CB, (lpentryid) blob. LPB, null, 0, ppmsgstore); <br/> If (failed (HR) <br/>{< br/> // MessageBox (null, l "failed: openmsgstore ", l" getsmsgstore ", mb_ OK); <br/> writestring (L" failed: openmsgstore-getsmsgstore "); <br/>}< br/> writestring (L "passed: openmsgstore-getsmsmsgstore"); <br />}< Br/> else <br/>{< br/> // MessageBox (null, l "prowset-> crows! = 1 ", l" getsmsgstore ", mb_ OK); <br/> writestring (L" prowset-> crows! = 1-getsmsgstore "); <br/> hR = hresult_from_win32 (error_not_found ); <br/>}< br/> // now remember to free the row set <br/> freeprows (prowset); <br/> If (failed (HR )) <br/>{< br/> break; <br/>}< br/> // now get the display name property from the <br/> // message store to compare it against the name <br/> //' SMS '<br/> sproptagarray props; <br/> props. cvalues = 1; <br/> props. aulproptag [0] = pr_display_nam E; <br/> ulong cvalues; <br/> spropvalue * pprops = NULL; <br/> hR = (* ppmsgstore)-> getprops (& props, mapi_unicode, & cvalues, & pprops); <br/> If (failed (HR) | cvalues! = 1) <br/>{< br/> // MessageBox (null, l "failed: getprops", l "getsmsgstore", mb_ OK ); <br/> writestring (L "failed: getprops-getsmsgstore"); <br/> break; <br/>}< br/> writestring (L "passed: getprops-getsmsgstore "); <br/> // if the name matches SMS then break and as <br/> // hR = s_ OK the current msgstore smart pointer <br/> // will correctly be set. <br/> If (_ tcsicmp (pprops [0]. value. lpszw, _ T ("SM S ") = 0) <br/>{< br/> break; <br/>}< br/> // if we failed for some reason then we clear out <br/> // The msgstore smartpointer and return the error. <br/> If (failed (HR) <br/>{< br/> (* ppmsgstore)-> release (); <br/>}< br/> return hr; <br/>}< br/> /////////////////////////////// //////////////////////////////////////// //// // <br/> // This function is used to get the folder named Dr AFTs from the msgstore on the <br/> // device. <br/> // I cocould have used just raw pointers but it is much easier and safer to just <br/> // use smart pointers. <br/> hresult getsmsfolder (imsgstore * pmsgstore, imapifolder ** ppfolder) <br/>{< br/> // now get the drafts folder. <br/> sproptagarray propdefaultfolder; <br/> propdefaultfolder. cvalues = 1; <br/> propdefaultfolder. aulproptag [0] = pr_ce_ipm_draft S_entryid; <br/> ulongcvalues; <br/> response; <br/> hresult hR = pmsgstore-> getprops (& propdefaultfolder, mapi_unicode, & cvalues, & ppropvals ); <br/> If (failed (HR) <br/>{< br/> // MessageBox (null, l "failed: getprops", l "getsmsfolder ", mb_ OK); <br/> writestring (L "failed: getprops-getsmsfolder"); <br/> return hr; <br/>}< br/> writestring (L "passed: getprops-getsmsfolder"); <br/> sbinary & Eiddrafts = ppropvals-> value. bin; <br/> hR = pmsgstore-> openentry (eiddrafts. CB, (lpentryid) eiddrafts. LPB, null, mapi_modify, null, (lpunknown *) ppfolder); <br/> If (failed (HR )) <br/>{< br/> // MessageBox (null, l "failed: openentry", l "getsmsfolder", mb_ OK); <br/> writestring (L "failed: openentry-getsmsfolder "); <br/>}< br/> writestring (L" passed: openentry-getsmsfolder "); <br/> return hr; <br/>}< br /> ////////////////////////////////////// //////////////////////////////////////// //// // <Br/> // This function is used to get the send message. <br/> // This uses an opened mapi session <br/> hresult sendsmsmessage (icemapisession * psession, lpctstr lpszfrom, lpctstr lpszto, and lpctstr lpszmessage) <br/>{< br/> // now get the SMS message store <br/> imsgstore * pmsgstore = NULL; <br/> hresult H R = getsmsgstore (psession, & pmsgstore); <br/> If (failed (HR) & pmsgstore! = NULL) <br/>{< br/> // MessageBox (null, l "failed: getsmsgstore", l "sendsmsmessage", mb_ OK ); <br/> writestring (L "failed: getsmsgstore-sendsmsmessage"); <br/> return hr; <br/>}< br/> writestring (L "passed: getsmsgstore-sendsmsmessage "); </P> <p> imapifolder * pfolder = NULL; <br/> hR = getsmsfolder (pmsgstore, & pfolder ); <br/> If (failed (HR) & pfolder! = NULL) <br/>{< br/> // MessageBox (null, l "failed: getsmsfolder", l "sendsmsmessage", mb_ OK ); <br/> writestring (L "failed: getsmsfolder-sendsmsmessage"); <br/> return hr; <br/>}< br/> writestring (L "passed: getsmsfolder-sendsmsmessage "); </P> <p> iMessage * pmessage = NULL; <br/> hR = pfolder-> createmessage (null, 0, & pmessage ); <br/> If (failed (HR) & pmessage! = NULL) <br/>{< br/> // MessageBox (null, l "failed: createmessage", l "sendsmsmessage", mb_ OK ); <br/> writestring (L "failed: createmessage-sendsmsmessage"); <br/> return hr; <br/>}< br/> writestring (L "passed: createmessage-sendsmsmessage "); <br/> // set the recipients <br/> // set up the required fields for a recipient <br/> spropvalue proprecipient [3]; <br/> // It is vital we clear the property structure <br/> // as there are fields we do not use but mapi seems <br/> // to be sentative to them. <br/> zeromemory (& proprecipient, sizeof (proprecipient); <br/> // set the recipient type which coul be to, CC, BCC <br/> // But ehre must at least be a to field <br/> proprecipient [0]. ulproptag = pr_recipient_type; <br/> proprecipient [0]. value. L = mapi_to; <br/> // we set the type of address to SMS instead of <br/> // SMTP <br/> proprecipient [1]. ulproptag = pr_addrtype; <br/> proprecipient [1]. value. lpszw = _ T ("SMS "); <br/> // We finally set the email address to <br/> // phone number of the person we are sending the message <br/> // to <br/> proprecipient [2]. ulproptag = pr_email_address; <br/> proprecipient [2]. value. lpszw = (lpwstr) lpszto; <br/> // set the addrlist to point to the properties <br/> adrlist; <br/> adrlist. centries = 1; <br/> adrlist. aentries [0]. cvalues = 3; <br/> adrlist. aentries [0]. rgpropvals = (lpspropvalue) (& proprecipient); <br/> // finally modify the recipients of the message <br/> hR = pmessage-> modifyrecipients (modrecip_add, & adrlist ); <br/> If (failed (HR) <br/> {<br/> // MessageBox (null, l "failed: modifyrecipients", l "sendsmsmessage ", mb_ OK); <br/> writestring (L "failed: modifyrecipients-sendsmsmessage"); <br/> return hr; <br/>}< br/> writestring (L "passed: modifyrecipients-sendsmsmessage "); <br/> // now we set the additional properties for the <br/> // message <br/> spropvalue props [4]; <br/> // note how we zero out the contents of the <br/> // structure as mapi is sensative to the <br/> // contents of other fields we do not use. <br/> zeromemory (& props, sizeof (props )); <br/> // first set the subject of the message <br/> // as the SMS we are going to send <br/> props [0]. ulproptag = pr_subject; <br/> props [0]. value. lpszw = (lpwstr) lpszmessage; <br/> // next set the senders email address to <br/> // the phone number of the person we are <br/> // sending the message to <br/> props [1]. ulproptag = pr_sender_email_address; <br/> props [1]. value. lpszw = (lpwstr) lpszfrom; <br/> // Finally and most importantly tell mapi <br/> // This is a SMS message in need of delivery <br/> props [2]. ulproptag = pr_msg_status; <br/> props [2]. value. ul = msgstatus_rectype_sms; <br/> props [3]. ulproptag = pr_message_flags; <br/> props [3]. value. ul = msgflag_fromme | msgflag_unsent; <br/> hR = pmessage-> setprops (sizeof (props)/sizeof (props [0]), (lpspropvalue) & props, null ); <br/> If (failed (HR) <br/>{< br/> // MessageBox (null, l "failed: setprops", l "sendsmsmessage ", mb_ OK); <br/> writestring (L "failed: setprops-sendsmsmessage"); <br/> return hr; <br/>}< br/> writestring (L "passed: setprops-sendsmsmessage "); <br/> // having set all the required fields we can now <br/> // pass the message over to the msgstore transport <br/> // to be delivered. <br/> hR = pmessage-> submitmessage (0); <br/> If (failed (HR) <br/>{< br/> // MessageBox (null, L "failed: submitmessage", l "sendsmsmessage", mb_ OK); <br/> writestring (L "failed: submitmessage-sendsmsmessage"); <br/> return hr; <br/>}< br/> writestring (L "passed: submitmessage-sendsmsmessage"); <br/> return s_ OK; <br/>}< br/> /////////////////////////////// //////////////////////////////////////// // <br/> // This is the function that creates the session, using the <br/> // from, the recipient and the message. <br/> // This opens the session, opens the SMS message store and opens <br/> // The drafts folder then create a new message and sets the sender, <br/> // recipient and mescript, then finally sends the message. <br/> bool dosendmessage (maid, maid, maid) <br/>{< br/> hresult hR = mapiinitialize (null ); <br/> If (failed (HR) <br/>{< br/> // MessageBox (null, l "failed: mapiinitialize", l "dosendmessage ", mb_ OK); <br/> writestring (L "failed: mapiinitialize-dosendmessage"); <br/> return hr; <br/>}< br/> writestring (L "passed: mapiinitialize-dosendmessage "); <br/> // initialized the mapi subsystem <br/> // ccomptr <imapisession> spsession; <br/> icemapisession * psession = NULL; <br/> bool Bret = false; <br/> hR = mapilogonex (0, null, null, 0, (lpmapisession *) & psession ); <br/> If (failed (HR) <br/>{< br/> // MessageBox (null, l "failed: mapilogonex", l "dosendmessage ", mb_ OK); <br/> writestring (L "failed: mapilogonex-dosendmessage "); <br/>}< br/> else <br/> {<br/> writestring (L "passed: mapilogonex-dosendmessage "); <br/> Bret = succeeded (sendsmsmessage (psession, lpszfrom, lpszto, lpszmessage); <br/> If (BRET) <br/> writestring (L "passed: sendsmsmessage-dosendmessage "); <br/> else <br/> writestring (L" failed: sendsmsmessage-dosendmessage "); <br/> psession-> logoff (0, 0, 0); <br/> psession-> release (); <br/>}< br/> mapiuninitialize (); <br/> return Bret; <br/>}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.