.NET Quickstart: Replying to a Message

Let's walk through creating your first Twilio SMS application: Mobile Monkey.

Prerequisites

  1. You'll need an SMS-enabled Twilio phone number for receiving messages. You can use a free [Twilio trial account][2] and get your own dedicated phone number.

  2. You will also need a web host that lets you host web applications. There are lots of web hosting services that will host your application for free or for a few dollars a month.

Hello Monkey, SMS Edition

  1. Let's say your web server answers HTTP requests at companyfoo.com. Create a file called sms-hello-monkey.cshtml in the document root. The URL http://companyfoo.com/sms-hello-monkey.cshtml will be the SMS URL for your phone number.
  2. Browse over to the [phone number configuration page][6] in your account on the Twilio website. Click on the phone number you wish to use for the demo. If you don't have a phone number, you can purchase one using the "Buy a Number" button at the top of the page. Click the "Edit" link next to the phone number you would like to use. Next, check the "SMS" box, and paste the URL of the cshtml file (e.g. http://companyfoo.com/sms-hello-monkey.cshtml) in the "URL" box. Hit save and this phone number will now be set up to receive SMS messages.
  3. Copy the following code sample into the sms-hello-monkey.cshtml file on your web server. The C# Helper Library tells Twilio to reply with an SMS that says "Hello, Mobile Monkey" whenever Twilio receives an SMS for your phone number.
  4. Now text message your Twilio number! If everything went according to plan, you should receive a text message in response saying, "Hello, Mobile Monkey".
Loading Code Samples...
Language
SDK Version:
  • 4.x
  • 5.x
Format:
  • TwiML
// In Package Manager, run:
// Install-Package Twilio.Mvc -DependencyVersion HighestMinor

using System.Web.Mvc;
using Twilio.Mvc;
using Twilio.TwiML;
using Twilio.TwiML.Mvc;

public class SmsHelloMonkey : TwilioController
{
    [HttpPost]
    public ActionResult Index(SmsRequest request)
    {
        var response = new TwilioResponse();
        response.Message("Hello, Mobile Monkey");
        return TwiML(response);
    }
}
// In Package Manager, run:
// Install-Package Twilio.AspNet.Mvc -DependencyVersion HighestMinor

using System.Web.Mvc;
using Twilio.AspNet.Mvc;
using Twilio.TwiML;

public class SmsHelloMonkey : TwilioController
{
    [HttpPost]
    public ActionResult Index()
    {
        var messagingResponse = new MessagingResponse();
        messagingResponse.Message("Hello, Mobile Monkey");

        return TwiML(messagingResponse);
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<Response>
    <Message>
        Hello, Mobile Monkey
    </Message>
</Response>
Reply SMS using Twilio

Hello Monkey Phone Message

Respond with an MMS

Now modify your sms-hello-monkey.cshtml on your server as follows specifying a URL of an image. If necessary, restart your server, then text your Twilio number again. You should receive a text message in response saying, "Hello, Mobile Monkey", but this time an image will be included!

MMS messages can only be sent and received by numbers having MMS capability. You can check the capabilities of numbers in the account portal or query the Available Phone Numbers resource to search for Twilio numbers that are MMS enabled.
Loading Code Samples...
Language
SDK Version:
  • 4.x
  • 5.x
Format:
  • TwiML
// In Package Manager, run:
// Install-Package Twilio.Mvc -DependencyVersion HighestMinor

using System.Web.Mvc;
using Twilio.Mvc;
using Twilio.TwiML;
using Twilio.TwiML.Mvc;

public class MmsHelloMonkey : TwilioController
{
    [HttpPost]
    public ActionResult Index(SmsRequest request)
    {
        var response = new TwilioResponse();
        response.Message("Hello, Mobile Monkey",
            new [] { "https://demo.twilio.com/owl.png" },
            null);
        return TwiML(response);
    }
}
// In Package Manager, run:
// Install-Package Twilio.AspNet.Mvc -DependencyVersion HighestMinor

using System.Web.Mvc;
using Twilio.AspNet.Mvc;
using Twilio.TwiML;

public class SmsController : TwilioController
{
    [HttpPost]
    public ActionResult Index()
    {
        var message = new Message();
        message.Body("Hello, Mobile Monkey");
        message.Media("https://demo.twilio.com/owl.png");

        var messagingResponse = new MessagingResponse();
        messagingResponse.Message(message);

        return TwiML(messagingResponse);
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<Response>
    <Message>
        <Body>Hello, Mobile Monkey</Body>
        <Media>https://demo.twilio.com/owl.png</Media>
    </Message>
</Response>
Reply MMS using Twilio

Note that is optional if you are sending one or more <Media> elements.

You can even send multiple images by adding more <Media> elements to your response. Check out the official docs for more details.

Next: Replying to Text Messages by Name »

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.

1 / 1
Loading Code Samples...
SDK Version:
  • 4.x
  • 5.x
Format:
  • TwiML
// In Package Manager, run:
// Install-Package Twilio.Mvc -DependencyVersion HighestMinor

using System.Web.Mvc;
using Twilio.Mvc;
using Twilio.TwiML;
using Twilio.TwiML.Mvc;

public class SmsHelloMonkey : TwilioController
{
    [HttpPost]
    public ActionResult Index(SmsRequest request)
    {
        var response = new TwilioResponse();
        response.Message("Hello, Mobile Monkey");
        return TwiML(response);
    }
}
// In Package Manager, run:
// Install-Package Twilio.AspNet.Mvc -DependencyVersion HighestMinor

using System.Web.Mvc;
using Twilio.AspNet.Mvc;
using Twilio.TwiML;

public class SmsHelloMonkey : TwilioController
{
    [HttpPost]
    public ActionResult Index()
    {
        var messagingResponse = new MessagingResponse();
        messagingResponse.Message("Hello, Mobile Monkey");

        return TwiML(messagingResponse);
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<Response>
    <Message>
        Hello, Mobile Monkey
    </Message>
</Response>
SDK Version:
  • 4.x
  • 5.x
Format:
  • TwiML
// In Package Manager, run:
// Install-Package Twilio.Mvc -DependencyVersion HighestMinor

using System.Web.Mvc;
using Twilio.Mvc;
using Twilio.TwiML;
using Twilio.TwiML.Mvc;

public class MmsHelloMonkey : TwilioController
{
    [HttpPost]
    public ActionResult Index(SmsRequest request)
    {
        var response = new TwilioResponse();
        response.Message("Hello, Mobile Monkey",
            new [] { "https://demo.twilio.com/owl.png" },
            null);
        return TwiML(response);
    }
}
// In Package Manager, run:
// Install-Package Twilio.AspNet.Mvc -DependencyVersion HighestMinor

using System.Web.Mvc;
using Twilio.AspNet.Mvc;
using Twilio.TwiML;

public class SmsController : TwilioController
{
    [HttpPost]
    public ActionResult Index()
    {
        var message = new Message();
        message.Body("Hello, Mobile Monkey");
        message.Media("https://demo.twilio.com/owl.png");

        var messagingResponse = new MessagingResponse();
        messagingResponse.Message(message);

        return TwiML(messagingResponse);
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<Response>
    <Message>
        <Body>Hello, Mobile Monkey</Body>
        <Media>https://demo.twilio.com/owl.png</Media>
    </Message>
</Response>