Sending bounced mail to specific mail address using Microsoft Graph API in ASP.NET C#

Pradeep VK 0 Reputation points
2024-04-09T07:01:02.0066667+00:00

When sending a mail if the mail gets failed/bounced, those failed/bounced back mail needs to be delivered to the specific mentioned mail address instead of from address using Microsoft Graph API in ASP.NET C#.

I tried to add bounce mail address but in Microsoft Graph there was no option to enter bounced mail address.

----Below is the code that I used to send bounced mail to specific address(******@email.com) using ChilKat. How to achieve this using Microsoft Graph API in asp.net core -----

     Chilkat.Email mail = new Chilkat.Email();
     mail.From = "******@email.com";
     mail.AddTo("", "******@email.com");
     mail.BounceAddress = "******@email.com";
     mail.Subject = "Test Mail";
     mail.Body = "This is a Test Message";
     MailMan mman = new MailMan();
     mman.SmtpHost = "smtp.office365.com";
     mman.SmtpPort = 587;
     mman.SmtpUsername = "******@email.com";
     mman.SmtpPassword = "******";
     bool mailSend = mman.SendEmail(mail);
     if (mailSend == true)
         Console.WriteLine("Message Sent Succesfully");
     else
         Console.WriteLine(mman.LastErrorText.ToString());

--Below code is used for Microsoft Graph API. How to add bounce mail address (like mail.BounceAddress="****@email.com"** in Chilkat) for the below code--

ClientSecretCredential credential = new(tenantId, clientId, clientSecret);
            GraphServiceClient graphClient = new(credential);
            var requestBody = new SendMailPostRequestBody
            {
                Message = new Message
                {
                    Subject = MailSubject,
                    Body = new ItemBody
                    {
                        ContentType = BodyType.Text,
                        Content = MailContent,
                    },
                    From = new Recipient { EmailAddress = new EmailAddress { Address = FromMailID } },
                    ToRecipients = new List<Recipient>
                        {
                        new Recipient
                        {
                            EmailAddress = new EmailAddress
                            {
                                Address = ToMailID,
                            },
                        },
                        },
                },
                SaveToSentItems = true                    
            };
           await GraphClient.Me.SendMail.PostAsync(requestBody);
           Console.WriteLine("Mail Sent Successfully");
Developer technologies | ASP.NET | ASP.NET Core
Microsoft Security | Microsoft Graph
Developer technologies | C#
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.