Skip to content

Commit 967624f

Browse files
Merge pull request #232819 from natekimball-msft/natekimball/update-dotnet-doc-from-feedback
Updating dotnet docs with RequestFailedException
2 parents 7c590e5 + d695189 commit 967624f

File tree

2 files changed

+94
-116
lines changed

2 files changed

+94
-116
lines changed

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

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -166,30 +166,30 @@ try
166166

167167
Duration timeElapsed = Duration.ofSeconds(0);
168168

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);
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());
178175

179-
if (timeElapsed.compareTo(POLLER_WAIT_TIME.multipliedBy(18)) >= 0)
180-
{
181-
throw new RuntimeException("Polling timed out.");
182-
}
183-
}
176+
Thread.sleep(POLLER_WAIT_TIME.toMillis());
177+
timeElapsed = timeElapsed.plus(POLLER_WAIT_TIME);
184178

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
179+
if (timeElapsed.compareTo(POLLER_WAIT_TIME.multipliedBy(18)) >= 0)
190180
{
191-
throw new RuntimeException(poller.getFinalResult().getError().getMessage());
181+
throw new RuntimeException("Polling timed out.");
192182
}
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+
}
193193
}
194194
catch (Exception exception)
195195
{

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

Lines changed: 74 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ var endpoint = new Uri("<your-endpoint-uri>");
150150
var emailClient = new EmailClient(endpoint, key);
151151
```
152152

153-
## Basic Email Sending
153+
## Basic email sending
154154

155155
### Construct your email message
156156

@@ -175,34 +175,31 @@ var recipient = "[email protected]";
175175

176176
To send an email message, you need to:
177177
- Call SendSync method that sends the email request as an asynchronous operation. Call with Azure.WaitUntil.Completed if your method should wait to return until the long-running operation has completed on the service. Call with Azure.WaitUntil.Started if your method should return after starting the operation.
178-
- SendAsync method returns EmailSendOperation that returns "Succeeded" EmailSendStatus if email is out for delivery. Add this code to the end of `Main` method in **Program.cs**:
178+
- SendAsync method returns EmailSendOperation that returns "Succeeded" EmailSendStatus if email is out for delivery and throws an exception otherwise. Add this code to the end of `Main` method in **Program.cs**:
179179

180180
```csharp
181181
try
182182
{
183-
Console.WriteLine("Sending email...");
184-
EmailSendOperation emailSendOperation = await emailClient.SendAsync(Azure.WaitUntil.Completed, sender, recipient, subject, htmlContent);
185-
EmailSendResult statusMonitor = emailSendOperation.Value;
186-
187-
string operationId = emailSendOperation.Id;
188-
var emailSendStatus = statusMonitor.Status;
183+
Console.WriteLine("Sending email...");
184+
EmailSendOperation emailSendOperation = await emailClient.SendAsync(
185+
Azure.WaitUntil.Completed,
186+
sender,
187+
recipient,
188+
subject,
189+
htmlContent);
190+
EmailSendResult statusMonitor = emailSendOperation.Value;
191+
192+
Console.WriteLine($"Email Sent. Status = {emailSendOperation.Value.Status}");
189193

190-
if (emailSendStatus == EmailSendStatus.Succeeded)
191-
{
192-
Console.WriteLine($"Email send operation succeeded with OperationId = {operationId}.\nEmail is out for delivery.");
193-
}
194-
else
195-
{
196-
var error = statusMonitor.Error;
197-
Console.WriteLine($"Failed to send email.\n OperationId = {operationId}.\n Status = {emailSendStatus}.");
198-
Console.WriteLine($"Error Code = {error.Code}, Message = {error.Message}");
199-
return;
200-
}
194+
/// Get the OperationId so that it can be used for tracking the message for troubleshooting
195+
string operationId = emailSendOperation.Id;
196+
Console.WriteLine($"Email operation id = {operationId}");
201197
}
202-
catch (Exception ex)
198+
catch (RequestFailedException ex)
203199
{
204-
Console.WriteLine($"Error in sending email, {ex}");
205-
}
200+
/// OperationID is contained in the exception message and can be used for troubleshooting purposes
201+
Console.WriteLine($"Email send operation failed with error code: {ex.ErrorCode}, message: {ex.Message}");
202+
}
206203
```
207204

208205
### Getting email delivery status
@@ -245,52 +242,41 @@ The returned EmailSendOperation object contains an EmailSendStatus object that c
245242
- An error object with failure details if the current status is in a failed state.
246243

247244
```csharp
245+
Console.WriteLine("Sending email with Async no Wait...");
246+
EmailSendOperation emailSendOperation = await emailClient.SendAsync(
247+
Azure.WaitUntil.Started,
248+
sender,
249+
recipient,
250+
subject,
251+
htmlContent);
252+
253+
/// Call UpdateStatus on the email send operation to poll for the status
254+
/// manually.
248255
try
249256
{
250-
Console.WriteLine("Sending email with Async no Wait...");
251-
EmailSendOperation emailSendOperation = await emailClient.SendAsync(Azure.WaitUntil.Started, sender, recipient, subject, htmlContent);
252-
253-
var cancellationToken = new CancellationTokenSource(TimeSpan.FromMinutes(2));
254-
255-
// Poll for email send status manually
256-
while (!cancellationToken.IsCancellationRequested)
257-
{
258-
await emailSendOperation.UpdateStatusAsync();
259-
if (emailSendOperation.HasCompleted)
260-
{
261-
break;
262-
}
263-
Console.WriteLine("Email send operation is still running...");
264-
await Task.Delay(1000);
265-
}
266-
267-
if (emailSendOperation.HasValue)
268-
{
269-
EmailSendResult statusMonitor = emailSendOperation.Value;
270-
string operationId = emailSendOperation.Id;
271-
var emailSendStatus = statusMonitor.Status;
272-
273-
if (emailSendStatus == EmailSendStatus.Succeeded)
257+
while (true)
274258
{
275-
Console.WriteLine($"Email send operation succeeded with OperationId = {operationId}.\nEmail is out for delivery.");
259+
await emailSendOperation.UpdateStatusAsync();
260+
if (emailSendOperation.HasCompleted)
261+
{
262+
break;
263+
}
264+
await Task.Delay(100);
276265
}
277-
else
266+
267+
if (emailSendOperation.HasValue)
278268
{
279-
var error = statusMonitor.Error;
280-
Console.WriteLine($"Failed to send email.\n OperationId = {operationId}.\n Status = {emailSendStatus}.");
281-
Console.WriteLine($"Error Code = {error.Code}, Message = {error.Message}");
282-
return;
269+
Console.WriteLine($"Email queued for delivery. Status = {emailSendOperation.Value.Status}");
283270
}
284-
}
285-
else if (cancellationToken.IsCancellationRequested)
286-
{
287-
Console.WriteLine($"We have timed out while polling for email status");
288-
}
289271
}
290-
catch (Exception ex)
272+
catch (RequestFailedException ex)
291273
{
292-
Console.WriteLine($"Error in sending email, {ex}");
274+
Console.WriteLine($"Email send failed with Code = {ex.ErrorCode} and Message = {ex.Message}");
293275
}
276+
277+
/// Get the OperationId so that it can be used for tracking the message for troubleshooting
278+
string operationId = emailSendOperation.Id;
279+
Console.WriteLine($"Email operation id = {operationId}");
294280
```
295281

296282

@@ -307,65 +293,57 @@ You can download the sample app from [GitHub](https://github.com/Azure-Samples/c
307293

308294
### Send an email message using the object model to construct the email payload
309295

310-
- Construct the email content and body using EmailContent.
296+
- Construct the email subject and body using EmailContent.
311297
- Add Recipients.
312298
- Set email importance through custom headers.
313299
- Construct your email message using your sender email address, defined in the MailFrom list of the domain linked in your Communication Services Resource.
314300
- Include your EmailContent and EmailRecipients, optionally adding attachments.
315301

316302
```csharp
317-
318-
EmailContent emailContent = new EmailContent("Welcome to Azure Communication Service Email APIs.");
319-
320303
var subject = "Welcome to Azure Communication Service Email APIs.";
321-
322304
var emailContent = new EmailContent(subject)
323305
{
324-
PlainText = "This email message is sent from Azure Communication Service Email using .NET SDK.",
325-
Html = "<html><body><h1>Quick send email test</h1><br/><h4>This email message is sent from Azure Communication Service Email using .NET SDK.</h4></body></html>"
306+
PlainText = "This email message is sent from Azure Communication Service Email using .NET SDK.",
307+
Html = "<html><body><h1>Quick send email test</h1><br/><h4>This email message is sent from Azure Communication Service Email using .NET SDK.</h4></body></html>"
326308
};
327-
328-
var sender = "[email protected]";
329309

330-
List<EmailAddress> emailAddresses = new List<EmailAddress> { new EmailAddress("[email protected]") { DisplayName = "Friendly Display Name" }};
310+
List<EmailAddress> emailAddresses = new List<EmailAddress>
311+
{
312+
new EmailAddress("[email protected]", "Friendly Display Name")
313+
};
331314

332315
EmailRecipients emailRecipients = new EmailRecipients(emailAddresses);
333316

334317
var emailMessage = new EmailMessage(sender, emailRecipients, emailContent)
335318
{
336-
// Header name is "x-priority" or "x-msmail-priority"
337-
// Header value is a number from 1 to 5. 1 or 2 = High, 3 = Normal, 4 or 5 = Low
338-
// Not all email clients recognize this header directly (outlook client does recognize)
339-
Headers =
340-
{
341-
// Set Email Importance to High
342-
{ "x-priority", "1" },
343-
{ "", "" }
344-
}
319+
// Header name is "x-priority" or "x-msmail-priority"
320+
// Header value is a number from 1 to 5. 1 or 2 = High, 3 = Normal, 4 or 5 = Low
321+
// Not all email clients recognize this header directly (outlook client does recognize)
322+
Headers =
323+
{
324+
// Set Email Importance to High
325+
{ "x-priority", "1" },
326+
{ "", "" }
327+
}
345328
};
346329

347330
try
348331
{
349-
Console.WriteLine("Sending email to multiple recipients...");
350-
EmailSendOperation emailSendOperation = await emailClient.SendAsync(Azure.WaitUntil.Completed, emailMessage);
351-
EmailSendResult statusMonitor = emailSendOperation.Value;
352-
353-
string operationId = emailSendOperation.Id;
354-
var emailSendStatus = statusMonitor.Status;
355-
356-
if (emailSendStatus == EmailSendStatus.Succeeded)
357-
{
358-
Console.WriteLine($"Email send operation succeeded with OperationId = {operationId}.\nEmail is out for delivery.");
359-
}
360-
else
361-
{
362-
Console.WriteLine($"Failed to send email. \n OperationId = {operationId}. \n Status = {emailSendStatus}");
363-
return;
364-
}
332+
Console.WriteLine("Sending email to multiple recipients...");
333+
EmailSendOperation emailSendOperation = emailClient.Send(
334+
WaitUntil.Completed,
335+
emailMessage);
336+
337+
Console.WriteLine($"Email Sent. Status = {emailSendOperation.Value.Status}");
338+
339+
/// Get the OperationId so that it can be used for tracking the message for troubleshooting
340+
string operationId = emailSendOperation.Id;
341+
Console.WriteLine($"Email operation id = {operationId}");
365342
}
366-
catch (Exception ex)
343+
catch (RequestFailedException ex)
367344
{
368-
Console.WriteLine($"Error in sending email, {ex}");
345+
/// OperationID is contained in the exception message and can be used for troubleshooting purposes
346+
Console.WriteLine($"Email send operation failed with error code: {ex.ErrorCode}, message: {ex.Message}");
369347
}
370348

371349
```

0 commit comments

Comments
 (0)