65.9K
CodeProject 正在变化。 阅读更多。
Home

如何从桌面应用程序发送 SMS 短信

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.94/5 (79投票s)

2012年6月1日

CPOL

2分钟阅读

viewsIcon

390606

downloadIcon

18270

如何为桌面应用程序添加发送短信(SMS)消息的功能

从 Win32 C++ 程序发送 WhatsApp 消息 - 第 1 部分 - 56.7 KB

背景

本文重点介绍使用 MFC / C++ 的实现。在寻找一种可靠且廉价的以编程方式发送短信解决方案时,我了解到一家名为 CardBoardFish 的公司,该公司覆盖 150 个国家/地区,并为从任何网站、手机或桌面应用程序进行接口提供易于使用但功能强大的 SDK,涵盖大多数平台和开发环境。不幸的是,在其网站的代码示例中,没有 C++ 示例,所以我决定开发自己的 C++ 实现。

以编程方式发送短信

大多数应用程序和网站都将发送短信作为其范围或与其他功能的一部分(例如,发送警报等),它们使用以下方法之一:

  • HTTP Web 服务 -  需要使用 HTTP “GET” 方法向给定的 Web 服务发送命令,使用包含凭据、参数和消息文本的 API。
  • 电子邮件到短信  - 使用 SMTP 协议允许发送格式独特的电子邮件,该电子邮件将所有必需的参数(凭据、发送者、接收者等)编码为电子邮件的一部分。

本文重点介绍第一种方法,即使用 Web 服务。

API

下表列出了可以(或应该)发送到 Web 服务的全部参数:

Using the Code

本文中的代码是在 Visual Studio 2010 Ultimate 中使用 MFC / C++ 开发的。我还使用了 Cheng Shi 的 HTTPClient(感谢 Cheng!)。

为了在您自己的应用程序中使用此代码,建议阅读名为 HTTPSMS 的 SDK 的规范。其次,您需要创建一个帐户并获取您的用户名和密码,这些密码可以硬编码在源代码中,也可以在运行时输入。

SendSMS 应用程序  

SendSMS Screen

我们应用程序的主要功能显然是发送短信,这在以下函数中完成:

// SendSms  - by Michael Haephrati
BOOL SendSms(CString From, CString CountryCode, CString To,CString Message,CString *Status)
    // From - the ID or number you are sending from. 
    // This is what will appear at the recipient's cellphone. 
    // CountyCode - the code of the country you are sending the SMS to 
    // (for example: 44 is for the UK
    // To - is the number you are texting to, 
    // which should not contain any leading zeros, spaces, commas, etc.
    // Message - is the message you are sending, which can be any multi lingual text
    // The status returned would be either a confirmation number along with the text "OK", 
    // which means that the message
    // was delivered, or an error code. 
{
    BOOL result=FALSE;
    wstring user=L"PLACE_YOUR_USERNAME_HERE",pass=L"PLACE_YOUR_PASSWORD_HERE",request=L"";
    // 
    request=L"http:   //sms1.cardboardfish.com:9001/HTTPSMS?S=H&UN=";
    request+=user;    // user name
    request+=L"&P=";
    request+=pass;    // password
    request+=L"&DA="; 
    request+=(wstring)(CountryCode+To); // country code
    request+=L"&SA="; 
    request+=(wstring)From;             // From (sender ID)
    request+=L"&M=";
    CString EncodedMessage;             // Message
    
    CString ccc;
    EncodedMessage=ConvertHex(Message)+ConvertHex
                   ( L" here you can place your marketing piech, website, etc.");
    
    request+=(wstring)EncodedMessage;   // Message to send

    request+=L"&DC=4";
    // Indicating that this message is encoded as opposed to plain text 

现在我们处理 HTTP “GET” 请求:

    WinHttpClient client(request); 
        
    client.SendHttpRequest(L"GET",true);
    // Get the response

    wstring httpResponseHeader = client.GetResponseHeader();
    wstring httpResponseContent = client.GetResponseContent();
    *Status=httpResponseContent.c_str();
    return result; 
} 

其他服务

我已经测试了 CardBoardFish 的服务,我将其用于附带的源代码。他们提供了自己的代码示例 此处,但这些不包括 C++,这就是我编写附带在本文章中的测试应用程序的原因。

我最近测试了他们提供的另一项服务,即在发送短信之前验证手机号码。我没有包含此功能,因为我发现它太慢了,而且它也不涵盖某些国家/地区,其中包括……美国。  

我找到了另一个替代服务提供商,http://www.clickatell.com,因此有多种选择可供选择。  

进一步阅读 

请参阅 我的另一篇文章,这次解释如何使用 iOS(iPhone / iPad)执行相同的操作。 

历史

  • 2013 年 7 月 1 日:初始版本
© . All rights reserved.