Skip to content

Commit 3e06ad6

Browse files
committed
Addressing PR feedback
1 parent eca28fc commit 3e06ad6

File tree

1 file changed

+58
-75
lines changed

1 file changed

+58
-75
lines changed

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

Lines changed: 58 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,12 @@ public class App
101101
}
102102
```
103103

104-
## [Sync Client](#tab/sync-client)
105-
106-
## Creating the email client with authentication
107-
104+
### Creating the email client with authentication
108105
There are a few different options available for authenticating an email client.
109106

110-
#### Connection String
107+
#### [Connection String](#tab/connection-string)
108+
109+
#### Sync Client
111110

112111
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.
113112

@@ -122,9 +121,26 @@ EmailClient emailClient = new EmailClientBuilder()
122121
.buildClient();
123122
```
124123

124+
#### Async Client
125+
126+
To authenticate a client, you instantiate an `EmailAsyncClient` 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.
127+
128+
To instantiate a client, add the following code to the `main` method:
129+
130+
```java
131+
// You can get your connection string from your resource in the Azure portal.
132+
String connectionString = "endpoint=https://<resource-name>.communication.azure.com/;accesskey=<access-key>";
133+
134+
EmailAsyncClient emailClient = new EmailClientBuilder()
135+
.connectionString(connectionString)
136+
.buildAsyncClient();
137+
```
138+
125139
<a name='azure-active-directory'></a>
126140

127-
#### Microsoft Entra ID
141+
#### [Microsoft Entra ID](#tab/entra-id)
142+
143+
#### Sync Client
128144

129145
A [DefaultAzureCredential](https://github.com/Azure/azure-sdk-for-java/tree/main/sdk/identity/azure-identity#defaultazurecredential) object must be passed to the `EmailClientBuilder` via the `credential()` method. An endpoint must also be set via the `endpoint()` method.
130146

@@ -139,8 +155,24 @@ EmailClient emailClient = new EmailClientBuilder()
139155
.buildClient();
140156
```
141157

142-
#### AzureKeyCredential
158+
#### Async Client
159+
160+
A [DefaultAzureCredential](https://github.com/Azure/azure-sdk-for-java/tree/main/sdk/identity/azure-identity#defaultazurecredential) object must be passed to the `EmailClientBuilder` via the `credential()` method. An endpoint must also be set via the `endpoint()` method.
161+
162+
The `AZURE_CLIENT_SECRET`, `AZURE_CLIENT_ID`, and `AZURE_TENANT_ID` environment variables are needed to create a `DefaultAzureCredential` object.
143163

164+
```java
165+
// You can find your endpoint and access key from your resource in the Azure portal
166+
String endpoint = "https://<resource-name>.communication.azure.com/";
167+
EmailAsyncClient emailClient = new EmailClientBuilder()
168+
.endpoint(endpoint)
169+
.credential(new DefaultAzureCredentialBuilder().build())
170+
.buildAsyncClient();
171+
```
172+
173+
#### [AzureKeyCredential](#tab/azure-key-credential)
174+
175+
#### Sync Client
144176
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/).
145177

146178
```java
@@ -152,7 +184,21 @@ EmailClient emailClient = new EmailClientBuilder()
152184
.buildClient();
153185
```
154186

187+
#### Async Client
188+
189+
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/).
190+
191+
```java
192+
String endpoint = "https://<resource-name>.communication.azure.com";
193+
AzureKeyCredential azureKeyCredential = new AzureKeyCredential("<access-key>");
194+
EmailAsyncClient emailClient = new EmailClientBuilder()
195+
.endpoint(endpoint)
196+
.credential(azureKeyCredential)
197+
.buildAsyncClient();
198+
```
199+
155200
For simplicity, this quickstart uses connection strings, but in production environments, we recommend using [service principals](../../../quickstarts/identity/service-principal.md).
201+
---
156202

157203
## Basic email sending
158204

@@ -172,18 +218,21 @@ Make these replacements in the code:
172218

173219
To send the email message, call the `beginSend` function from the `EmailClient`.
174220

221+
## [Sync Client](#tab/sync-client)
222+
175223
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.
176224

177225
```java
178226
try
179227
{
180-
SyncPoller<EmailSendResult, EmailSendResult> poller = emailClient.beginSend(message, null);
228+
SyncPoller<EmailSendResult, EmailSendResult> poller = emailClient.beginSend(message, null); // This will send out the initial request to send an email
181229

182230
PollResponse<EmailSendResult> pollResponse = null;
183231

184232
Duration timeElapsed = Duration.ofSeconds(0);
185233
Duration POLLER_WAIT_TIME = Duration.ofSeconds(10);
186234

235+
// Polling is done manually to avoid blocking the application in case of an error
187236
while (pollResponse == null
188237
|| pollResponse.getStatus() == LongRunningOperationStatus.NOT_STARTED
189238
|| pollResponse.getStatus() == LongRunningOperationStatus.IN_PROGRESS)
@@ -217,78 +266,12 @@ catch (Exception exception)
217266

218267
## [Async Client](#tab/async-client)
219268

220-
## Creating the email client with authentication
221-
222-
The Azure SDK for Java also contains non-blocking, asynchronous APIs for interacting with Azure services.
223-
224-
#### Connection String
225-
226-
To authenticate a client, you instantiate an `EmailAsyncClient` 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.
227-
228-
To instantiate a client, add the following code to the `main` method:
229-
230-
```java
231-
// You can get your connection string from your resource in the Azure portal.
232-
String connectionString = "endpoint=https://<resource-name>.communication.azure.com/;accesskey=<access-key>";
233-
234-
EmailAsyncClient emailClient = new EmailClientBuilder()
235-
.connectionString(connectionString)
236-
.buildAsyncClient();
237-
```
238-
239-
<a name='azure-active-directory'></a>
240-
241-
#### Microsoft Entra ID
242-
243-
A [DefaultAzureCredential](https://github.com/Azure/azure-sdk-for-java/tree/main/sdk/identity/azure-identity#defaultazurecredential) object must be passed to the `EmailClientBuilder` via the `credential()` method. An endpoint must also be set via the `endpoint()` method.
244-
245-
The `AZURE_CLIENT_SECRET`, `AZURE_CLIENT_ID`, and `AZURE_TENANT_ID` environment variables are needed to create a `DefaultAzureCredential` object.
246-
247-
```java
248-
// You can find your endpoint and access key from your resource in the Azure portal
249-
String endpoint = "https://<resource-name>.communication.azure.com/";
250-
EmailAsyncClient emailClient = new EmailClientBuilder()
251-
.endpoint(endpoint)
252-
.credential(new DefaultAzureCredentialBuilder().build())
253-
.buildAsyncClient();
254-
```
255-
256-
#### AzureKeyCredential
257-
258-
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/).
259-
260-
```java
261-
String endpoint = "https://<resource-name>.communication.azure.com";
262-
AzureKeyCredential azureKeyCredential = new AzureKeyCredential("<access-key>");
263-
EmailAsyncClient emailClient = new EmailClientBuilder()
264-
.endpoint(endpoint)
265-
.credential(azureKeyCredential)
266-
.buildAsyncClient();
267-
```
268-
269-
For simplicity, this quickstart uses connection strings, but in production environments, we recommend using [service principals](../../../quickstarts/identity/service-principal.md).
270-
271269
## Basic email sending
272270

273-
An email message can be crafted using the `EmailMessage` object in the SDK.
274-
275-
```java
276-
EmailMessage message = new EmailMessage()
277-
.setSenderAddress("<[email protected]>")
278-
.setToRecipients("<[email protected]>")
279-
.setSubject("Welcome to Azure Communication Services Email")
280-
.setBodyPlainText("This email message is sent from Azure Communication Services Email using the Java SDK.");
281-
```
282-
283-
Make these replacements in the code:
284-
- Replace `<[email protected]>` with the email address you would like to send a message to.
285-
- Replace `<[email protected]>` with the MailFrom address of your verified domain.
286-
287-
To send the email message, call the `beginSend` function from the `EmailAsyncClient`.
288-
289271
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.
290272

291273
```java
274+
// The initial request is sent out as soon as we subscribe the to PollerFlux object
292275
emailClient.beginSend(emailMessage).subscribe(
293276
response -> {
294277
if (response.getStatus() == LongRunningOperationStatus.SUCCESSFULLY_COMPLETED) {

0 commit comments

Comments
 (0)