@@ -23,15 +23,14 @@ To create these resource, you can use the [Azure Portal][communication_resource_
2323### Using statements
2424``` C# Snippet:Azure_Communication_Email_UsingStatements
2525using Azure .Communication .Email ;
26- using Azure .Communication .Email .Models ;
2726```
2827
2928### Authenticate the client
3029Email clients can be authenticated using the connection string acquired from an Azure Communication Resource in the [ Azure Portal] [ azure_portal ] .
3130
3231``` C# Snippet:Azure_Communication_Email_CreateEmailClient
3332var connectionString = " <connection_string>" ; // Find your Communication Services resource in the Azure portal
34- EmailClient client = new EmailClient (connectionString );
33+ EmailClient emailClient = new EmailClient (connectionString );
3534```
3635
3736Alternatively, Email clients can also be authenticated using a valid token credential. With this option,
@@ -41,119 +40,168 @@ Alternatively, Email clients can also be authenticated using a valid token crede
4140string endpoint = " <endpoint_url>" ;
4241TokenCredential tokenCredential = new DefaultAzureCredential ();
4342tokenCredential = new DefaultAzureCredential ();
44- EmailClient client = new EmailClient (new Uri (endpoint ), tokenCredential );
43+ EmailClient emailClient = new EmailClient (new Uri (endpoint ), tokenCredential );
4544```
4645## Examples
47- ### Send an Email Message
48- To send an email message, call the ` Send ` or ` SendAsync ` function from the ` EmailClient ` .
49- ``` C# Snippet:Azure_Communication_Email_Send
50- // Create the email content
51- var emailContent = new EmailContent (" This is the subject" );
52- emailContent .PlainText = " This is the body" ;
46+ ### Send a simple email message with automatic polling for status
47+ To send an email message, call the simple overload of ` Send ` or ` SendAsync ` function from the ` EmailClient ` .
48+ ``` C# Snippet:Azure_Communication_Email_Send_Simple_AutoPolling
49+ var emailSendOperation = emailClient .Send (
50+ wait : WaitUntil .Completed ,
51+ from : " <Send email address>" // The email address of the domain registered with the Communication Services resource
52+ to : " <recipient email address>"
53+ subject : " This is the subject" ,
54+ htmlContent : " <html><body>This is the html body</body></html>" );
55+ Console .WriteLine ($" Email Sent. Status = {emailSendOperation .Value .Status }" );
56+
57+ /// Get the OperationId so that it can be used for tracking the message for troubleshooting
58+ string operationId = emailSendOperation .Id ;
59+ Console .WriteLine ($" Email operation id = {operationId }" );
60+ ```
5361
54- // Create the recipient list
55- var emailRecipients = new EmailRecipients (
56- new List <EmailAddress >
62+ ### Send a simple email message with manual polling for status
63+ To send an email message, call the simple overload of ` Send ` or ` SendAsync ` function from the ` EmailClient ` .
64+ ``` C# Snippet:Azure_Communication_Email_Send_Simple_ManualPolling_Async
65+ /// Send the email message with WaitUntil.Started
66+ var emailSendOperation = await emailClient .SendAsync (
67+ wait : WaitUntil .Started ,
68+ from : " <Send email address>" // The email address of the domain registered with the Communication Services resource
69+ to : " <recipient email address>"
70+ subject : " This is the subject" ,
71+ htmlContent : " <html><body>This is the html body</body></html>" );
72+
73+ /// Call UpdateStatus on the email send operation to poll for the status
74+ /// manually.
75+ while (true )
76+ {
77+ await emailSendOperation .UpdateStatusAsync ();
78+ if (emailSendOperation .HasCompleted )
5779 {
58- new EmailAddress (
59- email : " <recipient email address> "
60- displayName : " <recipient displayname> "
61- });
80+ break ;
81+ }
82+ await Task . Delay ( 100 );
83+ }
6284
63- // Create the EmailMessage
64- var emailMessage = new EmailMessage (
65- sender : " <Send email address>" // The email address of the domain registered with the Communication Services resource
66- emailContent ,
67- emailRecipients );
85+ if (emailSendOperation .HasValue )
86+ {
87+ Console .WriteLine ($" Email Sent. Status = {emailSendOperation .Value .Status }" );
88+ }
6889
69- SendEmailResult sendResult = client .Send (emailMessage );
90+ /// Get the OperationId so that it can be used for tracking the message for troubleshooting
91+ string operationId = emailSendOperation .Id ;
92+ Console .WriteLine ($" Email operation id = {operationId }" );
93+ ```
7094
71- Console .WriteLine ($" Email id: {sendResult .MessageId }" );
95+ ### Send an email message with more options
96+ To send an email message, call the overload of ` Send ` or ` SendAsync ` function from the ` EmailClient ` that takes an ` EmailMessage ` parameter.
97+ ``` C# Snippet:Azure_Communication_Email_Send_With_MoreOptions
98+ // Create the email content
99+ var emailContent = new EmailContent (" This is the subject" )
100+ {
101+ PlainText = " This is the body" ,
102+ Html = " <html><body>This is the html body</body></html>"
103+ };
104+
105+ // Create the EmailMessage
106+ var emailMessage = new EmailMessage (
107+ fromAddress : " <Send email address>" // The email address of the domain registered with the Communication Services resource
108+ toAddress : " <recipient email address>"
109+ content : emailContent );
110+
111+ var emailSendOperation = emailClient .Send (
112+ wait : WaitUntil .Completed ,
113+ message : emailMessage );
114+ Console .WriteLine ($" Email Sent. Status = {emailSendOperation .Value .Status }" );
115+
116+ /// Get the OperationId so that it can be used for tracking the message for troubleshooting
117+ string operationId = emailSendOperation .Id ;
118+ Console .WriteLine ($" Email operation id = {operationId }" );
72119```
73120
74- ### Send an Email Message to Multiple Recipients
121+ ### Send an email message to multiple recipients
75122To send an email message to multiple recipients, add an ` EmailAddress ` object for each recipent type to the ` EmailRecipient ` object.
76123
77124``` C# Snippet:Azure_Communication_Email_Send_Multiple_Recipients
78125// Create the email content
79- var emailContent = new EmailContent (" This is the subject" );
80- emailContent .PlainText = " This is the body" ;
126+ var emailContent = new EmailContent (" This is the subject" )
127+ {
128+ PlainText = " This is the body" ,
129+ Html = " <html><body>This is the html body</body></html>"
130+ };
81131
82132// Create the To list
83133var toRecipients = new List <EmailAddress >
84134{
85135 new EmailAddress (
86- email : " <recipient email address>"
136+ address : " <recipient email address>"
87137 displayName : " <recipient displayname>"
88138 new EmailAddress (
89- email : " <recipient email address>"
139+ address : " <recipient email address>"
90140 displayName : " <recipient displayname>"
91141};
92142
93143// Create the CC list
94144var ccRecipients = new List <EmailAddress >
95145{
96146 new EmailAddress (
97- email : " <recipient email address>"
147+ address : " <recipient email address>"
98148 displayName : " <recipient displayname>"
99149 new EmailAddress (
100- email : " <recipient email address>"
150+ address : " <recipient email address>"
101151 displayName : " <recipient displayname>"
102152};
103153
104154// Create the BCC list
105155var bccRecipients = new List <EmailAddress >
106156{
107157 new EmailAddress (
108- email : " <recipient email address>"
158+ address : " <recipient email address>"
109159 displayName : " <recipient displayname>"
110160 new EmailAddress (
111- email : " <recipient email address>"
161+ address : " <recipient email address>"
112162 displayName : " <recipient displayname>"
113163};
114164
115165var emailRecipients = new EmailRecipients (toRecipients , ccRecipients , bccRecipients );
116166
117167// Create the EmailMessage
118168var emailMessage = new EmailMessage (
119- sender : " <Send email address>" // The email address of the domain registered with the Communication Services resource
120- emailContent ,
121- emailRecipients );
169+ senderAddress : " <Send email address>" // The email address of the domain registered with the Communication Services resource
170+ emailRecipients ,
171+ emailContent );
122172
123- SendEmailResult sendResult = client .Send (emailMessage );
173+ EmailSendOperation emailSendOperation = emailClient .Send (WaitUntil .Completed , emailMessage );
174+ Console .WriteLine ($" Email Sent. Status = {emailSendOperation .Value .Status }" );
124175
125- Console .WriteLine ($" Email id: {sendResult .MessageId }" );
176+ /// Get the OperationId so that it can be used for tracking the message for troubleshooting
177+ string operationId = emailSendOperation .Id ;
178+ Console .WriteLine ($" Email operation id = {operationId }" );
126179```
127180
128- ### Send Email with Attachments
129- Azure Communication Services support sending email swith attachments . See [ EmailAttachmentType ][ email_attachmentTypes ] for a list of supported attachments
181+ ### Send email with attachments
182+ Azure Communication Services support sending emails with attachments .
130183```C # Snippet : Azure_Communication_Email_Send_With_Attachments
131184// Create the EmailMessage
132185var emailMessage = new EmailMessage (
133- sender : " <Send email address>" // The email address of the domain registered with the Communication Services resource
134- emailContent ,
135- emailRecipients );
186+ fromAddress : " <Send email address>" // The email address of the domain registered with the Communication Services resource
187+ toAddress : " <recipient email address> "
188+ content : emailContent );
136189
137190var filePath = " <path to your file>" ;
138191var attachmentName = " <name of your attachment>" ;
139- EmailAttachmentType attachmentType = EmailAttachmentType . Txt ;
192+ var contentType = MediaTypeNames . Text . Plain ;
140193
141- // Convert the file content into a Base64 string
142- byte [] bytes = File .ReadAllBytes (filePath );
143- string attachmentFileInBytes = Convert .ToBase64String (bytes );
144- var emailAttachment = new EmailAttachment (attachmentName , attachmentType , attachmentFileInBytes );
194+ string content = new BinaryData (File .ReadAllBytes (filePath ));
195+ var emailAttachment = new EmailAttachment (attachmentName , contentType , content );
145196
146197emailMessage .Attachments .Add (emailAttachment );
147198
148- SendEmailResult sendResult = client .Send (emailMessage );
149- ```
150-
151- ### Get Email Message Status
152- The `EmailSendResult ` from the `Send ` call contains a `MessageId ` which can be used to query the status of the email .
153- ```C # Snippet : Azure_Communication_Email_GetSendStatus
154- SendEmailResult sendResult = client .Send (emailMessage );
199+ EmailSendOperation emailSendOperation = emailClient .Send (WaitUntil .Completed , emailMessage );
200+ Console .WriteLine ($" Email Sent. Status = {emailSendOperation .Value .Status }" );
155201
156- SendStatusResult status = client .GetSendStatus (sendResult .MessageId );
202+ /// Get the OperationId so that it can be used for tracking the message for troubleshooting
203+ string operationId = emailSendOperation .Id ;
204+ Console .WriteLine ($" Email operation id = {operationId }" );
157205```
158206
159207## Troubleshooting
@@ -184,5 +232,4 @@ This project has adopted the [Microsoft Open Source Code of Conduct][coc]. For m
184232 [nextsteps ]: https : // aka.ms/acsemail/qs-sendmail?pivots=programming-language-csharp
185233 [nuget ]: https : // www.nuget.org/
186234 [source ]: https : // github.com/Azure/azure-sdk-for-net/tree/main/sdk/communication/Azure.Communication.Email/src
187- [email_attachmentTypes ]: https : // github.com/Azure/azure-sdk-for-net/blob/main/sdk/communication/Azure.Communication.Email/src/Generated/Models/EmailAttachmentType.cs
188235 [domain_overview ]: https : // aka.ms/acsemail/domainsoverview
0 commit comments