Windows Mobile发送短信应用分为两种,一是调用Windows Mobile自有的发送短信窗口,将接收用户的手机号码传递给该窗口,完成短信发送(暂且称为窗口短信),另一种是通过调用API将文本发送给指定的号码,这中发送方式对于用户是无感知的(暂且称为无感知短信,下面对这两种方式的实现分别介绍。&G6~-v9c1R*O
窗口短信:
窗口短信实现比较简单,可以直接使用下面的启动参数启动系统的EXE文件即可, System.Diagnostics.Process.Start("tmail.exe", "-transport \"SMS\" -to \"" + 手机号码 + "\" -body \"短信内容\"");但要注意的是,在发送完毕后,短信列表页面会在你关闭发送信息窗口后呈现出来,可以在页面的关闭事件中,使用FindWind方法,找到系统的【短信】窗口,将其关闭即可。
无感知短信
1.Windows Mobile 5.0及以上版本
无感知短信接口与Mobile操作系统的版本有关,如果是基于Windows Mobile5.0 的设备 ,完成短信发送比较容易,因为微软已经给你封装好了底层的API,可以直接进行调用,引用Microsoft.WindowsMobile.PocketOutlook.dll后,使用SmsMessage smsSend = new SmsMessage(phone, sms);smsSend.Send();方法便可以完成对一条短息的发送。
2.Windows Mobile 2003或Windows Mobile2003SE设备. V*L:W"O5e
在5.0以前的版本,虽然现在使用量比较小了,但是在2年前,在其上面发送短息可以说很是费劲,当时确实还让我头疼了一段时间,需要用户自己封装底层的API,然后进行调用,我这里有一个,贴出来,以供大家参考
using System;
using System.Runtime.InteropServices;
using System.Text;
namespace SmartSMS
{
/// <summary>
/// 短信发送程序
/// </summary>
public class SMS
{
public SMS())
{
// TODO: 在此处添加构造函数逻辑
//
}
private const string SMS_MSGTYPE_TEXT = "Microsoft Text SMS Protocol";
private const int SMS_MODE_SEND = 2;
// Open in send mode
private const int SMSDE_GSM = 2;
// Use standard GSM encoding;
private const int SMSAT_INTERNATIONAL = 1;
// International number format
private const int PS_MESSAGE_OPTION_NONE = 0;
// No message options
private const int PS_MESSAGE_CLASS0 = 0;
// Show but do not store
private const int PS_MESSAGE_CLASS1 = 1;
// Store and show
private const int PSRO_NONE = 0;
// No replacements
private const int SMS_OPTION_DELIVERY_NONE = 0;
// No delivery options
private const int SMS_MAX_ADDRESS_LENGTH = 256;
// Max length of address
[DllImport("sms.dll")]
private static extern int SmsOpen(
string ptsMessageProtocol,
int dwMessageModes,
ref IntPtr psmshHandle,
int phMessageAvailableEvent);
[DllImport("sms.dll")]
private static extern int SmsSendMessage(
IntPtr smshHandle,
int psmsaSMSCAddress,
IntPtr psmsaDestinationAddress,
int pstValidityPeriod,
IntPtr pbData,
int dwDataSize,
IntPtr pbProviderSpecificData,
int dwProviderSpecificDataSize,
int 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);
private static 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 = new IntPtr(0);
// 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
// Encoding unicode = Encoding.Unicode;
// Encoding utf8 = Encoding.ASCII;
// byte[] smsMessageTag1 = Encoding.ASCII.GetBytes(Message);
// byte[] smsMessageTag = Encoding.Convert(utf8,unicode,smsMessageTag1);
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; // Could not open
)
// Send message
if (0 != SmsSendMessage(smsHandle, 0, smsAddress, 0,
smsMessage, smsMessageTag.Length,
smsProviderData, smsProviderDataTag.Length,
SMSDE_GSM, SMS_OPTION_DELIVERY_NONE, 0))
returnValue = -2;
FreeHGlobal(smsMessage);
FreeHGlobal(smsProviderData);
FreeHGlobal(smsAddress);
// Release handle
if (0 != SmsClose(smsHandle))
returnValue = -3;
// Could not close
return returnValue;
}
}
}
|
请发表评论