How to send simple message to Microsoft Teams channel using csharp?

Solution 1:

Using C#, one way to send messages to teams channel is using webhook url. Below code will help sending message to teams.

string webhookUrl = "<enter Teams webhook url>";
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(webhookUrl);
Message body = new Message();
body.text = "Hello World";
string serializeJson = JsonConvert.SerializeObject(body);
StringContent content = new StringContent(serializeJson,Encoding.UTF8,"application/json");
_ = await client.PostAsync(client.BaseAddress, content);

Declare a Message class

public class Message
{
    public string text { get; set; }
}

Webhook url can be set up by creating an Incoming webhook connection on desired Teams channel: enter image description here

Solution 2:

You can send messages to Teams channel using webhook. You can post messages by setting up incoming webhook within channel. Please look at Post external request in Teams with incoming webhook. But you should specify your requirement you can achieve this with the Bot. A bot is also helpful to have conversation within Teams channel. Bot supported [Personal, Team, GroupChat] scope. Please go through the Conversation basic for more information how bot works within different scopes.

Solution 3:

There are a few ways to accomplish this, depending on what your other broader requirements might be. Options are, for instance, webhooks or a bot, but please see my answer at Microsoft Teams: Is at all possible to create a app/connector/bot for broadcasting?