|
| 1 | +--- |
| 2 | +title: include file |
| 3 | +description: Manual polling .NET SDK include file |
| 4 | +author: natekimball-msft |
| 5 | +manager: koagbakp |
| 6 | +services: azure-communication-services |
| 7 | +ms.author: natekimball |
| 8 | +ms.date: 04/07/2023 |
| 9 | +ms.topic: include |
| 10 | +ms.service: azure-communication-services |
| 11 | +--- |
| 12 | + |
| 13 | +## Basic email sending |
| 14 | + |
| 15 | +### Construct your email message |
| 16 | + |
| 17 | +To send an email message, you need to: |
| 18 | +- Define the email subject and body. |
| 19 | +- Define your Sender Address. Construct your email message with your Sender information you get your MailFrom address from your verified domain. |
| 20 | +- Define the Recipient Address. |
| 21 | +- Call the SendAsync method. Add this code to the end of `Main` method in **Program.cs**: |
| 22 | + |
| 23 | +Replace with your domain details and modify the content, recipient details as required |
| 24 | +```csharp |
| 25 | + |
| 26 | +//Replace with your domain and modify the content, recipient details as required |
| 27 | +
|
| 28 | +var subject = "Welcome to Azure Communication Service Email APIs."; |
| 29 | +var htmlContent = "<html><body><h1>Quick send email test</h1><br/><h4>This email message is sent from Azure Communication Service Email.</h4><p>This mail was sent using .NET SDK!!</p></body></html>"; |
| 30 | + |
| 31 | +var recipient = "[email protected]"; |
| 32 | + |
| 33 | +``` |
| 34 | +### Send and get the email send status |
| 35 | + |
| 36 | +When you call SendAsync with Azure.WaitUntil.Started, your method returns back after starting the operation. The method returns EmailSendOperation object. You can call UpdateStatusAsync method to refresh the email operation status. |
| 37 | + |
| 38 | +The returned EmailSendOperation object contains an EmailSendStatus object that contains: |
| 39 | +- Current status of the Email Send operation. |
| 40 | +- An error object with failure details if the current status is in a failed state. |
| 41 | + |
| 42 | +```csharp |
| 43 | + |
| 44 | +/// Send the email message with WaitUntil.Started |
| 45 | +EmailSendOperation emailSendOperation = await emailClient.SendAsync( |
| 46 | + Azure.WaitUntil.Started, |
| 47 | + sender, |
| 48 | + recipient, |
| 49 | + subject, |
| 50 | + htmlContent); |
| 51 | + |
| 52 | +/// Call UpdateStatus on the email send operation to poll for the status |
| 53 | +/// manually. |
| 54 | +try |
| 55 | +{ |
| 56 | + while (true) |
| 57 | + { |
| 58 | + await emailSendOperation.UpdateStatusAsync(); |
| 59 | + if (emailSendOperation.HasCompleted) |
| 60 | + { |
| 61 | + break; |
| 62 | + } |
| 63 | + await Task.Delay(100); |
| 64 | + } |
| 65 | + |
| 66 | + if (emailSendOperation.HasValue) |
| 67 | + { |
| 68 | + Console.WriteLine($"Email queued for delivery. Status = {emailSendOperation.Value.Status}"); |
| 69 | + } |
| 70 | +} |
| 71 | +catch (RequestFailedException ex) |
| 72 | +{ |
| 73 | + Console.WriteLine($"Email send failed with Code = {ex.ErrorCode} and Message = {ex.Message}"); |
| 74 | +} |
| 75 | + |
| 76 | +/// Get the OperationId so that it can be used for tracking the message for troubleshooting |
| 77 | +string operationId = emailSendOperation.Id; |
| 78 | +Console.WriteLine($"Email operation id = {operationId}"); |
| 79 | +``` |
| 80 | + |
| 81 | +Run the application from your application directory with the `dotnet run` command. |
| 82 | + |
| 83 | +```console |
| 84 | +dotnet run |
| 85 | +``` |
| 86 | + |
| 87 | +### Sample code |
| 88 | + |
| 89 | +You can download the sample app from [GitHub](https://github.com/Azure-Samples/communication-services-dotnet-quickstarts/tree/main/SendEmailAdvanced/SendEmailWithManualPollingForStatus) |
0 commit comments