Click here to Skip to main content
11,480,701 members (64,921 online)
Click here to Skip to main content

Sending an SMS using CEMAPI

, 13 Sep 2004 CPOL 804.1K 1.1K 58
Rate this:
Please Sign up or sign in to vote.
How to send an SMS using the CEMAPI APIs.

Sample Image - main.gif

Introduction

If you have not got a Pocket PC Phone Edition or Windows CE SmartPhone device, sorry this article will not be useful.

Now having got that out of the way, this article will show how to send an SMS to another SMS capable device, using the Windows CE MAPI API (A.K.A. CEMAPI) instead of the supplied SMS APIs.

Background

SMS or Simple Messaging Service is a means of sending a short message (160 characters max) to another SMS capable device via the GSM network. On Pocket PC Phone edition devices and Windows CE Smart phones, when selecting a folder, there should have been at least an ActiveSync and SMS message store and possibly an MMS message store as well.

These message stores hold messages from different transport providers locally on the device.

For the purposes of this article, we will be concentrating on the SMS message store as we want to use this to send a message.

Using the code

To make the code simpler and easier to understand, the bulk of the code uses MFC for the User Interface and ATL for smart pointer support.

If people request it, I may make a Win32 only version available as the relevant code (i.e., not the UI code) only requires ATL and not MFC.

In this article, I am not going through a listing of the creation of the MAPI session and opening of the SMS message store and outbox folder. The code is well commented and provided in the source zip file in the two functions GetSMSMsgStore and GetSMSFolder which get the relevant SMS message store and the SMS drafts folder.

So we start looking at the code when we have created a new message in the Outbox folder.

The bulk of the code involves creating the recipients and setting the sender and message status.

First is setting the recipient of the message. To make the code clearer, I have only set one recipient on for the message, although multiple recipients may be set, each one requiring three properties, the type of recipient, the type of address, and the email address.

Unfortunately, I found the code that Microsoft provided for the MAPI samples too complex as they were overshadowed by overly complex memory and pointer manipulation. To that end, I have provided a simpler implementation for recipients.

First, we create the properties that we need for a recipient.

        SPropValue propRecipient[3];
        ZeroMemory(&propRecipient, sizeof(propRecipient));
        // set the recipient type which coul be to, cc, bcc
        // but there must at least be a to field then the 
        propRecipient[0].ulPropTag = PR_RECIPIENT_TYPE;
        propRecipient[0].Value.l = MAPI_TO;
        propRecipient[1].ulPropTag = PR_ADDRTYPE;
        propRecipient[1].Value.lpszW = _T("SMS");
        propRecipient[2].ulPropTag = PR_EMAIL_ADDRESS;
        propRecipient[2].Value.lpszW = (LPWSTR)lpszTo;

If you want to add additional recipients to the message, you can expand the propRecipient to a multiple of three, and add the appropriate recipient type, address type, and address properties.

Next, the ADRLIST structure needs to be set with pointers to the recipient properties and the count of the properties. You will need to provide one ADRENTRY per recipient.

// we create the addlist structure and set it to point to the recipient
// properties
    ADRLIST adrlist;
    adrlist.cEntries = 1;
    adrlist.aEntries[0].cValues = 3;
    adrlist.aEntries[0].rgPropVals = (LPSPropValue)(&propRecipient);

    // finally modify the recipients of the message
    hr = spMessage->ModifyRecipients(MODRECIP_ADD, &adrlist);

Finally, we set the subject of the message as the text of the SMS body to be sent. Note that the length of the SMS message can be no longer than 160 characters as this is the maximum size of a message that can be sent using the GSM Short Messaging Service.

Lastly, we set the remainder of the properties, which are the sender as the person originating the message, and the message record type to SMS so that is gets enumerated by the transport and get sent correctly. Optionally, the message can have the message flags set which indicate that the message is unsent and originates from me.

SPropValue props[4];
ZeroMemory(&props, sizeof(props));
// set the subject
props[0].ulPropTag = PR_SUBJECT;
props[0].Value.lpszW = (LPWSTR)lpszMessage;
//Set the sender
props[1].ulPropTag = PR_SENDER_EMAIL_ADDRESS;
props[1].Value.lpszW = (LPWSTR)lpszFrom;
//Set the message status to indicate this message needs
//enumeration. Vital property!
props[2].ulPropTag = PR_MSG_STATUS;
props[2].Value.ul = MSGSTATUS_RECTYPE_SMS;
// Optional flags property for the message flags
// Seems to work okay without these being set
props[3].ulPropTag = PR_MESSAGE_FLAGS;
props[3].Value.ul = MSGFLAG_FROMME | MSGFLAG_UNSENT;
// Finally set the properties on the message
hr = spMessage->SetProps(sizeof(props) / sizeof(props[0]), 
                             (LPSPropValue)&props, NULL);

As all the properties and recipients have now been set on the message, it is safe to call SubmitMessage.

//Submit the new message to the message store so that it may be submitted to the 
appropriate message transport provider.

hr = spMessage->SubmitMessage(0);

If you wait a couple of seconds, the message will appear on the phone number you specified in the To field of the message. Also note that the sent message will now appear in the Sent Items folder as you would expect.

I have not included a Win32 or SmartPhone version of the code here at the moment. However, if anyone wants versions of it, email me and I will consider updating the samples to include SmartPhone and a pure Win32 version.

Points of Interest

None

History

No revisions thus far.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

EK_Kiwi
Web Developer
United Kingdom United Kingdom
I make computers talk to other computers - well thats that I tell everyone I do. What I really do is connectivity and syncronization software for Windows Ce, PalmOS, Symbian on client devices and MFC/ATL/.NET on the Windows Servers.

I am also an expert with Microsoft Exchange and Databases and pretty good with Lotus Notes as well.

Comments and Discussions

 
QuestionHow can send a same msg to multiplpe recipients at same execution? Pin
"_$h@nky_"1-Jul-09 1:27
member"_$h@nky_"1-Jul-09 1:27 
Generalerror LNK2019: unresolved external symbol Pin
"_$h@nky_"29-Jun-09 21:20
member"_$h@nky_"29-Jun-09 21:20 
QuestionHow to MMS? Pin
JinOtaHuo19-Oct-07 1:24
memberJinOtaHuo19-Oct-07 1:24 
AnswerRe: How to MMS? Pin
Adamwills24-Oct-07 3:04
memberAdamwills24-Oct-07 3:04 
GeneralWIN 32 Pin
Manikumar V3-May-07 10:08
memberManikumar V3-May-07 10:08 
Questionwindows mobile 2005 Pin
eantaru25-Apr-07 19:51
membereantaru25-Apr-07 19:51 
QuestionRe: windows mobile 2005 Pin
eantaru25-Apr-07 21:42
membereantaru25-Apr-07 21:42 
QuestionHow Do i get the count of SMS send in smartphone Pin
Member #36513445-Mar-07 18:29
memberMember #36513445-Mar-07 18:29 
QuestionHow i get datetime of message Pin
navy_blue_whale9-Nov-06 20:54
membernavy_blue_whale9-Nov-06 20:54 
GeneralStatus report Pin
awni2-Nov-06 0:52
memberawni2-Nov-06 0:52 
QuestionMobile transfer Pin
warfans21-Sep-06 1:37
memberwarfans21-Sep-06 1:37 
GeneralGood job Pin
Zhang Zhixiang3-Sep-06 22:59
memberZhang Zhixiang3-Sep-06 22:59 
Questionhow can I read sms in my Pocket PC ? Pin
navy_blue_whale25-Apr-06 23:42
membernavy_blue_whale25-Apr-06 23:42 
QuestionSMS with unicode Pin
AAlGrou19-Apr-06 15:12
memberAAlGrou19-Apr-06 15:12 
AnswerRe: SMS with unicode Pin
lsnail24-May-07 23:21
memberlsnail24-May-07 23:21 
QuestionCan I create a message in the inbox? Pin
guest coder30-Mar-06 7:59
memberguest coder30-Mar-06 7:59 
GeneralRe: Can I create a message in the inbox? Pin
guest coder30-Mar-06 8:02
memberguest coder30-Mar-06 8:02 
AnswerRe: Can I create a message in the inbox? Pin
guest coder31-Mar-06 1:35
memberguest coder31-Mar-06 1:35 
Generalbe careful when using HRESULT !! Pin
Fred D.20-Feb-06 4:41
memberFred D.20-Feb-06 4:41 
GeneralCODED SMS Pin
conney14-Feb-06 22:55
memberconney14-Feb-06 22:55 
AnswerRe: CODED SMS Pin
guest coder30-Mar-06 2:25
memberguest coder30-Mar-06 2:25 
GeneralMessage to the subject 2? And Strange Characters Pin
Ralph Varjabedian17-Jan-06 23:19
memberRalph Varjabedian17-Jan-06 23:19 
AnswerRe: Message to the subject 2? And Strange Characters Pin
guest coder30-Mar-06 2:19
memberguest coder30-Mar-06 2:19 
Questionhow send my remainder to any mobile Pin
mortga13-Nov-05 22:29
membermortga13-Nov-05 22:29 
Questioncan I read SMS from PC Pin
prakasiwi21-Sep-05 23:38
memberprakasiwi21-Sep-05 23:38 
AnswerRe: can I read SMS from PC Pin
EK_Kiwi3-Oct-05 23:36
memberEK_Kiwi3-Oct-05 23:36 
Generalmissing file Pin
AlejandroDG4-Jun-05 23:53
memberAlejandroDG4-Jun-05 23:53 
AnswerRe: missing file Pin
guest coder29-Mar-06 3:30
memberguest coder29-Mar-06 3:30 
GeneralRe: missing file Pin
romyinf9929-Jul-07 18:50
memberromyinf9929-Jul-07 18:50 
Generalmissing file Pin
AlejandroDG4-Jun-05 23:53
sussAlejandroDG4-Jun-05 23:53 
QuestionCan I send an SMS from PC to Mobile?? Pin
ldgiap17-May-05 23:24
memberldgiap17-May-05 23:24 
AnswerRe: Can I send an SMS from PC to Mobile?? Pin
Anonymous30-May-05 21:28
sussAnonymous30-May-05 21:28 
AnswerRe: Can I send an SMS from PC to Mobile?? Pin
guest coder30-Mar-06 2:29
memberguest coder30-Mar-06 2:29 
AnswerRe: Can I send an SMS from PC to Mobile?? Pin
xaml.net13-Jan-07 3:18
memberxaml.net13-Jan-07 3:18 
GeneralMMS Pin
waelahmed12-May-05 16:48
memberwaelahmed12-May-05 16:48 
GeneralRe: MMS Pin
EK_Kiwi30-May-05 21:29
memberEK_Kiwi30-May-05 21:29 
QuestionHas anyone tried sending MMS msg using MAPI? Pin
-Larry1809-May-05 13:59
member-Larry1809-May-05 13:59 
AnswerRe: Has anyone tried sending MMS msg using MAPI? Pin
EK_Kiwi30-May-05 21:43
memberEK_Kiwi30-May-05 21:43 
GeneralRe: Has anyone tried sending MMS msg using MAPI? Pin
Erik Campo30-Jun-05 23:46
memberErik Campo30-Jun-05 23:46 
GeneralRe: Has anyone tried sending MMS msg using MAPI? Pin
agsthom8-Sep-05 0:20
memberagsthom8-Sep-05 0:20 
GeneralRe: Has anyone tried sending MMS msg using MAPI? Pin
merchecaso27-Jul-06 0:16
membermerchecaso27-Jul-06 0:16 
GeneralSending MMS Pin
hira8316-Apr-05 6:09
memberhira8316-Apr-05 6:09 
Generalcheck received message Pin
lvguangchuan14-Mar-05 17:28
memberlvguangchuan14-Mar-05 17:28 
GeneralRe: check received message Pin
EK_Kiwi30-May-05 21:42
memberEK_Kiwi30-May-05 21:42 
QuestionMessage to the subject? Pin
kurtak16-Feb-05 9:48
memberkurtak16-Feb-05 9:48 
AnswerRe: Message to the subject? Pin
HuangGX16-Feb-05 12:13
memberHuangGX16-Feb-05 12:13 
AnswerRe: Message to the subject? Pin
guest coder30-Mar-06 2:38
memberguest coder30-Mar-06 2:38 
QuestionUn-recognizied characters when sending SMS by CEMAPI?? Pin
HuangGX15-Feb-05 8:00
memberHuangGX15-Feb-05 8:00 
AnswerRe: Un-recognizied characters when sending SMS by CEMAPI?? Pin
HuangGX15-Feb-05 8:04
memberHuangGX15-Feb-05 8:04 
GeneralRe: Un-recognizied characters when sending SMS by CEMAPI?? Pin
nbcool25-Sep-05 3:47
membernbcool25-Sep-05 3:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150520.1 | Last Updated 13 Sep 2004
Article Copyright 2004 by EK_Kiwi
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid