Where Developers Build for Developers

JAVA

import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Base64;

public class SMSPoh {
    private static final String API_KEY = "YOUR_API_KEY";
    private static final String API_SECRET = "YOUR_API_SECRET";
    private static final String SENDER_ID = "YourSenderID";
    private static final String RECIPIENT = "+959********";
    private static final String MESSAGE = "Hello, this is a test message from SMSPoh!";

    public static void main(String[] args) {
        try {
            sendSMS(RECIPIENT, MESSAGE);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void sendSMS(String recipient, String message) throws Exception {
        String apiUrl = "https://v3.smspoh.com/api/rest/send";

        // Prepare the payload
        String payload = String.format("{\"to\":\"%s\",\"message\":\"%s\",\"from\":\"%s\"}", recipient, message, SENDER_ID);

        // Encode the API key and secret
        String credentials = Base64.getEncoder().encodeToString((API_KEY + ":" + API_SECRET).getBytes());

        // Set up the connection
        URL url = new URL(apiUrl);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Authorization", "Bearer " + credentials);
        connection.setRequestProperty("Content-Type", "application/json");
        connection.setDoOutput(true);

        // Send the request
        try (OutputStream os = connection.getOutputStream()) {
            byte[] input = payload.getBytes("utf-8");
            os.write(input, 0, input.length);
        }

        // Read the response
        int responseCode = connection.getResponseCode();
        if (responseCode == HttpURLConnection.HTTP_OK) {
            System.out.println("Message sent successfully!");
        } else {
            System.out.println("Failed to send message. HTTP Response Code: " + responseCode);
        }
    }
}