How to Send SMS in Java using Third Party SMS API’s

How to Send SMS in Java using Third Party SMS API’s : Almost all the bulk SMS providers are sharing the SMS Apis as well to send the SMS from our java/php/any other programming languages. When you are planning to send bulk sms to your customers/users about any offers then you may need to buy bulk SMS from any bulksms provider in india.

I have bought bulksms from shlrtechnosoft, I really enjoyed too with their service.

 

Requirements:

1. Buy the bulk SMS from any bulksms providers. (If you don’t find anyone, comment to this post to help you).
2. Ensure you got userid, password and senderid from them.

 

 

Steps in overview:

  • Create a contacts map with number and name.
  • Iterate the contacts map and send the numbers to the sendSMSThrougAPI() one by one to send the SMS.
  • Jsoup is used here to connect and trigger the SMS using the third party Api.
  • Character count restrictions/sending the name of the contact/different contact groups can be created/customized with this program.

 

Send SMS using Thirdparty APIs – Java Source Code:

Change the below things before running this program.

  • YOUR_SMS_API_USERID
  • YOUR_SMS_API_PASSWORD
  • YOUR_SMS_API_SENDERID
  • YOUR_SMS_API_URL in sendSMSThrougAPI().

[java]

package in.javadomain;

import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

public class SendSMS {

private static String inviteMsg = “Couponzcorner.com: Best Online shopping website in india. \n Couponzcorner.”;
HashMap<String, String> contactsMap = new HashMap<String, String>();
String thirdPartyAPIUserID = “YOUR_SMS_API_USERID”;
String thirdPartyAPIPassword = “YOUR_SMS_API_PASSWORD”;
String thirdPartyAPISenderID = “YOUR_SMS_API_SENDERID”;
// Don’t forget to change the url of the sms api in the sendSMSThrougAPI() method.

public static void main(String[] args) {
System.out.println(“Sending SMS Started”);
SendSMS ssms = new SendSMS();
ssms.getContacts();
System.out.println(“Sending SMS Completed”);
}

public void getContacts(){
// Enable this if loop if you want to restrict the message length to be less than 160
if (inviteMsg.length() <= 159) {
// Testing Contacts
commonIteration(this.getFriends());

// Create other contact groups and add it to commonIteration here to send
// commonIteration(this.getRelations());
}
}

public void commonIteration(HashMap<String, String> contactGrp) {
Set<Map.Entry<String, String>> cmnSet = contactGrp.entrySet();
for (Map.Entry<String, String> mp : cmnSet) {
// if you want to send the name from the map dynamically then use mp.getValue() to get it.
SendSMS sendSMS = new SendSMS();
boolean response = sendSMS.sendSMSThrougAPI(mp.getKey(),
inviteMsg.toString());
System.out.println(mp.getKey() + “==>” + response);
}
}

private boolean sendSMSThrougAPI(String number, String msg) {
boolean isSMSSent = false;
msg = URLEncoder.encode(msg);
try {
String URL = “http://jumbosms.shlrtechnosoft.com/websms/sendsms.aspx?userid=”+thirdPartyAPIUserID+”&password=”+thirdPartyAPIPassword+”&sender=”+thirdPartyAPISenderID+”&mobileno=”+ number + “&msg=” + msg;
Document apiResponse = Jsoup.connect(URL).timeout(0).get();
String responseIS = apiResponse.toString();
if (responseIS.contains(“Success”)) {
isSMSSent = true;
} else {
isSMSSent = false;
}
} catch (Exception e) {
e.printStackTrace();
System.out.println(“Issue with jsoup” + e.getStackTrace());
}
return isSMSSent;
}
public HashMap<String, String> getFriends() {
contactsMap.put(“9999999999″,”Fancy”);
return contactsMap;
}
}

[/java]

 

Console Output:

[plain]
Sending SMS Started
9999999999==>true
Sending SMS Completed

[/plain]

 

 

Feel free to share any thoughts/comments in the below comments section.

Leave a Reply