Skip to content

Commit 49a5271

Browse files
committed
Adding instructions for using async email client in Java SDK quickstart
1 parent a7b8779 commit 49a5271

File tree

1 file changed

+44
-6
lines changed

1 file changed

+44
-6
lines changed

articles/communication-services/quickstarts/email/includes/send-email-java.md

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,7 @@ package com.communication.quickstart;
9090

9191
import com.azure.communication.email.models.*;
9292
import com.azure.communication.email.*;
93-
import com.azure.core.util.polling.PollResponse;
94-
import com.azure.core.util.polling.SyncPoller;
93+
import com.azure.core.util.polling.*;
9594

9695
public class App
9796
{
@@ -155,19 +154,43 @@ EmailClient emailClient = new EmailClientBuilder()
155154

156155
For simplicity, this quickstart uses connection strings, but in production environments, we recommend using [service principals](../../../quickstarts/identity/service-principal.md).
157156

157+
#### Creating async client
158158

159+
The [Azure SDK for Java also contains non-blocking, asynchronous APIs for interacting with Azure services](https://learn.microsoft.com/en-us/azure/developer/java/sdk/async-programming).
160+
161+
To instantiate an async client, add the following code to the `main` method:
162+
163+
```java
164+
String connectionString = "endpoint=https://<resource-name>.communication.azure.com/;accesskey=<access-key>";
165+
166+
EmailAsyncClient emailClient = new EmailClientBuilder()
167+
.connectionString(connectionString)
168+
.buildAsyncClient();
169+
```
159170

160171
## Basic email sending
161172

162-
To send an email message, call the `beginSend` function from the `EmailClient`. This method returns a poller, which can be used to check on the status of the operation and retrieve the result once it's finished. Note that the initial request to send an email will not be sent until either the `poll` method is called or we wait for completion of the poller.
173+
An email message can be crafted using the `EmailMessage` object in the SDK.
163174

164175
```java
165176
EmailMessage message = new EmailMessage()
166177
.setSenderAddress("<[email protected]>")
167178
.setToRecipients("<[email protected]>")
168179
.setSubject("Welcome to Azure Communication Services Email")
169180
.setBodyPlainText("This email message is sent from Azure Communication Services Email using the Java SDK.");
181+
```
182+
183+
Make these replacements in the code:
184+
- Replace `<[email protected]>` with the email address you would like to send a message to.
185+
- Replace `<[email protected]>` with the MailFrom address of your verified domain.
170186

187+
To send the email message, call the `beginSend` function from the `EmailClient`. This method is present in both the sync and async client.
188+
189+
#### Email sending with the sync client
190+
191+
Calling `beginSend` on the sync client method returns a `SyncPoller` object, which can be used to check on the status of the operation and retrieve the result once it's finished. Note that the initial request to send an email will be sent as soon as the `beginSend` method is called.
192+
193+
```java
171194
try
172195
{
173196
SyncPoller<EmailSendResult, EmailSendResult> poller = emailClient.beginSend(message, null);
@@ -208,10 +231,25 @@ catch (Exception exception)
208231
}
209232
```
210233

211-
Make these replacements in the code:
234+
#### Email sending with the async client
212235

213-
- Replace `<[email protected]>` with the email address you would like to send a message to.
214-
- Replace `<[email protected]>` with the MailFrom address of your verified domain.
236+
Calling `beginSend` on the async client method returns a `PollerFlux` object to which you can subscribe. You will want to set up the subscriber in a seperate process to take advantage of the asynchronous functionality. Note that the initial request to send an email will not be sent until a subscriber is set up.
237+
238+
```java
239+
emailAsyncClient.beginSend(emailMessage).subscribe(
240+
response -> {
241+
if (response.getStatus() == LongRunningOperationStatus.SUCCESSFULLY_COMPLETED) {
242+
System.out.printf("Successfully sent the email (operation id: %s)", response.getValue().getId());
243+
}
244+
else {
245+
System.out.println("Email send status: " + response.getStatus());
246+
}
247+
},
248+
error -> {
249+
System.out.println("Error occurred while sending email: " + error.getMessage());
250+
}
251+
);
252+
```
215253

216254
### Run the code
217255

0 commit comments

Comments
 (0)