Skip to content

Commit

Permalink
Merge pull request #173 from apattath/main
Browse files Browse the repository at this point in the history
Add a SendEmail sample demonstrating how to rehydrate an EmailSendOperation using an existing OperationID
  • Loading branch information
kagbakpem authored Sep 20, 2024
2 parents 1c34668 + 958fd16 commit 4b517d3
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 1 deletion.
2 changes: 2 additions & 0 deletions SendEmailAdvanced/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ Additional documentation for this sample can be found on [Microsoft Docs](https:
- ./SendEmailWithManualPollingForStatus/Program.cs: Entry point for sending emails and manually poll for the email send status.
- ./SendHighImportanceEmailToMultipleRecipients/Program.cs: Entry point for sending high importance email to multiple recipients.
- ./SendEmailWithManagedIdentity/SendEmailWithManagedIdentityFunction.cs: Entry point for sending an email using a function app with a managed identity.
- ./SendBulkEmailWithOptionalThrottlingPolicy/Program.cs: Entry point for sending bulk emails with optional throttling policy.
- ./SendEmailWithManualPollingUsingOperationId/Program.cs: Entry point for sending an email and polling for status using operationID to rehydrated email send operation.

## Before running the sample for the first time

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System;
using System.Threading.Tasks;

namespace SendEmailPlainText
namespace SendEmailWithManualPollingForStatus
{
class Program
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using Azure;
using Azure.Communication.Email;
using System;
using System.Threading.Tasks;

namespace SendEmailWithManualPollingUsingOperationId
{
class Program
{
static async Task Main(string[] args)
{
// This code demonstrates how to send email using Azure Communication Services.
var connectionString = "<ACS_CONNECTION_STRING>";
var emailClient = new EmailClient(connectionString);

var sender = "<SENDER_EMAIL>";
var recipient = "<RECIPIENT_EMAIL>";
var subject = "Send email with manual status polling using operationID";

var emailContent = new EmailContent(subject)
{
PlainText = "This is plain text mail send test body \n Best Wishes!!",
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>"
};

var emailMessage = new EmailMessage(sender, recipient, emailContent);

var emailSendOperation = await emailClient.SendAsync(
wait: WaitUntil.Started,
message: emailMessage);

/// Get the OperationId so that it can be used for rehydrating an EmailSendOperation object
/// and use that object to poll for the status of the email send operation.
var operationId = emailSendOperation.Id;
Console.WriteLine($"Email operation id = {operationId}");

/// Do a bunch of other things here...

/// Poll for the status of the email send operation using the previous operationId
await PollForEmailSendOperationStatusWithExistingOperationId(emailClient, operationId);
}

private static async Task PollForEmailSendOperationStatusWithExistingOperationId(EmailClient emailClient, string operationId)
{
/// Rehydrate a new EmailSendOperation object using the given operationId
/// Rehydration refers to the process of creating a new EmailSendOperation object using the operation ID from a previous EmailSendOperation.
/// This is necessary in case you want to continue monitoring the status of the email manually, when you don't have
/// the original EmailSendOperation object from the initial request.
EmailSendOperation rehydratedEmailSendOperation = new EmailSendOperation(operationId, emailClient);

/// Call UpdateStatus on the rehydrated email send operation to poll for the status manually.
try
{
while (true)
{
await rehydratedEmailSendOperation.UpdateStatusAsync();
if (rehydratedEmailSendOperation.HasCompleted)
{
break;
}
await Task.Delay(100);
}

if (rehydratedEmailSendOperation.HasValue)
{
Console.WriteLine($"Email queued for delivery. Status = {rehydratedEmailSendOperation.Value.Status}");
}
}
catch (RequestFailedException ex)
{
Console.WriteLine($"Email send failed with Code = {ex.ErrorCode} and Message = {ex.Message}");
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<PackageReference Include="Azure.Communication.Email" Version="1.0.1" />
</ItemGroup>
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<RootNamespace>SendEmail</RootNamespace>
<AssemblyName>SendEmailWithManualPollingUsingOperationId</AssemblyName>
</PropertyGroup>

</Project>
6 changes: 6 additions & 0 deletions SendEmailAdvanced/SendMailAdvanced.sln
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SendBulkEmailWithOptionalTh
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SendEmailWithInlineAttachments", "SendEmailWithInlineAttachments\SendEmailWithInlineAttachments.csproj", "{C6AF39F9-1B10-4D23-80E1-4DBB4670C7F0}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SendEmailWithManualPollingUsingOperationId", "SendEmailWithManualPollingUsingOperationId\SendEmailWithManualPollingUsingOperationId.csproj", "{DCBD2EF5-2364-483F-99F9-300238B58291}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -56,6 +58,10 @@ Global
{C6AF39F9-1B10-4D23-80E1-4DBB4670C7F0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C6AF39F9-1B10-4D23-80E1-4DBB4670C7F0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C6AF39F9-1B10-4D23-80E1-4DBB4670C7F0}.Release|Any CPU.Build.0 = Release|Any CPU
{DCBD2EF5-2364-483F-99F9-300238B58291}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DCBD2EF5-2364-483F-99F9-300238B58291}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DCBD2EF5-2364-483F-99F9-300238B58291}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DCBD2EF5-2364-483F-99F9-300238B58291}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down

0 comments on commit 4b517d3

Please sign in to comment.