[Smart devices] common types of smartphone Development (I) text message sending

Source: Internet
Author: User
Method 1: Use a custom class
Using system;
Using system. runtime. interopservices;
Using system. text;

Namespace smartphone
{
/// <Summary>
/// SMS helper class.
/// </Summary>
Public class SMS
{
Public SMS ()
{
}
Private const string sms_msgtype_text = "Microsoft text SMS protocol"; // Message Protocol Mode

Private const int sms_max_address_length = 256; // max length of address
// Short Message Mode
Private const int sms_mode_receive = 1; // open in receiving mode
Private const int sms_mode_send = 2; // open in sending Mode
Private const int smsat_international = 1; // International format
Private const int ps_message_option_none = 0; // No message options
// Specify the message type by the provider
Private const int ps_message_class0 = 0; // display immediately but not store
Private const int ps_message_class1 = 1; // store and send a confirmation message back to the service center.
Privateconst int ps_message_class2 = 2; // before sending confirmation to the service center, first transfer the message to the text message area on the SIM card, send an unspecified protocol error to the service center if it cannot be stored but there is still space
Privateconst int ps_message_class3 = 3; // a confirmation message is sent back to the service center when the message arrives and can be stored.

Private const int psro_none = 0; // no replacement item

// SMS sending options
Private const int sms_option_delivery_none = 0; // No sending Option
Private const int sms_option_delivery_no_retry = 1; // If this option is set, resend the message. Otherwise, resend the message as scheduled.
// SMS encoding format
Private Enum sms_data_encoding
{
Smsde_optimal = 0, // optimal mode
Smsde_gsm, // use the default GSM 7-bit encoding
Smsde_ucs2, // uses Unicode ucs2 Encoding
}
[Dllimport ("SMS. dll")]
Private Static extern int smsopen (
String ptsmessageprotocol,
Int dwmessagemodes,
Ref intptr psmshhandle,
Int phmessageavailableevent );

/// <Summary>
///
/// </Summary>
/// <Param name = "SMS handle"> </param>
/// <Param name = "Short Message Service Center address, recommended as null"> </param>
/// <Param name = "Short Message destination address"> </param>
/// <Param name = "message validity period, which can be null"> </param>
/// <Param name = "text message data section, which can be null"> </param>
/// <Param name = "the size of the Data part of the text message, which can be 0"> </param>
/// <Param name = "text message provider specified data"> </param>
/// <Param name = "specify the data size of the text message provider"> </param>
/// <Param name = "SMS encoding format"> </param>
/// <Param name = "SMS sending option"> </param>
/// <Param name = "sms id. If it is not empty, it is the ID returned after the SMS is sent successfully. It can be used for smsgetmessagestatus"> </param>
/// <Returns> </returns>
[Dllimport ("SMS. dll")]
Private Static extern int smssendmessage (
Intptr smshhandle,
Int psmsasmscaddress,
Intptr psmsadestinationaddress,
Int pstvalidityperiod,
Intptr pbdata,
Int dwdatasize,
Intptr pbproviderspecificdata,
Int dwproviderspecificdatasize,
Sms_data_encoding smsdedataencoding,
Int dwoptions,
Int psmsmidmessageid );

[Dllimport ("SMS. dll")]
Private Static extern int smsclose (intptr smshhandle );

[Dllimport ("coredll. dll")]
Private Static extern intptr localalloc (uint uflags, uint ubytes );

[Dllimport ("coredll. dll")]
Internal static extern intptr localfree (intptr hmem );

Privatestatic intptr allochglobal (intptr CB)
{
Intptr hmem;

Hmem = localalloc (0x40, (uint) Cb );
Return hmem;
}

Private Static void freehglobal (intptr hglobal)
{
Localfree (hglobal );
}

Public static int sendsms (string number, string message)
{
Int returnvalue = 0;
Intptr smshandle = intptr. zero;
Try
{
If (number. substring (0, 3 )! = "+ 86") // domestic mobile phones must be added to the front of the mobile phone number + 86
Number = "+ 86" + number;
// Set address Structure
Byte [] smsataddresstype = bitconverter. getbytes (smsat_international );
Byte [] ptsaddress = encoding. Unicode. getbytes (number );
Byte [] smsaddresstag = new byte [smsataddresstype. Length + ptsaddress. Length];
Array. Copy (smsataddresstype, 0, smsaddresstag, 0, smsataddresstype. Length );
Array. Copy (ptsaddress, 0, smsaddresstag, smsataddresstype. length, ptsaddress. Length );
Intptr smsaddress = allochglobal (intptr) smsaddresstag. Length );
Marshal. Copy (smsaddresstag, 0, smsaddress, smsaddresstag. Length );

// Set provider Data Structure
Byte [] dwmessageoptions = bitconverter. getbytes (ps_message_option_none );
Byte [] psmessageclass = bitconverter. getbytes (ps_message_class1 );
Byte [] psreplaceoption = bitconverter. getbytes (psro_none );
Byte [] smsproviderdatatag = new byte [dwmessageoptions. Length + psmessageclass. Length + psreplaceoption. Length];
Array. Copy (dwmessageoptions, 0, smsproviderdatatag, 0, dwmessageoptions. Length );
Array. Copy (psmessageclass, 0, smsproviderdatatag, dwmessageoptions. length, psmessageclass. Length );
Array. Copy (psreplaceoption, 0, smsproviderdatatag, dwmessageoptions. Length + psmessageclass. length, psreplaceoption. Length );
Intptr smsproviderdata = allochglobal (intptr) smsproviderdatatag. Length );
Marshal. Copy (smsproviderdatatag, 0, smsproviderdata, smsproviderdatatag. Length );

// Set message
Byte [] smsmessagetag = encoding. Unicode. getbytes (Message );
Intptr smsmessage = allochglobal (intptr) smsmessagetag. Length );
Marshal. Copy (smsmessagetag, 0, smsmessage, smsmessagetag. Length );

// Get handle
If (0! = Smsopen (sms_msgtype_text, sms_mode_send, ref smshandle, 0 ))
Returnvalue =-1; // cocould not open
// Send message
If (0! = Smssendmessage (smshandle, 0, smsaddress, 0,
Smsmessage, smsmessagetag. length,
Smsproviderdata, smsproviderdatatag. length,
Sms_data_encoding.smsde_optimal, sms_option_delivery_none, 0 ))
Returnvalue =-2;

Freehglobal (smsmessage );
Freehglobal (smsproviderdata );
Freehglobal (smsaddress );
}
Finally
{
// Release handle
If (smshandle! = Intptr. Zero)
{
If (0! = Smsclose (smshandle ))
Returnvalue =-3; // cocould not close
}
}

Return returnvalue;
}
}
}

In addition to using custom classes, you can also use the opennetcf toolkit to download the http://www.opennetcf.org here, the current version 1.4
After the installation, reference opennetcf. Phone
The Code is as follows:
....
Using opennetcf. Phone;
Using opennetcf. Phone. SMS;

Private void menuitem2_click (Object sender, system. eventargs E)
{
If (textbox1.text. Trim (). length> 0) & (textbox1.text. Trim (). length> 0 ))
{
SMS mysms = new SMS (smsmode. Send );
Smsaddress myaddr = new smsaddress (); myaddr. Address = "+ 86" + textbox1.text;
Myaddr. type = addresstype. International; int smshandle = mysms. sendmessage (myaddr, textbox2.text );
}}
Note that when you have Framework 1.1 and Framework 2.0 in your system, loading the DLL will fail. The solution is as follows:

Set \ Program Files \ Microsoft Visual Studio. NET 2003 \ compactframeworksdk \ v1.0.5000 \ opennetcf in Windows CE. phone. copy the DLL to the project directory and include the DLL file in the project.

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.