0

I want using api send mail at: https://learn.microsoft.com/en-US/previous-versions/office/office-365-api/api/version-2.0/mail-rest-operations#SendMessageOnTheFly

 *POST https://outlook.office.com/api/v2.0/me/sendmail*

{
  "Message": {
    "Subject": "Meet for lunch?",
    "Body": {
      "ContentType": "Text",
      "Content": "The new cafeteria is open."
    },
    "ToRecipients": [
      {
        "EmailAddress": {
          "Address": "[email protected]"
        }
      }
    ],
    "Attachments": [
      {
        "@odata.type": "#Microsoft.OutlookServices.FileAttachment",
        "Name": "menu.txt",
        "ContentBytes": "bWFjIGFuZCBjaGVlc2UgdG9kYXk="
      }
    ]
  },
  "SaveToSentItems": "false"
}

I try create Message Json by:

 var json=new {  "Message": { "Subject": "Meet for lunch?",.......,"SaveToSentItems": "false"};

But C# not allow.

How can create Json object Message in C#? Thank you.

2
  • Best to create a number of classes to represent the output then serialize it to JSON with Newtonsoft. I.e make a Message class with a Subject field and a Body object, etc. If there is a type that will not have consistent fields, use a dictionary. Commented Dec 12, 2019 at 3:52
  • I had try, but C# not allow create this name : public string @odata.type { get; set; } Commented Dec 12, 2019 at 4:00

4 Answers 4

3

As @ataboo suggested the best way is to create multiple classes that match the json structure and serialize the object to JSON.

Creating all the other classes should be straight forward except for the Attachment class. C# does not allow naming properties as "public string @odata.type { get; set; }"

NewtonSoft.Json has a solution for this problem. Create your class with any legal property name for the field witht the JsonProperty attribute as follows

public class Attachment
{
    [JsonProperty("@odata.type")]
    public string OdataType { get; set; }

    public string Name { get; set; }

    public string ContentBytes { get; set; }
}

JsonProperty attribute allows you to name properties as you want regardless of the json field name. Hope it helps.

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

1 Comment

@DT no problem.
1

Your JSON should be a string

var myObject = @"{
  ""Message"": {
    ""Subject"": ""Meet for lunch?"",
    ""Body"": {
      ""ContentType"": ""Text"",
      ""Content"": ""The new cafeteria is open.""
    },
    ""ToRecipients"": [
      {
        ""EmailAddress"": {
          ""Address"": ""[email protected]""
        }
      }
    ],
    ""Attachments"": [
      {
        ""@odata.type"": ""#Microsoft.OutlookServices.FileAttachment"",
        ""Name"": ""menu.txt"",
        ""ContentBytes"": ""bWFjIGFuZCBjaGVlc2UgdG9kYXk=""
      }
    ]
  },
  ""SaveToSentItems"": ""false""
}";

//create the object
var serializer = new JavaScriptSerializer();
var result = serializer.DeserializeObject(myObject); 

//access the values

var userId = result["Message"]["Subject"];

1 Comment

Thank you , but JavaScriptSerializer not found, i had add system.web.
1

The problem is in

var json=new {  "Message": { "Subject": "Meet for lunch?",.......,"SaveToSentItems": "false"};

So, you can not create JSon in code. You can create dynamic object:

var json=new {  Message = new {  Subject = "Meet for lunch?",......., SaveToSentItems = false};

And then you can convert it to Json-string.

But you should understand that C# is language with strong typing. And when you call this it is not like "Json-object in JavaScript". When you write this it will create an anonimus type and compile it. So it can seems the same as Json. But it's only syntactic sugar. And problem is in strong typing. For example in Python or JavaScript it will work as dynamic object.

It's better to use method like @codemonkeytony wrote

Comments

0

Copy the raw json from Fiddler, and paste it into this site: http://json2csharp.com/. It will create the C# classes you need in order to serialize back to the desired JSON format. If you are returning string and response format is set to Json, .NET automatically serializes your string. So after first serialization, .NET did another and it simply escaped all

2 Comments

Thank you,Seems like a lot of classes have to be created
C#: not allow set create this name: public string @odata.type { get; set; }

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.