Sms sending with java

Send SMS and MMS Messages in Java

In this tutorial, we’ll show you how to send SMS and MMS messages from Java using Twilio’s Programmable SMS. The code snippets in this guide target Java SDK 8 or higher, and make use of the Twilio Java Helper Library.

While you can send text-only SMS messages almost anywhere on the planet, sending media is currently only available in the US and Canada. Learn more in this support article.

Sign up for (or log in to) your Twilio account

If you have a Twilio account and Twilio phone number with SMS (and MMS if possible) capabilities, you’re all set! Feel free to jump straight to the code.

Before you can send messages, you’ll need to sign up for a Twilio account and purchase a Twilio phone number.

If you’re brand new to Twilio, you can sign up for a free trial account to get started. Once you’ve signed up, head over to your Console and grab your Account SID and your Auth Token. You will need those values for the code samples below.

Читайте также:  Javascript form input list

Get a phone number with SMS (and MMS) capabilities

Sending messages requires a Twilio phone number with SMS capabilities. If you don’t currently own a Twilio phone number with SMS capabilities, you’ll need to buy one. After navigating to the Buy a Number page, check the ‘SMS’ box and click ‘Search’:

Buy A Number

If you live in the US or Canada and also wish to send MMS messages, you can select the ‘MMS’ box. When viewing the search results, you can see the capability icons in the list of available numbers:

Click Buy Button

Find a number you like and click «Buy» to add it to your account.

If you’re using a trial account, you will need to verify your personal phone number via the console so that you can test sending SMSes to yourself.

Send an SMS Message via the REST API with the Java Helper Library

Sending an outgoing SMS message requires sending an HTTP POST to the Messages resource URI. Using the helper library, you can create a new instance of the Message resource and specify the To, From, and Body parameters for your message.

The first phone number in the example below is the To parameter (your Twilio number), and the second phone number is the From parameter (your mobile number).

Send an SMS using the Programmable SMS API

If you want to send a message to several recipients, you could create an array of recipients and iterate through each phone number in that array.

You can send as many messages as you like, as fast as you like and Twilio will queue them up for delivery at your prescribed rate limit. See our guide on how to Send Bulk SMS Messages for more tips.

Send a Message in Java with Media (MMS)

You may wish to send outgoing MMS using Twilio. To send an MMS, you also make an HTTP POST request to the Messages resource but this time specify a parameter for the URL of media, such as an image.

We’ll add a line to our code from the above example that sets the MediaUrl for the message.

While you can send text-only SMS messages almost anywhere on the planet, sending media is currently only available in the US and Canada.

Send a Message with an Image URL

The setMediaUrl() method call in this code tells Twilio where to go to get the media we want to include. This must be a publicly accessible URL — Twilio will not be able to reach any URLs that are hidden or that require authentication.

Just as when you send a simple SMS, Twilio will send data about the message in its response to your request. The JSON response will contain the unique SID and URI for your media resource:

When the Twilio REST API creates your new Message resource, it will save the image found at the specified media url as a Media resource. Once created, you can access this resource at any time via the API.

You can print this value from your Java code to see where the image is stored. Add the following line to the end of your Example.java file to see your newly provisioned Media URI:

System.out.println(message.getSubresourceUris().get("media"));

What’s next?

Want to build more messaging functionality into your Java Application?

Check out these in-depth resources to take your programmatic messaging a step further:

  • Monitor the status of your message to confirm its delivery.
  • Learn how to reply to an SMS sent to your Twilio phone number.
  • Learn how to manage message state to turn individual messages into a true SMS conversation.
  • Dig into the details with our API reference documentation for Messages.
  • Sending high-volume messages? Check out our messaging services.

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 by visiting Twilio’s Stack Overflow Collective or browsing the Twilio tag on Stack Overflow.

Источник

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

Code to Send SMS From PC to Mobile using Internet

hoai/java-send-sms-with-smslib

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

Send SMS messages in Java using smslib

On the next occasion, I have a task to send SMS I find out and example code 1 application to send SMS to share =)).

1. Ways to send SMS in Java

I find out, there are 2 main ways to send SMS and use the following:

Method 1: Use API provided by 3rd party

This way you register an account with a provider like: twilio, nexmo

alt text

After having an account, in Java code you will send the request to the server of that provider (request including account, sms message to send, destination phone number) -> Service provider will determine if a valid account If valid, it will send sms to the destination phone number in the request.

Advantages: This method is quite easy to do (the provider lib is short, easy to understand, with detailed instructions, developers need not care about hardware devices, mobile waves to send sms messages . only need 1 line internet transmission is okay)

Method 2: Use your sim card to send sms.

This way we need to connect sim card to Java application via devices like USB 3G / 4G (dongle or dcom)

Insert sim card into USB 3G / 4G, connect USB 3G / 4G to computer.

alt text

In Java app, we will connect to USB 3G / 4G and send the command asking it to send sms messages.

Advantage: this way is free (sim still loses sms deposit), there is no fee or service charge, just as you text on the phone. A lot of messages, many out-of-network messages =))

Need hardware devices such as sim card, USB 3G / 4G (this is also not very expensive). It’s not very stable because you have to manage the hardware, for example, when I call the send command, another guy also accesses that 3G / 4G USB and fails. In case you deploy the web server, it is impossible to tell the server / host provider, «Can you install me a USB 3G / 4G server on the server: o» Installation is harder than the first one, I am struggling to find out the code and the code can run. 2. Send SMS in Java using smslib In this article, I will guide how to send sms messages in the way 2.

First, you have to prepare 1 USB 3G / 4G and 1 sim card to send sms messages (some 3G / 4G sim cards are only for internet access, so no SMS messages can be sent; SMS messaging is possible, no internet connection required.

JDK 6 is installed or newer.

Config smslib as follows: extract the downloaded smslib folder.

  1. Copy RXTXcomm.jar into folder: %JAVA_HOME%/lib and %JAVA_HOME%/jre/lib/ext
  2. Copy rxtxParallel.dll and rxtxSerial.dll into folder: %JAVA_HOME%/bin and %JAVA_HOME%/jre/bin
  3. Create Java project and add 2 smslib.java and log4j.jar libraries to the project
  4. (You can replace RXTXcomm.jar with comm.jar, replace rxtxParallel.dll and rxtxSerial.dll with win32com.dll: but it can only run on 32bit java)

%JAVA_HOME% is the path to the JDK installation folder

These .dll files allow jvm to connect to devices

For example, I install jdk in folder: C:\Program Files\Java\jdk1.8.0_131.

Insert USB 3G / 4G into the computer and check its port:

alt text alt text Send SMS messages in Java using smslib

Send SMS messages in Java using smslib

Note, in many cases you have to plug USB 3G / 4G into the computer + install software with USB 3G / 4G, it will show up Ports (COM & LPT) and information of USB 3G / 4G in this section.

After the computer receives the port, you check the USB 3G / 4G to receive the sim, then turn off the software that comes with 3G 3G / 4G or if Java sends the command it will report that the Port is being used by other application.

After completing the above steps, we proceed with the code: Code

System.out.println("stackjava.com: send sms by Java."); System.out.println(Library.getLibraryDescription()); System.out.println("Version: " + Library.getLibraryVersion()); //SerialModemGateway gateway = new SerialModemGateway(id, comPort, baudRate, manufacturer, model) SerialModemGateway gateway = new SerialModemGateway("model.com5", "COM5", 115200, null, null); gateway.setInbound(true); gateway.setOutbound(true); Service.getInstance().addGateway(gateway); Service.getInstance().startService(); System.out.println(); System.out.println("Thong tin modem:"); System.out.println("Nha san xuat: " + gateway.getManufacturer()); System.out.println("Model: " + gateway.getModel()); System.out.println("Serial No: " + gateway.getSerialNo()); System.out.println("SIM IMSI: " + gateway.getImsi()); System.out.println("Signal Level: " + gateway.getSignalLevel() + " dBm"); System.out.println(); String message = "stackjava.com \n demo send sms trong Java voi smslib"; // bạn thay xxx bằng số điện thoại người nhận // lưu ý: số điện thoại có định dạng +mã quốc gia + sdt // Ví dụ: số điện thoại của mình là 01644444444 thì mình sẽ để là +841644444444 String sdt = "xxx"; OutboundMessage msg = new OutboundMessage(sdt, message); Service.getInstance().sendMessage(msg); System.out.println(msg); Service.getInstance().stopService(); System.out.println("Finish!"); 
stackjava.com: send sms by Java. SMSLib: A Java API library for sending and receiving SMS via a GSM modem or other supported gateways. This software is distributed under the terms of the Apache v2.0 License. Web Site: http://smslib.org Version: 3.5.1 log4j:WARN No appenders could be found for logger (smslib). log4j:WARN Please initialize the log4j system properly. log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info. Thong tin modem: Nha san xuat: +CGMI: MTK1 Model: +CGMM: MTK2 Serial No: 352247046996717 SIM IMSI: ** MASKED ** Signal Level: -67 dBm =============================================================================== > ------------------------------------------------------------------------------- Gateway Id: null Message Id: 0 Message UUID: 289099ca-5e58-4ea6-9778-3719bf18a87e Encoding: 7-bit Date: Thu Nov 23 18:01:15 SGT 2017 SMSC Ref No: 16 Recipient: +84xxxxxxxx Dispatch Date: Thu Nov 23 18:01:18 SGT 2017 Message Status: SENT Failure Cause: NO_ERROR Validity Period (Hours): -1 Status Report: false Source / Destination Ports: -1 / -1 Flash SMS: false Text: stackjava.com demo send sms trong Java voi smslib PDU data: 737A78BC5687ED61D7F8DD062940E472FB0D9A97DD64D0BC3D07D1E56FF719A40CDBC320FB3B0D9AB7E7ECB418 Scheduled Delivery: null =============================================================================== Finish! 

alt text

Note: SerialModemGateway’s initialization function is:

new SerialModemGateway (id, comPort, baudRate, manufacturer, model) in which the most important is that the comPort must be correct, and the id you set is not to be identical to the SerialModemGateway. Usually id, manufacturer and model can be null.

That’s it, now you can apply it to send sms systems veritify, spam sms (just kidding, don’t do it) . =))

Thanks for following the article.

Источник

Оцените статью