question

WardHorsfall-2061 avatar image
0 Votes"
WardHorsfall-2061 asked WardHorsfall-2061 commented

Send an email in c# with category already set.

Hi,

How can I send a message using C# and using a category already set - so when it comes into the Inbox the category is already available.

So for example assuming I have a master category called "C1"

Then I can have some code to send a message and mark it as "C1"

Then read inbox and see "C1" category.

Thanks

Ward

microsoft-graph-mail
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

Sheena-MSFT avatar image
0 Votes"
Sheena-MSFT answered WardHorsfall-2061 commented

Hi @WardHorsfall-2061,

Using Graph API you can first create a draft message with category property.

Please find the below c# code for the same:

 GraphServiceClient graphClient = new GraphServiceClient( authProvider );
    
 var message = new Message
 {
     Subject = "Did you see last ",
     Importance = Importance.Low,
     Categories = new List<String>()
     {
         "Blue category"
     },
     Body = new ItemBody
     {
         ContentType = BodyType.Html,
         Content = "They were <b>awesome</b>!"
     },
     ToRecipients = new List<Recipient>()
     {
         new Recipient
         {
             EmailAddress = new EmailAddress
             {
                 Address = "test1@raiinfy.onmicrosoft.com"
             }
         }
     }
 };
    
 await graphClient.Me.Messages
     .Request()
     .AddAsync(message);


It will create a draft message with category as shown below

201061-category2.png

Then you have to send mail using the message id created in the above step.

If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have further questions about this answer, please click "Comment".




category2.png (21.7 KiB)
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.



Hi Sheena-MSFT,

Thanks for your answer. I get the error message:

{"Code: BadRequest\r\nMessage: Write requests (excluding DELETE) must contain the Content-Type header declaration.\r\nInner error:\r\n\tAdditionalData:\r\n\ .....

Any suggestions ?

From this call:

await graphClient.Me.Messages
.Request()
.AddAsync(message);

I notice the message is created in the drafts folder - however Message.Id is null at this stage.

Any suggestions on how to fix?

Thanks,
Ward


0 Votes 0 ·
Sheena-MSFT avatar image Sheena-MSFT WardHorsfall-2061 ·

Are you including the header "Content-Type.?

0 Votes 0 ·

Hi Sheena-MSFT,

Thanks for your response. I don't think so but am unsure on how to do that.

Here is the code I am using below.

Thanks,

Ward

 async Task Send_Test_Message(GraphServiceClient graphClient, string email_address)
 {
     var message = new Message
     {
         Subject = "Did you see last ",
         Importance = Importance.Low,
         Categories = new List<String>()
      {
          "Blue category"
      },
         Body = new ItemBody
         {
             ContentType = BodyType.Html,
             Content = "They were <b>awesome</b>!"
         },
         ToRecipients = new List<Recipient>()
      {
          new Recipient
          {
              EmailAddress = new EmailAddress
              {
                  Address = email_address
              }
          }
      }
     };
    
     try
     {
         await graphClient.Users[email_address].Messages
             .Request()
             .AddAsync(message);
     }
     catch (Exception ex)
     {
    
     }
    
     try
     {
         await graphClient.Users[email_address].Messages[message.Id]
             .Send()
             .Request()
             .PostAsync();
     }
     catch (Exception ex)
     {
    
     }
 }


0 Votes 0 ·