Skip to content

Commit a8d63b3

Browse files
Updates based on bugbash feedback
1 parent acaa9a3 commit a8d63b3

File tree

4 files changed

+79
-23
lines changed

4 files changed

+79
-23
lines changed

articles/communication-services/quickstarts/email/includes/send-email-java.md

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ EmailClient emailClient = new EmailClientBuilder()
119119

120120
### Option 2: Authenticate using Azure Active Directory
121121

122-
A `DefaultAzureCredential` object must be passed to the `EmailClientBuilder` via the `credential()` method. An endpoint must also be set via the `endpoint()` method.
122+
A [DefaultAzureCredential](https://github.com/Azure/azure-sdk-for-java/tree/main/sdk/identity/azure-identity#defaultazurecredential) object must be passed to the `EmailClientBuilder` via the `credential()` method. An endpoint must also be set via the `endpoint()` method.
123123

124124
The `AZURE_CLIENT_SECRET`, `AZURE_CLIENT_ID`, and `AZURE_TENANT_ID` environment variables are needed to create a `DefaultAzureCredential` object.
125125

@@ -158,10 +158,43 @@ EmailMessage message = new EmailMessage()
158158
.setSubject("Welcome to Azure Communication Services Email")
159159
.setBodyPlainText("This email message is sent from Azure Communication Services Email using the Java SDK.");
160160

161-
SyncPoller<EmailSendResult, EmailSendResult> poller = emailClient.beginSend(message, null);
162-
PollResponse<EmailSendResult> response = poller.waitForCompletion();
163-
164-
System.out.println("Operation Id: " + response.getValue().getId());
161+
try
162+
{
163+
SyncPoller<EmailSendResult, EmailSendResult> poller = emailClient.beginSend(message, null);
164+
165+
PollResponse<EmailSendResult> pollResponse = null;
166+
167+
Duration timeElapsed = Duration.ofSeconds(0);
168+
169+
while (pollResponse == null
170+
|| pollResponse.getStatus() == LongRunningOperationStatus.NOT_STARTED
171+
|| pollResponse.getStatus() == LongRunningOperationStatus.IN_PROGRESS)
172+
{
173+
pollResponse = poller.poll();
174+
System.out.println("Email send poller status: " + pollResponse.getStatus());
175+
176+
Thread.sleep(POLLER_WAIT_TIME.toMillis());
177+
timeElapsed = timeElapsed.plus(POLLER_WAIT_TIME);
178+
179+
if (timeElapsed.compareTo(POLLER_WAIT_TIME.multipliedBy(18)) >= 0)
180+
{
181+
throw new RuntimeException("Polling timed out.");
182+
}
183+
}
184+
185+
if (poller.getFinalResult().getStatus() == EmailSendStatus.SUCCEEDED)
186+
{
187+
System.out.printf("Successfully sent the email (operation id: %s)", poller.getFinalResult().getId());
188+
}
189+
else
190+
{
191+
throw new RuntimeException(poller.getFinalResult().getError().getMessage());
192+
}
193+
}
194+
catch (Exception exception)
195+
{
196+
System.out.println(exception.getMessage());
197+
}
165198
```
166199

167200
Make these replacements in the code:
@@ -227,15 +260,17 @@ EmailMessage message = new EmailMessage()
227260
.setSenderAddress("<[email protected]>")
228261
.setSubject("Welcome to Azure Communication Services Email")
229262
.setBodyPlainText("This email message is sent from Azure Communication Services Email using the Java SDK.")
230-
.setToRecipients(toAddress1, toAddress2);
263+
.setToRecipients(toAddress1, toAddress2)
264+
.setCcRecipients(toAddress1, toAddress2)
265+
.setBccRecipients(toAddress1, toAddress2)
231266

232267
SyncPoller<EmailSendResult, EmailSendResult> poller = emailClient.beginSend(message, null);
233268
PollResponse<EmailSendResult> response = poller.waitForCompletion();
234269

235270
System.out.println("Operation Id: " + response.getValue().getId());
236271
```
237272

238-
You can download the sample app demonstrating this action from [GitHub](https://github.com/Azure-Samples/communication-services-java-quickstarts/tree/main/send-email)
273+
You can download the sample app demonstrating this action from [GitHub](https://github.com/Azure-Samples/communication-services-java-quickstarts/tree/main/send-email-advanced)
239274

240275
### Send an email message with attachments
241276

@@ -264,4 +299,4 @@ System.out.println("Operation Id: " + response.getValue().getId());
264299

265300
For more information on acceptable MIME types for email attachments, see the [allowed MIME types](../../../concepts/email/email-attachment-allowed-mime-types.md) documentation.
266301

267-
You can download the sample app demonstrating this action from [GitHub](https://github.com/Azure-Samples/communication-services-java-quickstarts/tree/main/send-email)
302+
You can download the sample app demonstrating this action from [GitHub](https://github.com/Azure-Samples/communication-services-java-quickstarts/tree/main/send-email-advanced)

articles/communication-services/quickstarts/email/includes/send-email-js.md

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,30 @@ async function main() {
172172
};
173173

174174
const poller = await emailClient.beginSend(message);
175-
const response = await poller.pollUntilDone();
175+
176+
if (!poller.getOperationState().isStarted) {
177+
throw "Poller was not started."
178+
}
179+
180+
let timeElapsed = 0;
181+
while(!poller.isDone()) {
182+
poller.poll();
183+
console.log("Email send polling in progress");
184+
185+
await new Promise(resolve => setTimeout(resolve, POLLER_WAIT_TIME * 1000));
186+
timeElapsed += 10;
187+
188+
if(timeElapsed > 18 * POLLER_WAIT_TIME) {
189+
throw "Polling timed out.";
190+
}
191+
}
192+
193+
if(poller.getResult().status === KnownEmailSendStatus.Succeeded) {
194+
console.log(`Successfully sent the email (operation id: ${poller.getResult().id})`);
195+
}
196+
else {
197+
throw poller.getResult().error;
198+
}
176199
} catch (e) {
177200
console.log(e);
178201
}
@@ -273,7 +296,7 @@ const message = {
273296
attachments: [
274297
{
275298
name: path.basename(filePath),
276-
contentType: "text/plain",
299+
contentType: "<mime-type-for-your-file>",
277300
contentInBase64: readFileSync(filePath, "base64"),
278301
}
279302
]

articles/communication-services/quickstarts/email/includes/send-email-net.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,12 +124,12 @@ EmailClient emailClient = new EmailClient(connectionString);
124124

125125
### Option 2: Authenticate using Azure Active Directory
126126

127-
To authenticate using Azure Active Directory, install the `Azure.Identity` library package for .NET by using the `dotnet add package` command.
127+
To authenticate using [Azure Active Directory](https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/identity/Azure.Identity), install the `Azure.Identity` library package for .NET by using the `dotnet add package` command.
128128

129129
```console
130130
dotnet add package Azure.Identity
131131
```
132-
Open **Program.cs** in a text editor and replace the body of the `Main` method with code to initialize an `EmailClient` using `DefaultAzureCredential`. The Azure Identity SDK reads values from three environment variables at runtime to authenticate the application. Learn how to [create an Azure Active Directory Registered Application and set the environment variables](../../identity/service-principal.md?pivots=platform-azcli).
132+
Open **Program.cs** in a text editor and replace the body of the `Main` method with code to initialize an `EmailClient` using [DefaultAzureCredential](https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/identity/Azure.Identity#defaultazurecredential). The Azure Identity SDK reads values from three environment variables at runtime to authenticate the application. Learn how to [create an Azure Active Directory Registered Application and set the environment variables](../../identity/service-principal.md?pivots=platform-azcli).
133133

134134
```csharp
135135
// This code demonstrates how to authenticate to your Communication Service resource using

articles/communication-services/quickstarts/email/includes/send-email-python.md

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ email_client = EmailClient.from_connection_string(<connection_string>)
165165

166166
### Option 2: Authenticate using Azure Active Directory
167167

168-
You can also use Active Directory authentication using DefaultAzureCredential.
168+
You can also use Active Directory authentication using [DefaultAzureCredential](../../../concepts/authentication.md).
169169

170170
```python
171171
from azure.communication.email import EmailClient
@@ -242,7 +242,7 @@ POLLER_WAIT_TIME = 10
242242
try:
243243
email_client = EmailClient.from_connection_string(connection_string)
244244

245-
poller = client.begin_send(message);
245+
poller = email_client.begin_send(message);
246246

247247
time_elapsed = 0
248248
while not poller.done():
@@ -254,7 +254,7 @@ try:
254254
if time_elapsed > 18 * POLLER_WAIT_TIME:
255255
raise RuntimeError("Polling timed out.")
256256

257-
if poller.status() == "Succeeded":
257+
if poller.result()["status"] == "Succeeded":
258258
print(f"Successfully sent the email (operation id: {poller.result()['id']})")
259259
else:
260260
raise RuntimeError(str(poller.result()["error"]))
@@ -279,7 +279,7 @@ You can download the sample app from [GitHub](https://github.com/Azure-Samples/c
279279

280280
### Send an email message to multiple recipients
281281

282-
We can define multiple recipients by adding more EmailAddresses to the EmailRecipients object. These addresses can be added as `to`, `cc`, or `bcc` recipient lists accordingly.
282+
We can define multiple recipients by adding more email addresses to the `recipients` object. These addresses can be added as `to`, `cc`, or `bcc` recipient lists accordingly.
283283

284284
```python
285285
message = {
@@ -306,20 +306,18 @@ message = {
306306
}
307307
```
308308

309-
You can download the sample app demonstrating this action from [GitHub](https://github.com/Azure-Samples/communication-services-java-quickstarts/tree/main/send-email)
309+
You can download the sample app demonstrating this action from [GitHub](https://github.com/Azure-Samples/communication-services-python-quickstarts/tree/main/send-email-advanced)
310310

311311

312312
### Send an email message with attachments
313313

314-
We can add an attachment by defining an EmailAttachment object and adding it to our EmailMessage object. Read the attachment file and encode it using Base64. Decode the bytes as a string and pass it into the EmailAttachment object.
314+
We can add an attachment by defining an `attachment` and adding it to the `attachments` of our `message` object. Read the attachment file and encode it using Base64. Decode the bytes as a string and pass it into the `attachment` object.
315315

316316
```python
317317
import base64
318318

319-
with open("<your-attachment-path>", "rb") as file:
320-
file_bytes = file.read()
321-
322-
file_bytes_b64 = base64.b64encode(file_bytes)
319+
with open("<path-to-your-attachment>", "rb") as file:
320+
file_bytes_b64 = base64.b64encode(file.read())
323321

324322
message = {
325323
"content": {
@@ -348,4 +346,4 @@ message = {
348346

349347
For more information on acceptable MIME types for email attachments, see the [allowed MIME types](../../../concepts/email/email-attachment-allowed-mime-types.md) documentation.
350348

351-
You can download the sample app demonstrating this action from [GitHub](https://github.com/Azure-Samples/communication-services-java-quickstarts/tree/main/send-email)
349+
You can download the sample app demonstrating this action from [GitHub](https://github.com/Azure-Samples/communication-services-python-quickstarts/tree/main/send-email-advanced)

0 commit comments

Comments
 (0)