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
+79-27Lines changed: 79 additions & 27 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -101,11 +101,13 @@ public class App
101
101
}
102
102
```
103
103
104
+
## [Sync Client](#tab/sync-client)
105
+
104
106
## Creating the email client with authentication
105
107
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.
107
109
108
-
#### [Connection String](#tab/connection-string)
110
+
#### Connection String
109
111
110
112
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.
111
113
@@ -122,7 +124,7 @@ EmailClient emailClient = new EmailClientBuilder()
122
124
123
125
<aname='azure-active-directory'></a>
124
126
125
-
#### [Microsoft Entra ID](#tab/aad)
127
+
#### Microsoft Entra ID
126
128
127
129
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.
128
130
@@ -137,7 +139,7 @@ EmailClient emailClient = new EmailClientBuilder()
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/).
143
145
@@ -150,24 +152,8 @@ EmailClient emailClient = new EmailClientBuilder()
150
152
.buildClient();
151
153
```
152
154
153
-
---
154
-
155
155
For simplicity, this quickstart uses connection strings, but in production environments, we recommend using [service principals](../../../quickstarts/identity/service-principal.md).
156
156
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:
An email message can be crafted using the `EmailMessage` object in the SDK.
@@ -184,11 +170,9 @@ Make these replacements in the code:
184
170
- Replace `<[email protected]>` with the email address you would like to send a message to.
185
171
- Replace `<[email protected]>` with the MailFrom address of your verified domain.
186
172
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`.
188
174
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.
192
176
193
177
```java
194
178
try
@@ -231,12 +215,79 @@ catch (Exception exception)
231
215
}
232
216
```
233
217
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.
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.
235
242
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.
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).
268
+
269
+
## Basic email sending
270
+
271
+
An email message can be crafted using the `EmailMessage` object in the SDK.
.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(
240
291
response -> {
241
292
if (response.getStatus() ==LongRunningOperationStatus.SUCCESSFULLY_COMPLETED) {
242
293
System.out.printf("Successfully sent the email (operation id: %s)", response.getValue().getId());
0 commit comments