Rate this page:

Thanks for rating this page!

We are always striving to improve our documentation quality, and your feedback is valuable to us. How could this documentation serve you better?

SMS and MMS Marketing Notifications with C# and ASP.NET MVC

Download the Code

Ready to implement SMS and MMS marketing notifications?

Excellent - you've come to the right place. Today we'll explore adding them to a C# and ASP.NET MVC application.

Here's how it all works at a high level:

  1. A possible customer sends an SMS to a Twilio phone number you advertise somewhere.
  2. Your application confirms that the user wants to receive SMS and MMS notifications from your company.
  3. An administrator or marketing campaign manager uses a web form to craft a message that will go out to all subscribers via SMS/MMS message.

Building Blocks

To get this done, we'll be working with the following Twilio tools:

Let's get started! Click the arrow below to move on to the next step of the tutorial.



In order to send out marketing notifications to a subscriber, we need to first provide the right model:

  • PhoneNumber will store where to send the notifications.
  • Subscribed will store who is opted in to receive notifications.
Loading Code Sample...

      Subscriber model


      Next, let's look at how we'll manage incoming messages.


      Handling Incoming messages

      This is the endpoint that will be called every time our application receives a message.

      It relies on a MessageCreator abstraction that produces the message that will be returned to the sender. It also neatly hides the complexity behind creating new subscribers.

      Loading Code Sample...

          The Subscribers Controller


          When a new user sends us a message, we need to manage adding him or her to our database. We'll explore that functionality next.

          Add Some New Users

          Creating New Subscribers

          We begin by getting a new customer's phone number from the incoming Twilio request. Next, we try to find a Subscriber model with that phone number.

          If there's no subscriber with this phone number, we create one, save it, and respond with a friendly message asking to reply "add" back. We are very careful to confirm they want to receive messages from our company before flagging them as subscribed.

          Loading Code Sample...

              Looking up a user and potentially adding them to the database


              And that's all we want at this step! We've created a Subscriber model to keep track of the people that have requested our messages. We also have saved them in the database when they text us for the first time.

              Next let's go further and look at how a user will modify his or her subscriber status.


              Manage Subscriptions

              We want to provide the user with two SMS commands to manage their subscription status: add and remove.

              These commands will toggle a boolean flag for their Subscriber record in the database, and will determine whether or not they want to receive messages from our marketing campaign. Because we want to respect our users' desires, we don't opt them in automatically. We are sure to have them confirm that they want to receive our messages.

              To make this happen, we will need to update the controller logic which handles the incoming text message to do a couple things:

              • If the user is a person already in the database, parse the message they sent to see if it's a command we recognize.
              • If it is a add or remove command, update her or his subscription status in the database.
              • If it is a command we don't recognize, send her or him a message explaining available commands.
              Loading Code Sample...

                  Dealing with incoming user commands


                  With the logic behind subscribing and unsubscribing implemented, let's move on to sending messages out.


                  Sending Notifications

                  On the server, we grab an incoming form's text and (optional) image URL, then loop through all Subscribers to call the method Send on our MessageSender domain object.

                  When the messages are on their way, we redirect the form submitter back to the same form with a ViewBag Property message containing feedback about the messaging attempt.

                  Loading Code Sample...

                      The Notifications controller


                      And how do we actually send the MMS or SMS messages? Glad you asked - let's go there next.

                      Send MMS and SMS Messages

                      Send SMS or MMS Notifications

                      Now that we have a list of subscribers for our awesome SMS and MMS content, we need to provide our marketing team some kind of interface to send out their perfectly crafted messages.

                      When the model object is loaded, it initializes a Twilio REST API client that it can be used to send SMS and MMS messages. The client requires your Twilio account credentials (an account SID and auth token), which can be found in the console:

                      console credentials

                      Next all we need to do is call MessageResource.CreateAsync in order to send our message. The Twilio Message API call requires a From and To parameter, and either Body or a MediaUrl (or both).

                      Loading Code Sample...

                          A Notifications service


                          And that's a wrap! You should now be able to easily integrate this awesome feature into your own application. On the next pane, we'll look at some other easy-to-implement features.

                          What Else Can I Build?

                          Where to Next?

                          That's it! We've just implemented a an opt-in process and an administrative interface to run an SMS and MMS marketing campaign all through C# and ASP.NET MVC. Now all you need is killer content to share with your users via text or MMS... and you're on your own for that.

                          Twilio and .NET go together very well - perhaps you'd enjoy these other tutorials?

                          IVR: Phone Tree

                          Build an automated phone tree with C# and Twilio's powerful features.

                          Automated Survey

                          Don't let valuable feedback slip away - conduct instant surveys with voice or SMS.

                          Did this help?

                          Thanks for checking this tutorial out!

                          We'd love to hear from you - Tweet to us @twilio with what you're building!

                          Jennifer Aprahamian Agustin Camino Andrew Baker Paul Kamp Kat King Hector Ortega

                          Need some help?

                          We all do sometimes; code is hard. Get help now from our support team, or lean on the wisdom of the crowd browsing the Twilio tag on Stack Overflow.

                          Loading Code Sample...