Search code examples
javaweb-servicessmssms-gateway

How to send SMS to mobile phone from java program- Using free APIs or WebServices


I need to write a program that should be able to send a text SMS to mobile phone through java programming. What i got to know so far is using SMS Gateways and connect modem with sim card.

But I cannot install SMS Gateways and modem on client machine. Can i use smtp host.

How can i use Web Service to send sms?

Your time and contribution will be highly appreciated.


Solution

  • Using the twilio SMS gateway, you can easily accomplish this.

    import com.twilio.sdk.TwilioRestClient;
    import com.twilio.sdk.TwilioRestException;
    import com.twilio.sdk.resource.factory.MessageFactory;
    import com.twilio.sdk.resource.instance.Message;
    import org.apache.http.NameValuePair;
    import org.apache.http.message.BasicNameValuePair;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class Example {
    
      // Find your Account Sid and Token at twilio.com/user/account
      public static final String ACCOUNT_SID = "AC32a3c49700934481addd5ce1659f04d2";
      public static final String AUTH_TOKEN = "{{ auth_token }}";
    
      public static void main(String[] args) throws TwilioRestException {
        TwilioRestClient client = new TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN);
    
        // Build a filter for the MessageList
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("Body", "Abdul please?! I show you"));
        params.add(new BasicNameValuePair("To", "+14159352345"));
        params.add(new BasicNameValuePair("From", "+14158141829"));
    
        MessageFactory messageFactory = client.getAccount().getMessageFactory();
        Message message = messageFactory.create(params);
        System.out.println(message.getSid());
      }
    }
    

    Here is a link to the twilio library: https://www.twilio.com/docs/java/install