How to send SMS to mobile using SMTP server in win

2019-02-17 13:45发布

问题:


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 this i got some clue to send SMS through Gmail SMTP but not working as i think it is carrier specific (not sure).
My code sample :

try
{
    MailMessage message = new MailMessage();
    message.To.Add("1568235685@sms.sancharnet.in");
    message.From = new MailAddress("sameone@gmail.com"); //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("someonet@gmail.com", "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?

回答1:

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

Wikipedia has a List of SMS Gateways.

For example, to send to a Sprint PCS number you would send to number@messaging.sprintpcs.com, where number is the phone number (i.e. 5551234567, or whatever).



回答2:

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.

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/

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

I hope this helps!