Skip to content

Commit af7f1b1

Browse files
authored
Merge pull request #303823 from fanruisun/hydratepoller
Add hydrate message id quickstart
2 parents 173327a + 9293558 commit af7f1b1

4 files changed

Lines changed: 225 additions & 0 deletions

File tree

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
---
2+
title: Hydrating messageId using EmailClient
3+
titleSuffix: An Azure Communication Services Quickstart
4+
description: This article describes how to hydrate messageId using EmailClient with Azure Communication Services.
5+
author: fanruisun
6+
manager: koagbakp
7+
services: azure-communication-services
8+
ms.author: fanruisun
9+
ms.date: 08/04/2025
10+
ms.topic: quickstart
11+
ms.service: azure-communication-services
12+
ms.custom: devx-track-dotnet, devx-track-extended-java
13+
zone_pivot_groups: acs-csharp-java
14+
---
15+
16+
# Hydrating messageId using EmailClient
17+
18+
This article describes how to hydrate messageId using EmailClient with our Email SDKs.
19+
20+
::: zone pivot="programming-language-csharp"
21+
[!INCLUDE [prepend-net](./includes/prepend-net.md)]
22+
[!INCLUDE [hydrate-message-id-net](./includes/hydrate-email-client-with-message-id-dotnet.md)]
23+
::: zone-end
24+
25+
::: zone pivot="programming-language-java"
26+
[!INCLUDE [prepend-java](./includes/prepend-java.md)]
27+
[!INCLUDE [hydrate-message-id-java](./includes/hydrate-email-client-with-message-id-java.md)]
28+
::: zone-end
29+
30+
### Email delivery
31+
32+
To troubleshoot issues related to email delivery, you can [get status of the email delivery](../handle-email-events.md) to capture delivery details.
33+
34+
> [!IMPORTANT]
35+
> The success result returned by polling for the status of the send operation only validates that the email is sent out for delivery. For more information about the status of the delivery on the recipient end, see [how to handle email events](../handle-email-events.md).
36+
37+
### Email throttling
38+
39+
If your application is hanging, it could be due to email sending being throttled. You can [handle email throttling by logging or by implementing a custom policy](../send-email-advanced/throw-exception-when-tier-limit-reached.md).
40+
41+
> [!NOTE]
42+
> This sandbox is intended to help developers start building the application. You can gradually request to increase the sending volume once the application is ready to go live. Submit a support request to raise your desired sending limit if you require sending a volume of messages exceeding the rate limits.
43+
44+
## Clean up Azure Communication Service resources
45+
46+
If you want to clean up and remove a Communication Services subscription, you can delete the resource or resource group. Deleting the resource group also deletes any other resources associated with it. Learn more about [cleaning up resources](../../create-communication-resource.md#clean-up-resources).
47+
48+
## Next steps
49+
50+
- Learn how to [send email to multiple recipients](./send-email-to-multiple-recipients.md)
51+
- Learn more about [sending email with attachments](./send-email-with-attachments.md)
52+
- Familiarize yourself with [email client library](../../../concepts/email/sdk-features.md)
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
---
2+
title: include file
3+
description: Hydrating messageId using EmailClient for .NET SDK
4+
author: fanruisun
5+
manager: koagbakp
6+
services: azure-communication-services
7+
ms.author: fanruisun
8+
ms.date: 08/04/2025
9+
ms.topic: include
10+
ms.service: azure-communication-services
11+
---
12+
13+
## Hydrating messageId using EmailClient
14+
15+
You can hydrate a messageId (operationId) using the EmailClient to track and manage email operations more effectively. This process is called "rehydration" and allows you to continue monitoring the status of an email when you don't have the original EmailSendOperation object.
16+
17+
### Create the email client
18+
19+
```csharp
20+
// Create the EmailClient
21+
var connectionString = "<ACS_CONNECTION_STRING>";
22+
var emailClient = new EmailClient(connectionString);
23+
```
24+
25+
### Configure email details
26+
27+
```csharp
28+
var sender = "<SENDER_EMAIL>";
29+
var recipient = "<RECIPIENT_EMAIL>";
30+
var subject = "Send email with manual status polling using operationID";
31+
```
32+
33+
### Create email content
34+
35+
```csharp
36+
var emailContent = new EmailContent(subject)
37+
{
38+
PlainText = "This is plain text mail send test body \n Best Wishes!!",
39+
Html = "<html><body><h1>Quick send email test</h1><br/><h4>Communication email as a service mail send app working properly</h4><p>Happy Learning!!</p></body></html>"
40+
};
41+
42+
var emailMessage = new EmailMessage(sender, recipient, emailContent);
43+
```
44+
45+
### Send email and capture operation ID
46+
47+
```csharp
48+
var emailSendOperation = await emailClient.SendAsync(
49+
wait: WaitUntil.Started,
50+
message: emailMessage);
51+
52+
// Get the OperationId so that it can be used for rehydrating an EmailSendOperation object
53+
// and use that object to poll for the status of the email send operation.
54+
var operationId = emailSendOperation.Id;
55+
Console.WriteLine($"Email operation id = {operationId}");
56+
```
57+
58+
The `operationId` is the key identifier that allows you to rehydrate the operation later. In real applications, you would typically store this ID for future reference.
59+
60+
### Rehydrate operation ID and poll for completion
61+
62+
```csharp
63+
// Poll for the status of the email send operation using the previous operationId
64+
await PollForEmailSendOperationStatusWithExistingOperationId(emailClient, operationId);
65+
66+
private static async Task PollForEmailSendOperationStatusWithExistingOperationId(EmailClient emailClient, string operationId)
67+
{
68+
// Rehydrate a new EmailSendOperation object using the given operationId
69+
// Rehydration refers to the process of creating a new EmailSendOperation object using the operation ID from a previous EmailSendOperation.
70+
// This is necessary in case you want to continue monitoring the status of the email manually, when you don't have
71+
// the original EmailSendOperation object from the initial request.
72+
EmailSendOperation rehydratedEmailSendOperation = new EmailSendOperation(operationId, emailClient);
73+
74+
// Call UpdateStatus on the rehydrated email send operation to poll for the status manually.
75+
try
76+
{
77+
while (true)
78+
{
79+
await rehydratedEmailSendOperation.UpdateStatusAsync();
80+
if (rehydratedEmailSendOperation.HasCompleted)
81+
{
82+
break;
83+
}
84+
await Task.Delay(100);
85+
}
86+
87+
if (rehydratedEmailSendOperation.HasValue)
88+
{
89+
Console.WriteLine($"Email queued for delivery. Status = {rehydratedEmailSendOperation.Value.Status}");
90+
}
91+
}
92+
catch (RequestFailedException ex)
93+
{
94+
Console.WriteLine($"Email send failed with Code = {ex.ErrorCode} and Message = {ex.Message}");
95+
}
96+
}
97+
```
98+
99+
### Sample code
100+
101+
You can download the sample app demonstrating this action from GitHub Azure Samples [Send Email With Manual Polling Using Operation Id](https://github.com/Azure-Samples/communication-services-dotnet-quickstarts/tree/main/SendEmailAdvanced/SendEmailWithManualPollingUsingOperationId).
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
---
2+
title: include file
3+
description: Hydrate messageId using EmailClient for Java
4+
author: fanruisun
5+
manager: koagbakp
6+
services: azure-communication-services
7+
ms.author: fanruisun
8+
ms.date: 08/04/2025
9+
ms.topic: include
10+
ms.service: azure-communication-services
11+
---
12+
13+
## Hydrate messageId using EmailClient for Java
14+
15+
Use the Azure Communication Services Email SDK for Java. Add the following dependency to your `pom.xml` file:
16+
17+
18+
### Create an email message
19+
20+
```java
21+
// Create an email message with both plain text and HTML content
22+
EmailMessage message = new EmailMessage()
23+
.setSenderAddress(senderAddress)
24+
.setToRecipients(recipientAddress)
25+
.setSubject("Test email from Java Sample")
26+
.setBodyPlainText("This is plaintext body of test email.")
27+
.setBodyHtml("<html><h1>This is the html body of test email.</h1></html>");
28+
```
29+
30+
### Send email and capture operation ID
31+
32+
```java
33+
// STEP 1: Send the email and get the initial poller
34+
// This starts the email send operation and returns a poller to monitor progress
35+
SyncPoller<EmailSendResult, EmailSendResult> poller = client.beginSend(message);
36+
37+
// Poll once to get the initial response and extract the operation ID
38+
PollResponse<EmailSendResult> response = poller.poll();
39+
String operationId = response.getValue().getId();
40+
System.out.printf("Sent email send request from first poller (operation id: %s)\n", operationId);
41+
```
42+
43+
The `operationId` is the key identifier that allows you to rehydrate the poller later. In real applications, you would typically store this ID in a database for future reference.
44+
45+
### Rehydrate the poller using operation ID
46+
47+
```java
48+
// STEP 2: Demonstrate rehydration using the operation ID
49+
// In a real scenario, you might store this operationId in a database
50+
// and retrieve it later to continue monitoring the operation
51+
System.out.print("Started polling from second poller\n");
52+
53+
// REHYDRATION: Create a new poller using the existing operationId
54+
// This is the key concept - you can recreate a poller from just the operationId
55+
SyncPoller<EmailSendResult, EmailSendResult> poller2 = client.beginSend(operationId);
56+
```
57+
58+
### Poll for completion and get results
59+
60+
```java
61+
// Wait for the email operation to complete using the rehydrated poller
62+
PollResponse<EmailSendResult> response2 = poller2.waitForCompletion();
63+
64+
// Display the final result
65+
System.out.printf("Successfully sent the email (operation id: %s)\n", poller2.getFinalResult().getId());
66+
```
67+
68+
### Sample code
69+
70+
You can download the sample app demonstrating this action from GitHub Azure Samples [Send Email Rehydrate Poller for Java](https://github.com/Azure-Samples/communication-services-java-quickstarts/tree/main/send-email-advanced/send-email-rehydrate-poller).

articles/communication-services/toc.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,8 @@ items:
240240
href: quickstarts/email/send-email-advanced/throw-exception-when-tier-limit-reached.md
241241
- name: Use email object model for payload
242242
href: quickstarts/email/send-email-advanced/use-email-object-model-for-payload.md
243+
- name: Hydrating messageId using EmailClient
244+
href: quickstarts/email/send-email-advanced/hydrate-email-client-with-message-id.md
243245
- name: Manage sender reputation
244246
items:
245247
- name: Understand sender reputation

0 commit comments

Comments
 (0)