Hi @Sandeep K,
It seems that you're not using Graph's SDK, it is recommended that you use GraphClient
to send requests. Like the code below:
var scopes = new[] { "https://graph.microsoft.com/.default" };
// Values from app registration
var clientId = "Your ClienId";
var tenantId = "Your TenantId";
var clientSecret = "Your Clent Secret";
var file = "Your Path\\MailWork.txt";
byte[] fileArray = System.IO.File.ReadAllBytes(@file);
// using Azure.Identity;
var options = new ClientSecretCredentialOptions
{
AuthorityHost = AzureAuthorityHosts.AzurePublicCloud,
};
// https://learn.microsoft.com/dotnet/api/azure.identity.clientsecretcredential
var clientSecretCredential = new ClientSecretCredential(
tenantId, clientId, clientSecret, options);
var graphClient = new GraphServiceClient(clientSecretCredential, scopes);
var requestBody = new SendMailPostRequestBody
{
Message = new Message
{
Subject = "Meet for lunch?",
Body = new ItemBody
{
ContentType = BodyType.Text,
Content = "The new cafeteria is open.",
},
ToRecipients = new List<Recipient>
{
new Recipient
{
EmailAddress = new EmailAddress
{
Address = "******@contoso.onmicrosoft.com",
},
},
},
Attachments = new List<Attachment>
{
new FileAttachment
{
OdataType = "#microsoft.graph.fileAttachment",
Name = "attachment.txt",
ContentType = "text/plain",
ContentBytes = fileArray,
},
},
},
};
// To initialize your graphClient, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=csharp
await graphClient.Users["<userEmail>"].SendMail.PostAsync(requestBody);
If the reply is helpful, please click Accept Answer and kindly upvote it. If you have additional questions about this answer, please click Comment.