FileAttachment ContentType not working in MS Graph 5 API

Jason Sheats 6 Reputation points
2023-12-18T19:25:34.46+00:00

No matter what I put for ContentType, it doesn't come through as a PDF. It will open with Acrobat if you PICK it and you can read it just fine, but it doesn't auto-launch the preferred program for a PDF and it doesn't show as a PDF in the email (or a todo attachment for that matter).

                                IsInline = true,

Breaks everything as far as the attachment goes. It's nowhere to be found.

               var requestBody = new SendMailPostRequestBody
                {
                    Message = new Message
                    {
                        Subject = "Email",
                        Body = new ItemBody
                        {
                            ContentType = BodyType.Html,
                            Content = "TEST EMAIL.",
                        },
                        ToRecipients = new List<Recipient>
                        {
                            new Recipient
                            {
                                EmailAddress = new EmailAddress
                                {
                                    Address = "******@test.com",
                                },
                            },
                        },
                        Attachments = new List<Attachment>
                        {
                            new FileAttachment
                            {
                                OdataType = "#microsoft.graph.fileAttachment",
                                Name = "Hopefully this is a PDF",
                                ContentBytes = File.ReadAllBytes("C:\\genericpdf.pdf"),
                                ContentType = "application/pdf",
                            },
                        },

                    },
                };

                await graphClient.Users[ConfigurationManager.AppSettings["sendingAccount"].ToString()].SendMail
                    .PostAsync(requestBody);
Microsoft Security | Microsoft Graph
{count} votes

2 answers

Sort by: Most helpful
  1. Jason Sheats 6 Reputation points
    2023-12-18T19:38:33.7833333+00:00

    Name = "Hopefully this is a PDF",

    has to be
    Name = "Hopefully this is a PDF.pdf",

    Has to have an extension of .pdf for it to work. Just trial and error.

    0 comments No comments

  2. Md Asif Muztaba 325 Reputation points Microsoft External Staff
    2023-12-18T22:00:57.1933333+00:00

    It seems you’re trying to send an email with a PDF attachment using the Microsoft Graph API. From your code, it looks like you’re setting the ContentType to "application/pdf", which is correct for a PDF file.

    However, based on the information you provided, it appears that the issue might be with the Name property of the FileAttachment. The Name property should include the file extension. So instead of "Hopefully this is a PDF", it should be "Hopefully this is a PDF.pdf".

    Here’s the corrected part of your code:

    Attachments = new List<Attachment>

    {

    new FileAttachment

    {

    OdataType = "#microsoft.graph.fileAttachment",

    Name = "Hopefully this is a PDF.pdf",

    ContentBytes = File.ReadAllBytes("C:\\genericpdf.pdf"),

    ContentType = "application/pdf",

    },

    },

    This should ensure that the attached file is recognized as a PDF in the email. Please give this a try and see if it resolves your issue.
    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    0 comments No comments

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.