Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Azure Communication Services (ACS) Email .NET SDK (Public Preview 2 before GA) #33110

Merged
merged 52 commits into from
Mar 1, 2023
Merged
Show file tree
Hide file tree
Changes from 51 commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
b1a164f
AutoGenerated SDK changes from REST API
apattath Dec 13, 2022
97612dc
Update version in EmailClientOptions
apattath Dec 13, 2022
ad78a3f
Add generated SDK after another round of swagger changes
apattath Dec 16, 2022
b9303ea
Add convenience overloads for EmailClient.Send
apattath Dec 16, 2022
caa80f0
Fix failing unit tests
apattath Dec 16, 2022
525299a
Add overload for EmailAttachment constructor that takes BinaryData
apattath Dec 16, 2022
d8eca6e
Add overload for EmailAttachment that takes BinaryData as parameter
apattath Dec 17, 2022
4c0148d
Restore global.json
apattath Dec 17, 2022
7ae49e7
Merge branch 'Azure:main' into master
apattath Dec 17, 2022
cef7b29
Merge branch 'Azure:main' into master
apattath Jan 9, 2023
ae92bb5
update SDK with long running operation
apattath Jan 9, 2023
56c6caa
revert global.json
apattath Jan 9, 2023
4633d88
Merge branch 'Azure:main' into master
apattath Jan 10, 2023
7f37eef
Merge branch 'master' of https://github.com/apattath/azure-sdk-for-ne…
apattath Jan 10, 2023
d646812
fix build failures
apattath Jan 10, 2023
2ec61c8
Update SDK to include changes due to LRO patternn and fix issues that…
apattath Jan 12, 2023
10e695a
Remove default value of null for displayName in EmailAddress constructor
apattath Feb 1, 2023
4ff2279
Regenerated SDK after swagger approved by ARB
apattath Feb 11, 2023
5b54ca3
fix tests and samples
apattath Feb 14, 2023
3205c7c
fix failing unit tests by removing the generatd StartSend method
apattath Feb 15, 2023
038e4e9
rengerate SDK with updated swagger after ARB feedback
apattath Feb 17, 2023
2748928
stash changes
apattath Feb 17, 2023
4a8c8f1
Implement LRO pattern SDK guidelines
apattath Feb 18, 2023
24da045
Removing Models namespace from hand-rolled models as well
apattath Feb 18, 2023
e006f55
Hide the Id property in EmailSendResult
apattath Feb 18, 2023
e136afc
Add more unit tests for new scenarios
apattath Feb 20, 2023
ca09e84
update property ContentBytesBase64 to ContentInBase64
apattath Feb 21, 2023
9a1d4e7
Update samples
apattath Feb 21, 2023
88301a5
Merge remote-tracking branch 'upstream/main' into master
apattath Feb 21, 2023
4fcf462
fix errors from merge
apattath Feb 21, 2023
debee37
update public API and snippets
apattath Feb 22, 2023
b3683c4
regenerate SDK after merge from remote
apattath Feb 22, 2023
5116a10
Convert EmailAddress to struct
apattath Feb 22, 2023
bee7425
Convert Attachment content to base64 only at the end before sending o…
apattath Feb 22, 2023
2616b4c
Fix failing unit tests
apattath Feb 22, 2023
505b1f6
regenerate public API
apattath Feb 22, 2023
ad0988d
Rename DisableUserEngagementTracking proprety
apattath Feb 23, 2023
4461fe4
Update Public API surface
apattath Feb 23, 2023
5c79d0c
update EmailMessage constructor to reorder parameters and fix tests
apattath Feb 23, 2023
29255dd
Update recorded tests
apattath Feb 23, 2023
0dd4411
Fix build errors and override EmailsendResult.Serialization.cs
apattath Feb 23, 2023
d1b57f6
update recorded tests with sanitized urls
apattath Feb 24, 2023
6d9b506
Update recorded test that was timing out.
apattath Feb 25, 2023
53c51c9
Address feedback - customize EmailModelFacory, split sample md files …
apattath Feb 27, 2023
6a6fa15
fix broken links and add raw github file link in autorest.md
apattath Feb 28, 2023
949d725
Merge branch 'Azure:main' into master
apattath Feb 28, 2023
33aa7bb
remove codegenclient and codegensuppresstype
apattath Feb 28, 2023
7b6b389
Merge branch 'master' of https://github.com/apattath/azure-sdk-for-ne…
apattath Feb 28, 2023
0a19d9e
add asp.net extensions for EmailClient
apattath Feb 28, 2023
0793c0d
actually adding the file for asp.net extensions for EmailClient
apattath Feb 28, 2023
c244c81
Add missing asp.net extension and rename RestClient
apattath Mar 1, 2023
331e52a
update public APIs
apattath Mar 1, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
159 changes: 103 additions & 56 deletions sdk/communication/Azure.Communication.Email/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,14 @@ To create these resource, you can use the [Azure Portal][communication_resource_
### Using statements
```C# Snippet:Azure_Communication_Email_UsingStatements
using Azure.Communication.Email;
using Azure.Communication.Email.Models;
```

### Authenticate the client
Email clients can be authenticated using the connection string acquired from an Azure Communication Resource in the [Azure Portal][azure_portal].

```C# Snippet:Azure_Communication_Email_CreateEmailClient
var connectionString = "<connection_string>"; // Find your Communication Services resource in the Azure portal
EmailClient client = new EmailClient(connectionString);
EmailClient emailClient = new EmailClient(connectionString);
```

Alternatively, Email clients can also be authenticated using a valid token credential. With this option,
Expand All @@ -41,119 +40,168 @@ Alternatively, Email clients can also be authenticated using a valid token crede
string endpoint = "<endpoint_url>";
TokenCredential tokenCredential = new DefaultAzureCredential();
tokenCredential = new DefaultAzureCredential();
EmailClient client = new EmailClient(new Uri(endpoint), tokenCredential);
EmailClient emailClient = new EmailClient(new Uri(endpoint), tokenCredential);
```
## Examples
### Send an Email Message
To send an email message, call the `Send` or `SendAsync` function from the `EmailClient`.
```C# Snippet:Azure_Communication_Email_Send
// Create the email content
var emailContent = new EmailContent("This is the subject");
emailContent.PlainText = "This is the body";
### Send a simple email message with automatic polling for status
To send an email message, call the simple overload of `Send` or `SendAsync` function from the `EmailClient`.
```C# Snippet:Azure_Communication_Email_Send_Simple_AutoPolling
var emailSendOperation = emailClient.Send(
wait: WaitUntil.Completed,
from: "<Send email address>" // The email address of the domain registered with the Communication Services resource
to: "<recipient email address>"
subject: "This is the subject",
htmlContent: "<html><body>This is the html body</body></html>");
Console.WriteLine($"Email Sent. Status = {emailSendOperation.Value.Status}");

/// Get the OperationId so that it can be used for tracking the message for troubleshooting
string operationId = emailSendOperation.Id;
Console.WriteLine($"Email operation id = {operationId}");
```

// Create the recipient list
var emailRecipients = new EmailRecipients(
new List<EmailAddress>
### Send a simple email message with manual polling for status
To send an email message, call the simple overload of `Send` or `SendAsync` function from the `EmailClient`.
```C# Snippet:Azure_Communication_Email_Send_Simple_ManualPolling_Async
/// Send the email message with WaitUntil.Started
var emailSendOperation = await emailClient.SendAsync(
wait: WaitUntil.Started,
from: "<Send email address>" // The email address of the domain registered with the Communication Services resource
to: "<recipient email address>"
subject: "This is the subject",
htmlContent: "<html><body>This is the html body</body></html>");

/// Call UpdateStatus on the email send operation to poll for the status
/// manually.
while (true)
{
await emailSendOperation.UpdateStatusAsync();
if (emailSendOperation.HasCompleted)
{
new EmailAddress(
email: "<recipient email address>"
displayName: "<recipient displayname>"
});
break;
}
await Task.Delay(100);
}

// Create the EmailMessage
var emailMessage = new EmailMessage(
sender: "<Send email address>" // The email address of the domain registered with the Communication Services resource
emailContent,
emailRecipients);
if (emailSendOperation.HasValue)
{
Console.WriteLine($"Email Sent. Status = {emailSendOperation.Value.Status}");
}

SendEmailResult sendResult = client.Send(emailMessage);
/// Get the OperationId so that it can be used for tracking the message for troubleshooting
string operationId = emailSendOperation.Id;
Console.WriteLine($"Email operation id = {operationId}");
```

Console.WriteLine($"Email id: {sendResult.MessageId}");
### Send an email message with more options
To send an email message, call the overload of `Send` or `SendAsync` function from the `EmailClient` that takes an `EmailMessage` parameter.
```C# Snippet:Azure_Communication_Email_Send_With_MoreOptions
// Create the email content
var emailContent = new EmailContent("This is the subject")
{
PlainText = "This is the body",
Html = "<html><body>This is the html body</body></html>"
};

// Create the EmailMessage
var emailMessage = new EmailMessage(
fromAddress: "<Send email address>" // The email address of the domain registered with the Communication Services resource
toAddress: "<recipient email address>"
content: emailContent);

var emailSendOperation = emailClient.Send(
wait: WaitUntil.Completed,
message: emailMessage);
Console.WriteLine($"Email Sent. Status = {emailSendOperation.Value.Status}");

/// Get the OperationId so that it can be used for tracking the message for troubleshooting
string operationId = emailSendOperation.Id;
Console.WriteLine($"Email operation id = {operationId}");
```

### Send an Email Message to Multiple Recipients
### Send an email message to multiple recipients
To send an email message to multiple recipients, add an `EmailAddress` object for each recipent type to the `EmailRecipient` object.

```C# Snippet:Azure_Communication_Email_Send_Multiple_Recipients
// Create the email content
var emailContent = new EmailContent("This is the subject");
emailContent.PlainText = "This is the body";
var emailContent = new EmailContent("This is the subject")
{
PlainText = "This is the body",
Html = "<html><body>This is the html body</body></html>"
};

// Create the To list
var toRecipients = new List<EmailAddress>
{
new EmailAddress(
email: "<recipient email address>"
address: "<recipient email address>"
displayName: "<recipient displayname>"
new EmailAddress(
email: "<recipient email address>"
address: "<recipient email address>"
displayName: "<recipient displayname>"
};

// Create the CC list
var ccRecipients = new List<EmailAddress>
{
new EmailAddress(
email: "<recipient email address>"
address: "<recipient email address>"
displayName: "<recipient displayname>"
new EmailAddress(
email: "<recipient email address>"
address: "<recipient email address>"
displayName: "<recipient displayname>"
};

// Create the BCC list
var bccRecipients = new List<EmailAddress>
{
new EmailAddress(
email: "<recipient email address>"
address: "<recipient email address>"
displayName: "<recipient displayname>"
new EmailAddress(
email: "<recipient email address>"
address: "<recipient email address>"
displayName: "<recipient displayname>"
};

var emailRecipients = new EmailRecipients(toRecipients, ccRecipients, bccRecipients);

// Create the EmailMessage
var emailMessage = new EmailMessage(
sender: "<Send email address>" // The email address of the domain registered with the Communication Services resource
emailContent,
emailRecipients);
senderAddress: "<Send email address>" // The email address of the domain registered with the Communication Services resource
emailRecipients,
emailContent);

SendEmailResult sendResult = client.Send(emailMessage);
EmailSendOperation emailSendOperation = emailClient.Send(WaitUntil.Completed, emailMessage);
Console.WriteLine($"Email Sent. Status = {emailSendOperation.Value.Status}");

Console.WriteLine($"Email id: {sendResult.MessageId}");
/// Get the OperationId so that it can be used for tracking the message for troubleshooting
string operationId = emailSendOperation.Id;
Console.WriteLine($"Email operation id = {operationId}");
```

### Send Email with Attachments
Azure Communication Services support sending email swith attachments. See [EmailAttachmentType][email_attachmentTypes] for a list of supported attachments
### Send email with attachments
Azure Communication Services support sending emails with attachments.
```C# Snippet:Azure_Communication_Email_Send_With_Attachments
// Create the EmailMessage
var emailMessage = new EmailMessage(
sender: "<Send email address>" // The email address of the domain registered with the Communication Services resource
emailContent,
emailRecipients);
fromAddress: "<Send email address>" // The email address of the domain registered with the Communication Services resource
toAddress: "<recipient email address>"
content: emailContent);

var filePath = "<path to your file>";
var attachmentName = "<name of your attachment>";
EmailAttachmentType attachmentType = EmailAttachmentType.Txt;
var contentType = MediaTypeNames.Text.Plain;

// Convert the file content into a Base64 string
byte[] bytes = File.ReadAllBytes(filePath);
string attachmentFileInBytes = Convert.ToBase64String(bytes);
var emailAttachment = new EmailAttachment(attachmentName, attachmentType, attachmentFileInBytes);
string content = new BinaryData(File.ReadAllBytes(filePath));
var emailAttachment = new EmailAttachment(attachmentName, contentType, content);

emailMessage.Attachments.Add(emailAttachment);

SendEmailResult sendResult = client.Send(emailMessage);
```

### Get Email Message Status
The `EmailSendResult` from the `Send` call contains a `MessageId` which can be used to query the status of the email.
```C# Snippet:Azure_Communication_Email_GetSendStatus
SendEmailResult sendResult = client.Send(emailMessage);
EmailSendOperation emailSendOperation = emailClient.Send(WaitUntil.Completed, emailMessage);
Console.WriteLine($"Email Sent. Status = {emailSendOperation.Value.Status}");

SendStatusResult status = client.GetSendStatus(sendResult.MessageId);
/// Get the OperationId so that it can be used for tracking the message for troubleshooting
string operationId = emailSendOperation.Id;
Console.WriteLine($"Email operation id = {operationId}");
```

## Troubleshooting
Expand Down Expand Up @@ -184,5 +232,4 @@ This project has adopted the [Microsoft Open Source Code of Conduct][coc]. For m
[nextsteps]:https://aka.ms/acsemail/qs-sendmail?pivots=programming-language-csharp
[nuget]: https://www.nuget.org/
[source]: https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/communication/Azure.Communication.Email/src
[email_attachmentTypes]: https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/communication/Azure.Communication.Email/src/Generated/Models/EmailAttachmentType.cs
[domain_overview]: https://aka.ms/acsemail/domainsoverview
Loading