---
title: How to send SMS in Java | 100% best example
url: https://kensoftph.com/how-to-send-sms-in-java/
date: 2020-10-04T12:24:00+00:00
modified: 2022-02-07T05:14:00+00:00
lang: en_US
---

# How to send SMS in Java | 100% best example

## How to send SMS in Java

This is how to send SMS in Java with bulksms API. If you don't have an account at bulksms, you can register it [here](https://www.bulksms.com/account/#!/registration) and get your free credits or buy credits. Once you have successfully created an account at bulksms then enter your **username** and **password** as shown below to send an SMS in Java.

```
// change these values to match your own account
String myUsername = "YOUR BULKSMS USERNAME HERE";
String myPassword = "YOUR BULKSMS PASSWORD HERE";
```

Enter the **contact number** instead of 1111111 make sure you include the country code in your contact number and your **message**.

```
// the details of the message we want to send
String myData = "{to: "1111111", encoding: "UNICODE", body: "YOUR MESSAGE HERE"}";
```

## Source Code:

Copy this code to send SMS in Java

```
import java.net.*;
import java.util.Base64;
import java.io.*;

public class MainClass {

static public void main(String[] args) throws Exception {

// This URL is used for sending messages
String myURI = "https://api.bulksms.com/v1/messages";

// change these values to match your own account
String myUsername = "YOUR BULKSMS USERNAME HERE";
String myPassword = "YOUR BULKSMS PASSWORD HERE";

// the details of the message we want to send
String myData = "{to: "CONTACT NUMBER HERE", encoding: "UNICODE", body: "YOUR MESSAGE HERE"}";

// if your message does not contain unicode, the "encoding" is not required:
// String myData = "{to: "1111111", body: "Hello Mr. Smith!"}";

// build the request based on the supplied settings
URL url = new URL(myURI);
HttpURLConnection request = (HttpURLConnection) url.openConnection();
request.setDoOutput(true);

// supply the credentials
String authStr = myUsername + ":" + myPassword;
String authEncoded = Base64.getEncoder().encodeToString(authStr.getBytes());
request.setRequestProperty("Authorization", "Basic " + authEncoded);

// we want to use HTTP POST
request.setRequestMethod("POST");
request.setRequestProperty( "Content-Type", "application/json");

// write the data to the request
OutputStreamWriter out = new OutputStreamWriter(request.getOutputStream());
out.write(myData);
out.close();

// try ... catch to handle errors nicely
try {
// make the call to the API
InputStream response = request.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(response));
String replyText;
while ((replyText = in.readLine()) != null) {
System.out.println(replyText);
}
in.close();
} catch (IOException ex) {
System.out.println("An error occurred:" + ex.getMessage());
BufferedReader in = new BufferedReader(new InputStreamReader(request.getErrorStream()));
// print the detail that comes with the error
String replyText;
while ((replyText = in.readLine()) != null) {
System.out.println(replyText);
}
in.close();
}
request.disconnect();
}
}
```

Please share this blog if you find it helpful!

[![send SMS in java](https://kensoftph.com/wp-content/uploads/2020/10/CODE-scaled.jpg)](https://kensoftph.com/wp-content/uploads/2020/10/CODE-scaled.jpg)

## YouTube Video

https://youtu.be/8rUnadMeTR4
