Skip to content

Commit 4b66969

Browse files
Merge pull request #275823 from yogeshmo/email/quickstart-async-client
Adding instructions for using async email client in Java SDK quickstart
2 parents 2e75412 + efdf24a commit 4b66969

File tree

1 file changed

+76
-9
lines changed

1 file changed

+76
-9
lines changed

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

Lines changed: 76 additions & 9 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
{
@@ -104,13 +103,13 @@ public class App
104103

105104
## Creating the email client with authentication
106105

107-
There are a few different options available for authenticating an email client:
106+
There are a few different options available for authenticating an email client.
108107

109108
#### [Connection String](#tab/connection-string)
110109

111110
To authenticate a client, you instantiate an `EmailClient` with your connection string. Learn how to [manage your resource's connection string](../../create-communication-resource.md#store-your-connection-string). You can also initialize the client with any custom HTTP client that implements the `com.azure.core.http.HttpClient` interface.
112111

113-
To instantiate a client, add the following code to the `main` method:
112+
To instantiate a synchronous client, add the following code to the `main` method:
114113

115114
```java
116115
// You can get your connection string from your resource in the Azure portal.
@@ -121,6 +120,17 @@ EmailClient emailClient = new EmailClientBuilder()
121120
.buildClient();
122121
```
123122

123+
To instantiate an asynchronous client, add the following code to the `main` method:
124+
125+
```java
126+
// You can get your connection string from your resource in the Azure portal.
127+
String connectionString = "endpoint=https://<resource-name>.communication.azure.com/;accesskey=<access-key>";
128+
129+
EmailAsyncClient emailClient = new EmailClientBuilder()
130+
.connectionString(connectionString)
131+
.buildAsyncClient();
132+
```
133+
124134
<a name='azure-active-directory'></a>
125135

126136
#### [Microsoft Entra ID](#tab/aad)
@@ -129,6 +139,8 @@ A [DefaultAzureCredential](https://github.com/Azure/azure-sdk-for-java/tree/main
129139

130140
The `AZURE_CLIENT_SECRET`, `AZURE_CLIENT_ID`, and `AZURE_TENANT_ID` environment variables are needed to create a `DefaultAzureCredential` object.
131141

142+
To instantiate a synchronous client, add the following code to the `main` method:
143+
132144
```java
133145
// You can find your endpoint and access key from your resource in the Azure portal
134146
String endpoint = "https://<resource-name>.communication.azure.com/";
@@ -138,10 +150,34 @@ EmailClient emailClient = new EmailClientBuilder()
138150
.buildClient();
139151
```
140152

153+
To instantiate an asynchronous client, add the following code to the `main` method:
154+
155+
```java
156+
// You can find your endpoint and access key from your resource in the Azure portal
157+
String endpoint = "https://<resource-name>.communication.azure.com/";
158+
EmailAsyncClient emailClient = new EmailClientBuilder()
159+
.endpoint(endpoint)
160+
.credential(new DefaultAzureCredentialBuilder().build())
161+
.buildAsyncClient();
162+
```
163+
141164
#### [AzureKeyCredential](#tab/azurekeycredential)
142165

143166
Email clients can also be created and authenticated using the endpoint and Azure Key Credential acquired from an Azure Communication Resource in the [Azure portal](https://portal.azure.com/).
144167

168+
To instantiate a synchronous client, add the following code to the `main` method:
169+
170+
```java
171+
String endpoint = "https://<resource-name>.communication.azure.com";
172+
AzureKeyCredential azureKeyCredential = new AzureKeyCredential("<access-key>");
173+
EmailClient emailClient = new EmailClientBuilder()
174+
.endpoint(endpoint)
175+
.credential(azureKeyCredential)
176+
.buildClient();
177+
```
178+
179+
To instantiate an asynchronous client, add the following code to the `main` method:
180+
145181
```java
146182
String endpoint = "https://<resource-name>.communication.azure.com";
147183
AzureKeyCredential azureKeyCredential = new AzureKeyCredential("<access-key>");
@@ -159,24 +195,37 @@ For simplicity, this quickstart uses connection strings, but in production envir
159195

160196
## Basic email sending
161197

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.
198+
An email message can be crafted using the `EmailMessage` object in the SDK.
163199

164200
```java
165201
EmailMessage message = new EmailMessage()
166202
.setSenderAddress("<[email protected]>")
167203
.setToRecipients("<[email protected]>")
168204
.setSubject("Welcome to Azure Communication Services Email")
169205
.setBodyPlainText("This email message is sent from Azure Communication Services Email using the Java SDK.");
206+
```
207+
208+
Make these replacements in the code:
209+
- Replace `<[email protected]>` with the email address you would like to send a message to.
210+
- Replace `<[email protected]>` with the MailFrom address of your verified domain.
211+
212+
To send the email message, call the `beginSend` function from the `EmailClient`.
213+
214+
## [Sync Client](#tab/sync-client)
170215

216+
Calling `beginSend` on the sync client 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. Sending an email is a long running operation, so calling `getFinalResult` on the poller returned by `beginSend` could potentially block the application for a long time. The recommended method is to do manual polling at an interval that's appropriate for your application needs as demonstrated in the sample below.
217+
218+
```java
171219
try
172220
{
173-
SyncPoller<EmailSendResult, EmailSendResult> poller = emailClient.beginSend(message, null);
221+
SyncPoller<EmailSendResult, EmailSendResult> poller = emailClient.beginSend(message, null); // This will send out the initial request to send an email
174222

175223
PollResponse<EmailSendResult> pollResponse = null;
176224

177225
Duration timeElapsed = Duration.ofSeconds(0);
178226
Duration POLLER_WAIT_TIME = Duration.ofSeconds(10);
179227

228+
// Polling is done manually to avoid blocking the application in case of an error
180229
while (pollResponse == null
181230
|| pollResponse.getStatus() == LongRunningOperationStatus.NOT_STARTED
182231
|| pollResponse.getStatus() == LongRunningOperationStatus.IN_PROGRESS)
@@ -208,10 +257,28 @@ catch (Exception exception)
208257
}
209258
```
210259

211-
Make these replacements in the code:
260+
## [Async Client](#tab/async-client)
212261

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.
262+
Calling `beginSend` on the async client 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.
263+
264+
```java
265+
// The initial request is sent out as soon as we subscribe the to PollerFlux object
266+
emailClient.beginSend(emailMessage).subscribe(
267+
response -> {
268+
if (response.getStatus() == LongRunningOperationStatus.SUCCESSFULLY_COMPLETED) {
269+
System.out.printf("Successfully sent the email (operation id: %s)", response.getValue().getId());
270+
}
271+
else {
272+
System.out.println("Email send status: " + response.getStatus());
273+
}
274+
},
275+
error -> {
276+
System.out.println("Error occurred while sending email: " + error.getMessage());
277+
}
278+
);
279+
```
280+
281+
---
215282

216283
### Run the code
217284

0 commit comments

Comments
 (0)