Send SMS messages with C# & .net6

Send SMS messages with C# & .net6

In modern web applications, the business may require to send SMS to end-users in various scopes like Alerting the users, acknowledging the users, or even sending OTP. Sending as SMS is the easiest way and also can be implemented easily in .NET applications using Twilio.

Setting Up Twilio Account

Twilio two-way SMS and MMS messages allow you to carry on a conversation by both sending and receiving text and multimedia messages. This can enable a contact center agent to engage in an SMS-based chat with a customer, or let customers re-schedule appointments via text and receive automated responses.

  1. Try / Create a free account –?https://www.twilio.com/try-twilio

No alt text provided for this image

2. Choose the Service which we want like SMS, WhatsApp, Voice and etc from the option.

No alt text provided for this image
No alt text provided for this image

3. Get a Virtual Twilio Phone Number to Send & Recieve SMS

No alt text provided for this image

Sending SMS in.NET Application

For this article example, let’s create a simple Api application that has one post object with two fields, one is to enter the Receiver Phone Number and another is for Message Content.

project files:

No alt text provided for this image

Install the required Twilio NuGet package, for that navigate to Tools and search Twilio:

No alt text provided for this image


C# Code Implementation

Create a service and needed method for sending SMS. Now run the application and enter the receiver’s phone number and message content.

API:

No alt text provided for this image

SendSmsRQ:

No alt text provided for this image

Services and extension method for registrate:

SmsSenderService:

No alt text provided for this image

AppSettingsAdapter:

No alt text provided for this image

ExtensionMethod:

No alt text provided for this image

When we have all the services made in appsettings.json we enter the required values:

No alt text provided for this image

Demo

We launch the application and call the SendSMS route via swagger or postman:

No alt text provided for this image

After which an SMS message arrives with the value we sent:

No alt text provided for this image

Conclusion

We can use sms publishing for different purposes:

1. Marketing - to inform your customers about a new promotion or similar

2. As a warning - let's create a hosted job that every 5 seconds checks if a link is available and if not sends a message to the worker on duty about the problem ...

In this simple article, we discussed how to send SMS using Twilio in C# and the .NET applications. We will discuss more on this in upcoming articles. I hope you all liked this article and please feel free to share your feedback in the comment section.

Please find the full sample solution here:?https://github.com/matijakatadzic/TwilloSmsSender.Demo.Net6

要查看或添加评论,请登录

Matija Katadzic的更多文章

  • Run and manage periodic background tasks in ASP.NET Core 6 with C#

    Run and manage periodic background tasks in ASP.NET Core 6 with C#

    Sometimes your web app needs to do work in the background periodically e.g.

  • Mapping Experiment in .net core- AutoMapper, ExpressMapper, Mapster & Manual mapping

    Mapping Experiment in .net core- AutoMapper, ExpressMapper, Mapster & Manual mapping

    Nuget package that we need: You can read more in official pages: AutoMapper - https://automapper.org/, BenchmarkDotNet…

  • What is a Decorator Pattern?

    What is a Decorator Pattern?

    The decorator pattern (also known as Wrapper) is a structural design pattern and it allows developers to dynamically…

  • What is Strategy Design Pattern?

    What is Strategy Design Pattern?

    Let’s say you are implementing an online eCommerce shop checkout page and you have been told to implement a feature…

  • What is Adapter Design Pattern?

    What is Adapter Design Pattern?

    The Adapter design pattern is one of the most common and useful structural design patterns. It allows objects with…

  • What is a Builder Pattern?

    What is a Builder Pattern?

    Builder Design Pattern allows us to separate the construction of a complex object from its representation so that we…

  • What is Observer Design Pattern?

    What is Observer Design Pattern?

    The Observer is a behavioral design pattern and it allows some objects (known as subjects or publishers or observables)…

  • What’s a design pattern?

    What’s a design pattern?

    Design patterns are typical solutions to commonly occurring problems in software design. They are like pre-made…

  • What is gRPC?

    What is gRPC?

    What is gRPC? gRPC (gRPC Remote Procedure Calls) is an open source remote procedure call (RPC) system initially…

社区洞察

其他会员也浏览了