|
| 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). |
0 commit comments