Skip to content

Commit 61ebb49

Browse files
committed
Adding Sync and Async quickstarts in seperate tabs
1 parent 49a5271 commit 61ebb49

File tree

1 file changed

+79
-27
lines changed

1 file changed

+79
-27
lines changed

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

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

104+
## [Sync Client](#tab/sync-client)
105+
104106
## Creating the email client with authentication
105107

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

108-
#### [Connection String](#tab/connection-string)
110+
#### Connection String
109111

110112
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.
111113

@@ -122,7 +124,7 @@ EmailClient emailClient = new EmailClientBuilder()
122124

123125
<a name='azure-active-directory'></a>
124126

125-
#### [Microsoft Entra ID](#tab/aad)
127+
#### Microsoft Entra ID
126128

127129
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.
128130

@@ -137,7 +139,7 @@ EmailClient emailClient = new EmailClientBuilder()
137139
.buildClient();
138140
```
139141

140-
#### [AzureKeyCredential](#tab/azurekeycredential)
142+
#### AzureKeyCredential
141143

142144
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/).
143145

@@ -150,24 +152,8 @@ EmailClient emailClient = new EmailClientBuilder()
150152
.buildClient();
151153
```
152154

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

157-
#### Creating async client
158-
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-
```
170-
171157
## Basic email sending
172158

173159
An email message can be crafted using the `EmailMessage` object in the SDK.
@@ -184,11 +170,9 @@ Make these replacements in the code:
184170
- Replace `<[email protected]>` with the email address you would like to send a message to.
185171
- Replace `<[email protected]>` with the MailFrom address of your verified domain.
186172

187-
To send the email message, call the `beginSend` function from the `EmailClient`. This method is present in both the sync and async client.
173+
To send the email message, call the `beginSend` function from the `EmailClient`.
188174

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.
175+
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.
192176

193177
```java
194178
try
@@ -231,12 +215,79 @@ catch (Exception exception)
231215
}
232216
```
233217

234-
#### Email sending with the async client
218+
## [Async Client](#tab/async-client)
219+
220+
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).
221+
222+
#### Connection String
223+
224+
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.
225+
226+
To instantiate a client, add the following code to the `main` method:
227+
228+
```java
229+
// You can get your connection string from your resource in the Azure portal.
230+
String connectionString = "endpoint=https://<resource-name>.communication.azure.com/;accesskey=<access-key>";
231+
232+
EmailAsyncClient emailClient = new EmailClientBuilder()
233+
.connectionString(connectionString)
234+
.buildAsyncClient();
235+
```
236+
237+
<a name='azure-active-directory'></a>
238+
239+
#### Microsoft Entra ID
240+
241+
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.
235242

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.
243+
The `AZURE_CLIENT_SECRET`, `AZURE_CLIENT_ID`, and `AZURE_TENANT_ID` environment variables are needed to create a `DefaultAzureCredential` object.
237244

238245
```java
239-
emailAsyncClient.beginSend(emailMessage).subscribe(
246+
// You can find your endpoint and access key from your resource in the Azure portal
247+
String endpoint = "https://<resource-name>.communication.azure.com/";
248+
EmailAsyncClient emailClient = new EmailClientBuilder()
249+
.endpoint(endpoint)
250+
.credential(new DefaultAzureCredentialBuilder().build())
251+
.buildAsyncClient();
252+
```
253+
254+
#### AzureKeyCredential
255+
256+
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/).
257+
258+
```java
259+
String endpoint = "https://<resource-name>.communication.azure.com";
260+
AzureKeyCredential azureKeyCredential = new AzureKeyCredential("<access-key>");
261+
EmailAsyncClient emailClient = new EmailClientBuilder()
262+
.endpoint(endpoint)
263+
.credential(azureKeyCredential)
264+
.buildAsyncClient();
265+
```
266+
267+
For simplicity, this quickstart uses connection strings, but in production environments, we recommend using [service principals](../../../quickstarts/identity/service-principal.md).
268+
269+
## Basic email sending
270+
271+
An email message can be crafted using the `EmailMessage` object in the SDK.
272+
273+
```java
274+
EmailMessage message = new EmailMessage()
275+
.setSenderAddress("<[email protected]>")
276+
.setToRecipients("<[email protected]>")
277+
.setSubject("Welcome to Azure Communication Services Email")
278+
.setBodyPlainText("This email message is sent from Azure Communication Services Email using the Java SDK.");
279+
```
280+
281+
Make these replacements in the code:
282+
- Replace `<[email protected]>` with the email address you would like to send a message to.
283+
- Replace `<[email protected]>` with the MailFrom address of your verified domain.
284+
285+
To send the email message, call the `beginSend` function from the `EmailAsyncClient`.
286+
287+
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.
288+
289+
```java
290+
emailClient.beginSend(emailMessage).subscribe(
240291
response -> {
241292
if (response.getStatus() == LongRunningOperationStatus.SUCCESSFULLY_COMPLETED) {
242293
System.out.printf("Successfully sent the email (operation id: %s)", response.getValue().getId());
@@ -250,6 +301,7 @@ emailAsyncClient.beginSend(emailMessage).subscribe(
250301
}
251302
);
252303
```
304+
---
253305

254306
### Run the code
255307

0 commit comments

Comments
 (0)