C# 如何在 Windows 应用程序中使用 SMTP 服务器向手机发送短信?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19635594/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 15:28:49  来源:igfitidea点击:

How to send SMS to mobile using SMTP server in windows application?

c#winformsemailsmtpsms

提问by BhushanK


I am developing a windows application using C#, in which i want to send SMS to some user based on some condition. i goes through the many forum post to "Send SMS using SMTP Server" but none of them use-full for me. In thisi got some clue to send SMS through Gmail SMTP but not working as i think it is carrier specific (not sure).
My code sample :


我正在使用 C# 开发 Windows 应用程序,其中我想根据某些条件向某个用户发送 SMS。我浏览了许多论坛帖子以“使用 SMTP 服务器发送短信”,但没有一个对我有用。在这方面,我得到了一些通过 Gmail SMTP 发送 SMS 的线索,但由于我认为它是特定于运营商的(不确定),因此无法正常工作。
我的代码示例:

try
{
    MailMessage message = new MailMessage();
    message.To.Add("[email protected]");
    message.From = new MailAddress("[email protected]"); //See the note afterwards...
    message.Body = "Hi, How r you ?";

    SmtpClient smtp = new SmtpClient("smtp.gmail.com");
    smtp.EnableSsl = true;
    smtp.Port = 587;
    smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
    smtp.Credentials = new NetworkCredential("[email protected]", "password");

    smtp.Send(message);
    MessageBox.Show("Message sent successfully");
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message, "Error");
}

above code not giving any exception or error but also i am not getting any sms on my number as well.

上面的代码没有给出任何异常或错误,但我的号码也没有收到任何短信。

So, what i want to ask that is there any way to send SMS using SMTP server to the mobile number of any carrier?

那么,我想问一下,有什么方法可以使用SMTP服务器向任何运营商的手机号码发送短信?

采纳答案by Jim Mischel

You have to send to the SMS gateway. It is provider specific.

您必须发送到短信网关。它是特定于提供商的。

Wikipedia has a List of SMS Gateways.

维基百科有一个SMS 网关列表

For example, to send to a Sprint PCS number you would send to [email protected], where number is the phone number (i.e. 5551234567, or whatever).

例如,要发送到 Sprint PCS 号码,您可以发送到 [email protected],其中 number 是电话号码(即 5551234567 或其他)。

回答by Rafa Ayadi

For those who have looked so much for a free way to send SMS from a web app, and are in France, and having FreeMobile as operator, I've just found a way in calling a free web service provided by FreeMobile. I've written this code in C# and it works fine.

对于那些一直在寻找从网络应用程序发送 SMS 的免费方式并且在法国并且拥有 FreeMobile 作为运营商的人来说,我刚刚找到了一种调用 FreeMobile 提供的免费网络服务的方法。我已经用 C# 编写了这段代码,它运行良好。

private void SendSMSAlert(String message)
{
    try
    {
        String url = "https://smsapi.free-mobile.fr/sendmsg?user="YourFreeMobileIdentifierHere"&pass="YOURPASSHERE"&msg=" + message;
        var request = (HttpWebRequest)WebRequest.Create(url);
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        response.Close();
    }
    catch(WebException e)
    {
        System.Diagnostics.Trace.WriteLine("SMS Not Sent! Exception "+e.ToString());
    }

}

So if you have a freeMobile line in France, you can get your Pass from https://mobile.free.fr/moncompte/

因此,如果您在法国有 freeMobile 线路,则可以从https://mobile.free.fr/moncompte/获取通票

Then, if you need to forward the SMS to other numbers, it can be done with many mobile apps on AppStore or GooglePlay.

然后,如果您需要将短信转发到其他号码,可以通过 AppStore 或 GooglePlay 上的许多移动应用程序来完成。

I hope this helps!

我希望这有帮助!