🚀 Love Java & Cloud Tutorials?
Don't miss out on the latest coding guides! Join our community of developers.
SUBSCRIBE TO RAM N JAVAHow to Send SMS Directly using AWS SNS in Java
Sending a text message shouldn't always require complex setups like creating "Topics." In this guide, I will show you how to use AWS Simple Notification Service (SNS) to send direct SMS messages to any phone number using Java.
Step 1: Prerequisites
- An active AWS Account.
- AWS SDK for Java dependencies added to your
pom.xml. - Your AWS User must have SNS:Publish permissions.
Step 2: Set Up the SMS Sandbox
AWS requires you to verify destination numbers if you are in the Sandbox environment:
- Log into your AWS Console and search for SNS.
- Go to Mobile -> Text messaging (SMS).
- Under Sandbox destination phone numbers, click "Add phone number."
- Enter your mobile number and verify it using the OTP sent to your phone.
Step 3: The Java Implementation
To send the message, we create an SnsClient and use a PublishRequest. Here is the logic:
SnsClient snsClient = SnsClient.builder().build();
// Create Publish Request
PublishRequest request = PublishRequest.builder()
.message("Hello from Ram N Java!")
.phoneNumber("+1234567890")
.build();
// Send SMS
PublishResponse result = snsClient.publish(request);
System.out.println("Message sent. ID: " + result.messageId());
Summary
By following these steps, you can integrate SMS notifications directly into your Java applications without the overhead of SNS Topics. This is perfect for one-time alerts, OTPs, or direct notifications.