Facing issue while sending mail using Microsoft Graph API With Attachment in c#

Sandeep K 20 Reputation points
2024-01-16T12:34:00.61+00:00

I am trying to send mail using Microsoft Graph API With Attachment as per this article where I have explained the issue with error code 400 - https://stackoverflow.com/questions/77825549/facing-issue-while-sending-mail-using-microsoft-graph-api-with-attachment-in-c-s Can some one suggest here what I am missing? Thanks

Microsoft Security | Microsoft Graph
0 comments No comments
{count} votes

Accepted answer
  1. Chen Li 160 Reputation points
    2024-01-17T08:03:35.4633333+00:00

    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.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

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.