How do we send custom headers in Azure Email Service while sending a mail via C# code?

Nanda, Karan 0 Reputation points
2024-02-29T09:49:50.17+00:00

I would like to send custom headers via mail to ACS (we're looking for feature parity with mailgun). Eventually I would like to know if a user clicked on a mail, which environment it was from (prod or staging), was it sent as a marketing campaign or not etc.

Azure Communication Services
Azure Communication Services
An Azure communication platform for deploying applications across devices and platforms.
686 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,288 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Sedat SALMAN 13,165 Reputation points
    2024-03-02T11:58:33.1333333+00:00

    Azure ACS documentation may help you

    https://learn.microsoft.com/en-us/dotnet/api/azure.communication.email.emailmessage.headers?view=azure-dotnet

    according to the documentation

    Headers property of EmailMessage class will solve your problem

    here is an example code block

    using System;
    using System.Collections.Generic;
    using Azure.Communication.Email;
    using Azure.Communication.Email.Models;
    class Program
    {
        static async Task Main(string[] args)
        {
          
            
    		EmailClient emailClient = new EmailClient(connectionString);
    
     
            
    		var emailContent = new EmailContent("Welcome to ACS Email APIs.") 
    		{ 
    			PlainText = "This email message is sent from ACS Email.", 
    			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>" 
    		}; 
    
    		// Create the EmailMessage 
    		var emailMessage = new EmailMessage( 
    		senderAddress: "donotreply@xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx.azurecomm.net" 
    		resource recipientAddress: "emailalias@contoso.com" 
    		content: emailContent);
    
    
            var customHeaders = new Dictionary<string, string>
            {
                { "X-Environment", "staging" },
                { "X-Campaign", "marketing" }
            };
    
       	
            emailMessage.Headers = customHeaders;
    
            
    		EmailSendOperation emailSendOperation = emailClient.Send(WaitUntil.Completed, emailMessage); 
    		
     		Console.WriteLine($"Email Sent. Status = {emailSendOperation.Value.Status}");
    
        }
    }
    
    
    
    0 comments No comments