How Can I Send Sms Using Java?


If you are using Windows, download and Install Adoptium. After installation, verify by running the following command in the Command Prompt (cmd):


java -version

If you are using Linux, run the following command in the Terminal to install Java:


sudo apt install default-jdk # Debian/Ubuntu brew install openjdk # macOS java -version # Check Java version

Copy and paste this code into a Java file, save it as SendSMS.java, and replace its contents with the provided Java code. Then, update the Authentication_id and Authentication_key with your credentials. Click Authentication Key ID, and a page will display your 32-character Authentication ID and 32-character Authentication Key, which you can copy and paste into the script.


import java.net.HttpURLConnection; import java.net.URL; import java.io.OutputStream; import java.io.BufferedReader; import java.io.InputStreamReader; public class SendSMS { public static void main(String[] args) { try { String url = "https://msgpserver.com/api/"; String jsonPayload = "{" + "\"mtype\": \"N\"," + "\"to\": \"+91XXXXXXXXXX\"," + "\"message\": \"This is a test message from JAVA...\"," + "\"auth_id\": \"Authentication_Id\"," + "\"auth_key\": \"Authentication_Key\"}"; URL obj = new URL(url); HttpURLConnection con = (HttpURLConnection) obj.openConnection(); con.setRequestMethod("POST"); con.setRequestProperty("Content-Type", "application/json"); con.setDoOutput(true); try (OutputStream os = con.getOutputStream()) { byte[] input = jsonPayload.getBytes("utf-8"); os.write(input, 0, input.length); } int responseCode = con.getResponseCode(); System.out.println("Response Code: " + responseCode); BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream(), "utf-8")); StringBuilder response = new StringBuilder(); String line; while ((line = in.readLine()) != null) { response.append(line); } in.close(); System.out.println("Response: " + response.toString()); } catch (Exception e) { e.printStackTrace(); } } }

Return Success Response.


[ SUCCESS ] {'status': 'success', 'login': 'success', 'recharge': 'success', 'host': 'success', 'msg': 'success'}

Return Error Response.


[ ERROR ] {'status': 'success', 'login': 'success', 'recharge': 'success', 'host': 'error', 'msg': 'error'} {'status': 'success', 'login': 'success', 'recharge': 'expire', 'host': 'error', 'msg': 'error'} {'status': 'online', 'login': 'error', 'recharge': 'error', 'host': 'error', 'msg': 'error'}


Are you satisfied this Answer?