You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: articles/communication-services/quickstarts/email/includes/send-email-java.md
+58-75Lines changed: 58 additions & 75 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -101,13 +101,12 @@ public class App
101
101
}
102
102
```
103
103
104
-
## [Sync Client](#tab/sync-client)
105
-
106
-
## Creating the email client with authentication
107
-
104
+
### Creating the email client with authentication
108
105
There are a few different options available for authenticating an email client.
109
106
110
-
#### Connection String
107
+
#### [Connection String](#tab/connection-string)
108
+
109
+
#### Sync Client
111
110
112
111
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.
113
112
@@ -122,9 +121,26 @@ EmailClient emailClient = new EmailClientBuilder()
122
121
.buildClient();
123
122
```
124
123
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.
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.
130
146
@@ -139,8 +155,24 @@ EmailClient emailClient = new EmailClientBuilder()
139
155
.buildClient();
140
156
```
141
157
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.
143
163
164
+
```java
165
+
// You can find your endpoint and access key from your resource in the Azure portal
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/).
145
177
146
178
```java
@@ -152,7 +184,21 @@ EmailClient emailClient = new EmailClientBuilder()
152
184
.buildClient();
153
185
```
154
186
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/).
For simplicity, this quickstart uses connection strings, but in production environments, we recommend using [service principals](../../../quickstarts/identity/service-principal.md).
201
+
---
156
202
157
203
## Basic email sending
158
204
@@ -172,18 +218,21 @@ Make these replacements in the code:
172
218
173
219
To send the email message, call the `beginSend` function from the `EmailClient`.
174
220
221
+
## [Sync Client](#tab/sync-client)
222
+
175
223
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.
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.
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
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/).
For simplicity, this quickstart uses connection strings, but in production environments, we recommend using [service principals](../../../quickstarts/identity/service-principal.md).
270
-
271
269
## Basic email sending
272
270
273
-
An email message can be crafted using the `EmailMessage` object in the SDK.
.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
-
289
271
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.
290
272
291
273
```java
274
+
// The initial request is sent out as soon as we subscribe the to PollerFlux object
292
275
emailClient.beginSend(emailMessage).subscribe(
293
276
response -> {
294
277
if (response.getStatus() ==LongRunningOperationStatus.SUCCESSFULLY_COMPLETED) {
0 commit comments