winforms 我可以从C#应用程序发送SMS消息吗?

8ehkhllq  于 2023-03-24  发布在  C#
关注(0)|答案(5)|浏览(232)

我希望建立一个程序,将允许我发送短信直接从C#应用程序。我打算建立一个'自动预约提醒'系统,将自动发送短信到收件人移动的通知他们即将到来的约会。
有人能建议我如何实现这种类型的功能,因为我没有经验的'移动的通信'和移动连接与桌面应用程序。

  • 我的运营商是EE(如果这有帮助?)*
ht4b089n

ht4b089n1#

大多数主要运营商都提供电子邮件到文本服务。该程序可以使用电子邮件发送SMS消息。例如:

发送邮件

var message = new MailMessage();
message.From = new MailAddress("sender@foo.bar.com");

message.To.Add(new MailAddress("5551234567@txt.att.net"));//See carrier destinations below
//message.To.Add(new MailAddress("5551234568@txt.att.net"));

//message.CC.Add(new MailAddress("carboncopy@foo.bar.com"));
message.Subject = "This is my subject";
message.Body = "This is the content";

var client = new SmtpClient();
client.Send(message);

运营商目的地

  • ATT:撰写一封新的电子邮件,使用收件人的10位数无线电话号码,后跟@ txt.att.net。例如,5551234567@txt.att.net。
  • Verizon:同样,##@vtext.com
  • Sprint:##@messaging.sprintpcs.com
  • 移动电话:##@移动邮箱.net
  • 维珍移动的:##@vmobl.com
  • Nextel:##@messaging.nextel.com
  • Boost:##@myboostmobile.com
  • Alltel:##@message.alltel.com
  • EE:##@mms.ee.co.uk(可能支持发送而不回复)

替代品

  • vendors通过API提供SMS消息服务
5cnsuln7

5cnsuln72#

Twilio有一个C# helper library可以让你做到这一点。
以下是使用库发送文本消息所需的代码:

using System;
using Twilio;
class Example
{
  static void Main(string[] args)
  {
    // Find your Account Sid and Auth Token at twilio.com/user/account
    string AccountSid = "{{ account_sid }}";
    string AuthToken = "{{ auth_token }}";

    var twilio = new TwilioRestClient(AccountSid, AuthToken);
    var message = twilio.SendMessage("+14158141829", "+14159352345", "This text message was sent with code!");

    Console.WriteLine(message.Sid);
  }
}

免责声明:我为Twilio工作。

rjee0c15

rjee0c153#

您可以通过各种方式发送短信

  • 使用GSM调制解调器
  • 使用Web服务
  • 使用服务提供者提供的终结点

您可以通过下面提供的链接了解上述每一点的基本逻辑,并尝试在代码中实现这一点。
http://www.codeproject.com/Articles/19023/Sending-SMS-using-NET
你需要像这样在表单构造函数中创建一个sms引擎的示例。

public partial class Form1 : Form
    {
        SMSCOMMS SMSEngine;

        public Form1()
        {

                    SMSEngine = new SMSCOMMS("COM1");


            InitializeComponent();
            SMSEngine.Open();
        }

        private void button1_Click(object sender, EventArgs e)
        {
          SMSEngine.SendSMS("919888888888","THIS IS YOUR MESSAGE");
          SMSEngine.Close();
        }
    }
}
gmol1639

gmol16394#

Ozeki的C# sms api以事件的形式提供反馈。这很棒,因为其他短信api-s不提供传递到手机的报告或任何其他关于短信发生情况的真实的反馈。下面是代码。
使用系统;使用OZX;
命名空间OzekiConsoleClient { class Program { static OzxClient Client;

static void Main(string[] args)
    {
        Client = new OzxClient();
        Client.AutoReconnect = true;

        Client.OnMessageAcceptedForDelivery += Client_OnMessageAcceptedForDelivery;
        Client.OnMessageNotAcceptedForDelivery += Client_OnMessageNotAcceptedForDelivery;
        Client.OnMessageSubmitSuccess += Client_OnMessageSubmitSuccess;
        Client.OnMessageSubmitFailed += Client_OnMessageSubmitFailed;
        Client.OnMessageDeliverySuccess += Client_OnMessageDeliverySuccess;
        Client.OnMessageDeliveryFailed += Client_OnMessageDeliveryFailed;
        Client.OnMessageViewed += Client_OnMessageViewed;
        Client.OnConnected += Client_OnConnected;
        Client.OnDisconnected += Client_OnDisconnected;
     
        Client.Connect("127.0.0.1",9580,"testuser","testpass");
    }
    
    static void Client_OnConnected(object sender, EventArgs e)
    {
        Console.WriteLine("Successfully connected.");
         
        var msg = new OzxMessage();
        msg.ToAddress = "+447958448798";
        msg.Text = "Hello world";

        Console.WriteLine("Sending message. ID: "+msg.ID);
        Client.Send(msg);
    }
}

}
您还可以使用此代码通过Android移动的发送SMS,而不是订阅在线SMS服务。
免责声明:我为Ozeki工作。

dffbzjpn

dffbzjpn5#

我使用的是https://d7networks.com/,集成很简单

using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json; // Install-Package Newtonsoft.Json

class Program
{
    static async Task Main(string[] args)
    {
        // Set the URL to send the POST request to
        string url = "https://api.d7networks.com/messages/v1/send";

        // Create a new HttpClient object
        HttpClient client = new HttpClient();

        // Set the bearer token authentication header
        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "YOUR_API_TOKE"); // Replace YOUR_API_TOKEN with your API token

        // Create a new message object with channel, recipients, and content attributes
        var message = new
        {
            originator = "smsinfo", // Replace smsinfo with your sender ID
            recipients = new string[] { "+97150900XXXX"}, // Replace +97150900XXXX with your recipient's number
            content = "Testing multi lined message. \n This is the second line. \n This is the third line."
        };

        // Nest the message object inside a messages object
        var messages = new
        {
            messages = new[] { message }
        };

        // Convert the messages object to a JSON string
        string json = JsonConvert.SerializeObject(messages);

        // Create a new StringContent object with the JSON string
        var content = new StringContent(json, Encoding.UTF8, "application/json");

        // Send the POST request and get the response
        HttpResponseMessage response = await client.PostAsync(url, content);

        // Print the response status code and message
        Console.WriteLine($"Response status code: {response.StatusCode}");
        Console.WriteLine($"Response message: {await response.Content.ReadAsStringAsync()}");
    }
}

相关问题