0

I have problem sending an email with an attachment. Without attachment it works. If I use the same function and add an attachment to the message I get the following error message:

Code: ErrorRequiredPropertyMissing Message: Required property is missing. ClientRequestId: 2af....

I am using MS Graph v4.0.30319

public static async Task<String> SendMyMailAsync()
{
    try
    {
        var FromSender = new Microsoft.Graph.Recipient()
        {
            EmailAddress = new Microsoft.Graph.EmailAddress
            {
                Address = "[email protected]"
            }
        };
        byte[] contentBytes = System.IO.File.ReadAllBytes(@"C:\Users\me\Desktop\Test.pdf");
        String bs64 = Convert.ToBase64String(contentBytes);
        var attachment = new FileAttachment
        {

            AdditionalData = new Dictionary<string, object>()
            {
                {"@odata.type","#microsoft.graph.fileAttachment"}
            },
            //ODataType = "#microsoft.graph.fileAttachment",
            ContentType = "application/pdf",
            Name = "Test.pdf",
            ContentBytes = Convert.FromBase64String(bs64),
            IsInline = false,
            Size = bs64.Length,
            ContentId = "TestMail",
            LastModifiedDateTime = DateTime.Now,
            Id = "HSDJHEWuDSjfkkfGt",
        };


        Microsoft.Graph.Message message = new Microsoft.Graph.Message
        {
            Sender = FromSender,
            From = FromSender,
            Subject = "Mail no1",
            Importance = Microsoft.Graph.Importance.Normal,
            Body = new Microsoft.Graph.ItemBody
            {
                ContentType = Microsoft.Graph.BodyType.Html,
                Content = "Hello World",
            },
            ToRecipients = new List<Microsoft.Graph.Recipient>()
            {
                new Microsoft.Graph.Recipient
                {
                    EmailAddress = new Microsoft.Graph.EmailAddress
                    {
                        Address = "[email protected]"
                    }
                }
            },
            Attachments = new MessageAttachmentsCollectionPage(),
        };

        // -- If I comment this out I can send the mail without error but without attachment----
        message.Attachments.Add(attachment);
        message.HasAttachments = true;
        //-----------------------------------------------------------------


        var request = graphClient.Me.SendMail(message, true); 
        // Messages[message.Id].Send();// SendMail(message, null);
        await request.Request().PostAsync();

        return "Mail send OK";
    }
    catch (ServiceException ex)
    {
        Console.WriteLine($"Error getting events: {ex.Message}");
        return "Send mail error";
    }
}

2 Answers 2

1

The below code works perfectly fine for me

        public static async Task<String> SendMyMailAsync()
        {
            try
            {
                var FromSender = new Microsoft.Graph.Recipient()
                {
                    EmailAddress = new Microsoft.Graph.EmailAddress
                    {
                        Address = "[email protected]"
                    }
                };
                byte[] contentBytes = System.IO.File.ReadAllBytes(@"C:\Users\Shiva\Desktop\sample.pdf");
                String bs64 = Convert.ToBase64String(contentBytes);
                var attachment = new FileAttachment
                {

                    AdditionalData = new Dictionary<string, object>()
                    {
                        {"@odata.type","#microsoft.graph.fileAttachment"}
                    },
                    ContentType = "application/pdf",
                    Name = "Test.pdf",
                    ContentBytes = Convert.FromBase64String(bs64),
                    IsInline = false,
                    Size = bs64.Length,
                    ContentId = "TestMail",
                    LastModifiedDateTime = DateTime.Now,
                    Id = "HSDJHEWuDSjfkkfGt",
                };


                Microsoft.Graph.Message message = new Microsoft.Graph.Message
                {
                    Sender = FromSender,
                    From = FromSender,
                    Subject = "Mail no1",
                    Importance = Microsoft.Graph.Importance.Normal,
                    Body = new Microsoft.Graph.ItemBody
                    {
                        ContentType = Microsoft.Graph.BodyType.Html,
                        Content = "Hello World",
                    },
                    ToRecipients = new List<Microsoft.Graph.Recipient>()
                    {
                        new Microsoft.Graph.Recipient
                        {
                            EmailAddress = new Microsoft.Graph.EmailAddress
                            {
                                Address = "[email protected]"
                            }
                        }
                    },
                    Attachments = new MessageAttachmentsCollectionPage(),
                };
                message.HasAttachments = true;
                message.Attachments.Add(attachment);
                var request = graphClient.Me.SendMail(message, true);
                await request.Request().PostAsync();

                return "Mail send OK";
            }
            catch (ServiceException ex)
            {
                Console.WriteLine($"Error getting events: {ex.Message}");
                return "Send mail error";
            }
        }

You need to make sure that you have the PDF file path correctly specified and the fromSender variable will obviously be yours as you are calling me/sendMail. If you want to send mail from other user's mailbox then you need to have client credential flow setup so that it can give you an App-only token which should have required permissions and you need to change the call something like this var request = graphClient.Users["userid/UPN"].SendMail(message, true);.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the answer. When I debug the program I see that the PDF file is loaded. When I send the mail without attachment it works for me too. Only when I add the attachment in the mail I get the following error message: Code: ErrorRequiredPropertyMissing Message: Required property is missing. ClientRequestId: 2af... It does not seem to be a permission problem. The PDF file is also loaded correctly. Which MS Graph version do you use? @shiva-keshav-varma
1

I have just updated the MS Graph preview version from 4.0.0-preview.1 to 4.0.0-preview.2 Now everything works as expected I think it was a bug in preview.1

2 Comments

Great to hear. Always i would suggest you to make use of the latest Graph API versions, as most of the reported the issues are fixed.
You can select your own answer as correct. You get points and it closes out the question as complete.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.