microsoftgraph / microsoftgraph/msgraph-sdk-dotnet

SendMail fails when any recipient collections are explicitly assigned a null value

Open
#2,230 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Needs: Attention :wave:
Dominant language
C#
Stars
789
Forks
264
Avg merge
15h 17m
Merged PRs (30d)
3

Description

Describe the bug
When using SendMail.PostAsync to send a Message with either the ToRecipients, CcRecipients or BccRecipients properties explicitly assigned a null value, a Microsoft.Graph.Models.ODataErrors.ODataError exception with the following message is thrown: A null value was found for the property named 'bccRecipients', which has the expected type 'Collection(microsoft.graph.recipient)[Nullable=True]'. The expected type 'Collection(microsoft.graph.recipient)[Nullable=True]' cannot be null but it can have null values.

To Reproduce

Minimal code to reproduce error (example with BccRecipients explicitly set to null):

var body = new SendMailPostRequestBody
{
    Message = new Message
    {
        From = new Recipient
        {
            EmailAddress = new EmailAddress { Address = "someuser@mail.com" }
        },
        ToRecipients = new List<Recipient>
        {
            new Recipient { EmailAddress = new EmailAddress { Address = "someuser@mail.com" }}
        },
        BccRecipients = null,
        Subject = "Test",
        Body = new ItemBody
        {
            ContentType = BodyType.Text,
            Content = "Test"
        }
    }
};

var graphClient = CreateClient(); // helper to create a GraphServiceClient
try
{
    await graphClient
        .Users["someuser@mail.com"]
        .SendMail
        .PostAsync(body);
    Console.WriteLine("Success");
}
catch (Exception ex)
{
    Console.WriteLine("Error: " + ex.Message);
}

Expected behavior
The Message is supposed to be successfully sent with the recipients properties not causing issues with a null value, as the properties are nullable.

Client version
Microsoft.Graph 5.36.0 running on .NET 8.0.

Additional context
Removing the BccRecipients = null assignment in the Message example above (i.e. leaving the BccRecipients property as default) will cause the Message to be able to be sent successfully. However, my use case requires me to dynamically create the recipient values, so it would be convenient if I was able to just assign a null value directly to these properties in my logic. This used to work in v4.x but it broke when I upgraded the SDK to v5.36.0.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the SendMail.PostAsync entry point and the SendMailPostRequestBody example, focusing on null ToRecipients, CcRecipients, and BccRecipients values. Run the provided reproduction against Microsoft.Graph 5.36.0 on .NET 8.0; done means a message sends successfully when these nullable properties are explicitly assigned null.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
api
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.