Send SMS messages with Azure Communication Services SMS Java SDK. Use when implementing SMS notifications, alerts, OTP delivery, bulk messaging, or delivery reports.
Add this skill
npx mdskills install sickn33/azure-communication-sms-javaComprehensive Azure SMS integration with clear examples, multiple auth methods, and error handling patterns
1---2name: azure-communication-sms-java3description: Send SMS messages with Azure Communication Services SMS Java SDK. Use when implementing SMS notifications, alerts, OTP delivery, bulk messaging, or delivery reports.4package: com.azure:azure-communication-sms5---67# Azure Communication SMS (Java)89Send SMS messages to single or multiple recipients with delivery reporting.1011## Installation1213```xml14<dependency>15 <groupId>com.azure</groupId>16 <artifactId>azure-communication-sms</artifactId>17 <version>1.2.0</version>18</dependency>19```2021## Client Creation2223```java24import com.azure.communication.sms.SmsClient;25import com.azure.communication.sms.SmsClientBuilder;26import com.azure.identity.DefaultAzureCredentialBuilder;2728// With DefaultAzureCredential (recommended)29SmsClient smsClient = new SmsClientBuilder()30 .endpoint("https://<resource>.communication.azure.com")31 .credential(new DefaultAzureCredentialBuilder().build())32 .buildClient();3334// With connection string35SmsClient smsClient = new SmsClientBuilder()36 .connectionString("<connection-string>")37 .buildClient();3839// With AzureKeyCredential40import com.azure.core.credential.AzureKeyCredential;4142SmsClient smsClient = new SmsClientBuilder()43 .endpoint("https://<resource>.communication.azure.com")44 .credential(new AzureKeyCredential("<access-key>"))45 .buildClient();4647// Async client48SmsAsyncClient smsAsyncClient = new SmsClientBuilder()49 .connectionString("<connection-string>")50 .buildAsyncClient();51```5253## Send SMS to Single Recipient5455```java56import com.azure.communication.sms.models.SmsSendResult;5758// Simple send59SmsSendResult result = smsClient.send(60 "+14255550100", // From (your ACS phone number)61 "+14255551234", // To62 "Your verification code is 123456");6364System.out.println("Message ID: " + result.getMessageId());65System.out.println("To: " + result.getTo());66System.out.println("Success: " + result.isSuccessful());6768if (!result.isSuccessful()) {69 System.out.println("Error: " + result.getErrorMessage());70 System.out.println("Status: " + result.getHttpStatusCode());71}72```7374## Send SMS to Multiple Recipients7576```java77import com.azure.communication.sms.models.SmsSendOptions;78import java.util.Arrays;79import java.util.List;8081List<String> recipients = Arrays.asList(82 "+14255551111",83 "+14255552222",84 "+14255553333"85);8687// With options88SmsSendOptions options = new SmsSendOptions()89 .setDeliveryReportEnabled(true)90 .setTag("marketing-campaign-001");9192Iterable<SmsSendResult> results = smsClient.sendWithResponse(93 "+14255550100", // From94 recipients, // To list95 "Flash sale! 50% off today only.",96 options,97 Context.NONE98).getValue();99100for (SmsSendResult result : results) {101 if (result.isSuccessful()) {102 System.out.println("Sent to " + result.getTo() + ": " + result.getMessageId());103 } else {104 System.out.println("Failed to " + result.getTo() + ": " + result.getErrorMessage());105 }106}107```108109## Send Options110111```java112SmsSendOptions options = new SmsSendOptions();113114// Enable delivery reports (sent via Event Grid)115options.setDeliveryReportEnabled(true);116117// Add custom tag for tracking118options.setTag("order-confirmation-12345");119```120121## Response Handling122123```java124import com.azure.core.http.rest.Response;125126Response<Iterable<SmsSendResult>> response = smsClient.sendWithResponse(127 "+14255550100",128 Arrays.asList("+14255551234"),129 "Hello!",130 new SmsSendOptions().setDeliveryReportEnabled(true),131 Context.NONE132);133134// Check HTTP response135System.out.println("Status code: " + response.getStatusCode());136System.out.println("Headers: " + response.getHeaders());137138// Process results139for (SmsSendResult result : response.getValue()) {140 System.out.println("Message ID: " + result.getMessageId());141 System.out.println("Successful: " + result.isSuccessful());142143 if (!result.isSuccessful()) {144 System.out.println("HTTP Status: " + result.getHttpStatusCode());145 System.out.println("Error: " + result.getErrorMessage());146 }147}148```149150## Async Operations151152```java153import reactor.core.publisher.Mono;154155SmsAsyncClient asyncClient = new SmsClientBuilder()156 .connectionString("<connection-string>")157 .buildAsyncClient();158159// Send single message160asyncClient.send("+14255550100", "+14255551234", "Async message!")161 .subscribe(162 result -> System.out.println("Sent: " + result.getMessageId()),163 error -> System.out.println("Error: " + error.getMessage())164 );165166// Send to multiple with options167SmsSendOptions options = new SmsSendOptions()168 .setDeliveryReportEnabled(true);169170asyncClient.sendWithResponse(171 "+14255550100",172 Arrays.asList("+14255551111", "+14255552222"),173 "Bulk async message",174 options)175 .subscribe(response -> {176 for (SmsSendResult result : response.getValue()) {177 System.out.println("Result: " + result.getTo() + " - " + result.isSuccessful());178 }179 });180```181182## Error Handling183184```java185import com.azure.core.exception.HttpResponseException;186187try {188 SmsSendResult result = smsClient.send(189 "+14255550100",190 "+14255551234",191 "Test message"192 );193194 // Individual message errors don't throw exceptions195 if (!result.isSuccessful()) {196 handleMessageError(result);197 }198199} catch (HttpResponseException e) {200 // Request-level failures (auth, network, etc.)201 System.out.println("Request failed: " + e.getMessage());202 System.out.println("Status: " + e.getResponse().getStatusCode());203} catch (RuntimeException e) {204 System.out.println("Unexpected error: " + e.getMessage());205}206207private void handleMessageError(SmsSendResult result) {208 int status = result.getHttpStatusCode();209 String error = result.getErrorMessage();210211 if (status == 400) {212 System.out.println("Invalid phone number: " + result.getTo());213 } else if (status == 429) {214 System.out.println("Rate limited - retry later");215 } else {216 System.out.println("Error " + status + ": " + error);217 }218}219```220221## Delivery Reports222223Delivery reports are sent via Azure Event Grid. Configure an Event Grid subscription for your ACS resource.224225```java226// Event Grid webhook handler (in your endpoint)227public void handleDeliveryReport(String eventJson) {228 // Parse Event Grid event229 // Event type: Microsoft.Communication.SMSDeliveryReportReceived230231 // Event data contains:232 // - messageId: correlates to SmsSendResult.getMessageId()233 // - from: sender number234 // - to: recipient number235 // - deliveryStatus: "Delivered", "Failed", etc.236 // - deliveryStatusDetails: detailed status237 // - receivedTimestamp: when status was received238 // - tag: your custom tag from SmsSendOptions239}240```241242## SmsSendResult Properties243244| Property | Type | Description |245|----------|------|-------------|246| `getMessageId()` | String | Unique message identifier |247| `getTo()` | String | Recipient phone number |248| `isSuccessful()` | boolean | Whether send succeeded |249| `getHttpStatusCode()` | int | HTTP status for this recipient |250| `getErrorMessage()` | String | Error details if failed |251| `getRepeatabilityResult()` | RepeatabilityResult | Idempotency result |252253## Environment Variables254255```bash256AZURE_COMMUNICATION_ENDPOINT=https://<resource>.communication.azure.com257AZURE_COMMUNICATION_CONNECTION_STRING=endpoint=https://...;accesskey=...258SMS_FROM_NUMBER=+14255550100259```260261## Best Practices2622631. **Phone Number Format** - Use E.164 format: `+[country code][number]`2642. **Delivery Reports** - Enable for critical messages (OTP, alerts)2653. **Tagging** - Use tags to correlate messages with business context2664. **Error Handling** - Check `isSuccessful()` for each recipient individually2675. **Rate Limiting** - Implement retry with backoff for 429 responses2686. **Bulk Sending** - Use batch send for multiple recipients (more efficient)269270## Trigger Phrases271272- "send SMS Java", "text message Java"273- "SMS notification", "OTP SMS", "bulk SMS"274- "delivery report SMS", "Azure Communication Services SMS"275
Full transparency — inspect the skill content before installing.