Level up your Twilio API skills in TwilioQuest, an educational game for Mac, Windows, and Linux. Download Now


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 Java and Servlets

Ready to implement SMS and MMS marketing notifications with Java and Servlets?


  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 to push to all subscribers via SMS/MMS message.

Walmartが「Value of the day」販促キャンペーンの一環として顧客に特売情報を毎日送信した方法をご覧ください。



  • TwiML and the <Message> verb: We'll use TwiML to manage interactions initiated by the user via SMS.
  • Messages Resource: We will use the REST API to broadcast messages out to all subscribers.

さぁ、はじめましょう! 下のボタンをクリックして、チュートリアルの次の手順に移動してください。

The Subscriber Model

Before send out marketing notifications to a subscriber, we need to start with the perfect model:

  • phoneNumber stores where to send the notifications.
  • subscribed stores if a subscriber is active (only an active subscriber will receive notifications).

        The Subscriber model


        Let's see how to handle incoming messages.

        See Message Handling

        Handle Incoming Messages


        We begin by getting the user'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 our newest user to send an SMS with "add" as a command. This is done to confirm that they want to receive messages from us.


              Register a new subscriber in the database or apply a user command


              We've created a Subscriber model to keep track of the people that want our messages. Now, let's look at the options we provide our users for opting in or out.

              Managing User Subscriptions

              ユーザーの購読状態の管理に、addremove という2つのSMSコマンドを提供することにします。 

              These commands will toggle a boolean flag for their Subscriber record in the database and will determine whether they get our marketing messages. Because we want to respect our user's preferences, we don't opt them in automatically - rather, we have them confirm that they want to receive SMSes or MMSes from us.


              • If it is a add or remove command, create/update a Subscription with the right status in the database.
              • もし認識しないコマンドだった場合は、利用可能な2つのコマンドを説明するメッセージを送信します。

                    Validate and execute a subscriber's command


                    Let's now take a look at how to send notifications.

                    Send Out Some Notifications

                    Send Notifications Out

                    When a form is submitted, we first grab the message text and/or image URL. Second, we loop through all Subscribers and call the method send on our Sender domain object.

                    When the messages are on their way, render the index page with a success message and delight the marketing team.


                          Send a message to all active subscribers


                          Let's take a closer look at how to send SMS or MMS notifications.


                          When the model object is loaded, it creates a Twilio REST API client that 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:


                          Next, all we need to do is create a message using the MessageCreator class. The Twilio Message API call creator requires To, From and Body parameters. MediaUrl is an optional parameter.


                                Use Twilio Java client to send a message


                                これで終わりです。SMS および MMS マーケティングキャンペーンを実行するためのオプトインプロセスと管理インターフェイスを実装しました。これで、あと必要なのはテキストまたは MMS でユーザーと共有するキラーコンテンツのみとなりました。


                                Love Java? So do we at Twilio! Here are two other excellent tutorials you should check out:

                                SMS・MMS 通知

                                SMS 通知は、重要なイベントが発生した場合に迅速にアラートを送信するのにうってつけの方法です。この例では、Web アプリケーションでエラーが発生した場合に、リストに載っている関係者に SMS 通知を送信する方法を示します。


                                Don't let valuable insights slip away - survey your customers with Voice and SMS.


                                Thanks for checking out this tutorial... tweet @twilio to let us know how you did and what you're building!

                                Kevin Whinnery Agustin Camino Andrew Baker Paul Kamp Kat King
                                Rate this page:


                                誰しもが一度は考える「コーディングって難しい」。そんな時は、お問い合わせフォームから質問してください。 または、Stack Overflow でTwilioタグのついた情報から欲しいものを探してみましょう。