Skip to content

Commit 50d0973

Browse files
authored
Add custom headers documentation
Add custom headers documentation
1 parent 4769f05 commit 50d0973

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
---
2+
title: Custom headers for Azure Email Communication Service
3+
titleSuffix: An Azure Communication Services concept document
4+
description: Learn about sending custom headers.
5+
author: anmolbohra
6+
manager: koagbakp
7+
services: azure-communication-services
8+
ms.author: anmolbohra
9+
ms.date: 01/09/2025
10+
ms.topic: conceptual
11+
ms.service: azure-communication-services
12+
---
13+
# Adding Custom headers
14+
15+
Custom headers can be sent along with an email request. These headers are defined as a dictionary. There are some pre defined custom headers which can be used to handle some of the email sending scenarios.
16+
17+
18+
| Custom Header Name | Property |
19+
| --- | --- |
20+
| x-ms-acsemail-ignore-duplicate-content-id | When this header is explicitly set to true, we do not validate duplicate content id and will let the author manage duplicates. |
21+
| x-ms-acsemail-suppress-invalid-attachment | By default, if the attachment is invalid, we return bad request. If this header is set to true and if an attachment is invalid, we continue with the request without the attachment. |
22+
| x-ms-acsemail-validate-message-id | When this header is explicitly set to true, we validate the internet message id sent by the customer. The validations include checking for RFC 2822 Internet Message Id format and also if there's a duplicate already present. If it fails, we return bad request. If the header is not set and the internet message id validation fails, we remove the message id and let ideas create one. |
23+
24+
25+
## Send an email message with custom headers
26+
27+
We can define custom headers by adding header details to emailMessage.
28+
29+
```csharp
30+
// Create the email content
31+
var emailContent = new EmailContent("Welcome to Azure Communication Service Email APIs.")
32+
{
33+
PlainText = "This email message is sent from Azure Communication Service Email.",
34+
Html = "<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>"
35+
};
36+
37+
// Create the To list
38+
var toRecipients = new List<EmailAddress>
39+
{
40+
new EmailAddress("<[email protected]>"),
41+
new EmailAddress("<[email protected]>"),
42+
};
43+
44+
// Add Custom headers
45+
var customHeaders = new Dictionary<string, string>
46+
{
47+
{"x-ms-acsemail-suppress-invalid-attachment", "true"}, // if the attachment
48+
}
49+
50+
var emailAttachment = new EmailAttachment("attachment.pdf", MediaTypeNames.Application.Pdf, contentBinaryData);
51+
52+
EmailRecipients emailRecipients = new EmailRecipients(toRecipients);
53+
54+
// Create the EmailMessage
55+
var emailMessage = new EmailMessage(
56+
senderAddress: "[email protected]" // The email address of the domain registered with the Communication Services resource
57+
emailRecipients,
58+
emailContent);
59+
60+
// Add custom headers to the emailMessage
61+
emailMessage.Headers.Add(customHeaders);
62+
// Add attachment to the emailMessage
63+
emailMessage.Attachments.Add(emailAttachment);
64+
65+
try
66+
{
67+
EmailSendOperation emailSendOperation = emailClient.Send(WaitUntil.Completed, emailMessage);
68+
Console.WriteLine($"Email Sent. Status = {emailSendOperation.Value.Status}");
69+
70+
/// Get the OperationId so that it can be used for tracking the message for troubleshooting
71+
string operationId = emailSendOperation.Id;
72+
Console.WriteLine($"Email operation id = {operationId}");
73+
}
74+
catch (RequestFailedException ex)
75+
{
76+
/// OperationID is contained in the exception message and can be used for troubleshooting purposes
77+
Console.WriteLine($"Email send operation failed with error code: {ex.ErrorCode}, message: {ex.Message}");
78+
}
79+
80+
```
81+
Run the application from your application directory with the `dotnet run` command.
82+
83+
```console
84+
dotnet run
85+
```

0 commit comments

Comments
 (0)