Tutorials

How to Send SMS through Java code in Windows

JAVA SMS SEND CODE Working

Now a days SMS is major interactive thing in major mobile application development platform like Android, IOS, Blackberry, Symbain & Bada etc. Developers are re-imagine the way of SMS used previously. So intelligent auto sender SMS apps like ebuddy XSMS, Nimbuzz makes peoples life easier. Now a days SMS is nothing but day to day notification service. If you are a developer then here is a good chance to explore your idea using sms.For this you don’t require to BUY SMS gateways and all.You can use your mobile as middleware to avail free SMS sending service. All actions will be taken from your cell phone & you will  charge only according to your carrier SMS rates 🙂

JAVA SMS SEND CODE Working

Requirements-

1)      Multimedia mobile with its drivers installed on your computer,you must connect it through USB mode.

2)      rstxSerial.dll file (please copy this dll to your ‘C:WindowsSystem32’ folder)

3)      RXTXcomm.jar file

4)      IDE- Netbeans or Easy Eclipse

Java-SMS-Code

We are all known Netbeans and Eclipse IDE’s but Easy Eclipse for GUI purpose.

Please download required jar and dll files from following link:

Download SMS Sending JAR & DLL files

AT commands-

  • AT commands are instructions used to control a modem. AT is the abbreviation of ATtention. AT commands are also known as Hayes AT commands. There are different views to understand the meanings of “AT”. Some call it “Attention Telephone”, whereas others interpret it as “Attention Terminal” commands.
  • For execution purpose we need hyperterminal. For this run command “hypertrm.exe” .
  • If you are running this command first time you will get the following window –

Send SMS using Java Code Settings

  • Just fill first two options, click OK,  after that you will get this window-

Hyper Termianl Java code setting

 

  • If you are using Windows 7, copy these 3 files from Windows XP:-

1. C: Program Files/Windows NT/hypertrm.exe

2. C: WINODWS/system32/hypertrm.dll

3. C: WINODWS/Help/hypertrm.chm

Create folder “HyperTerminal” in C:/Program Files and paste above files in this folder. Double click on hypertrm.exe and you will get HyperTerminal in Windows 7.

Connection description Window-

  • Type any name you want and click OK.
  • Then see the option of “Connect using”, chose COM port on your mobile device gets connected.
  • (For that Go to Control Panel -> Phone and Modem Options. Then click the Modems tab. In the list box, you can see which COM port the mobile phone is connected to.)
  • Choose it and click OK.
  • You will get following windows, select “Bits per second option as 9600”. Click OK.

COM port properties-

Send SMS Java Code Comp port settings

Type following commands one by one-

  • AT  (hit enter).

(if you don’t see anything on the window, then go to File->Properties->Settings->ASCII Setup->Select check box of “Echo typed characters locally”->Click OK->Click OK.)

  • You will get a response as OK.
  • AT+CMGF=1 (hit enter).
  • AT+CMGS=”**********” ( Replace * by 10 digit mobile number to which you want to send message).
  • You will get greater than symbol like this “ > “
  • Type your message and press ctrl+z key , hit enter after this.
  • The message will be sent.

Download Java Source Code Project from following link

1. Send SMS using Java Source Code

2. For that check following window

On Hyperterminal  command execution

Java SMS send code configration Hyper Terminal

Enter Mobile Number in Double-Quotes ” “

Please close this window after using commands, because when this instance is present, we can not use it for another purpose.

Snapshots of Application To Send SMS –

(In Eclipse) :-

Send SMS using Java application interface

How to import existing project into eclipse-

  • Extract project zip file which you downloaded.
  • On eclipse, click File->Import->General->Existing project into workspace->Browse option for Select root directory-> (Suppose you extract file on the Desktop then Desktop will be the root directory , also choose ” Copy projects into workspace”)->click Finish.
  • After that you have to change the COM port number in “GUIToSendMessage.java ” file on line number 87. I have written there “COM15”. You have to change it according to your settings.
  • Run GUIToSendMessage. java file. You will see above window. Type mobile number and enter a message and click on the Send button.  The message will be sent.

How to import existing project into Netbeans-

  • Extract project zip file which you downloaded.
  • On Netbeans, click File->Import Project->Eclipse Project->Chose  option of “ Import Project Ignoring Project Dependencies ”->Browse for “Project To Import”->(here you import your project)->Browse for “Destination Folder”->(It must be your Netbeans default workspace where you store  all your Netbeans projects)->Click “Finish”.
  • After that you have to change the COM port number in “GUIToSendMessage.java ” file on line number 87. I have written there “COM15”. You have to change it according to your settings.
  • Run GUIToSendMessage. java file. You will see above window. Type mobile number and enter a message and click on the Send button.  The message will be sent.

If you are facing any problem with this code then let us know our team will help you to fix your problems ! if you develop any cool application using this code then do share with us our readers appreciate your development ! 🙂

Amit
Amit is a Software Engineering & Android developer,who really want to share his Android tips & idea to android learner.Hope it will help you in future.Follow Him On
You may also like
Thumb Blog Writing Skills
4 Ways to Improve Your Writing Skills as a Blogger
Photography Blogs
Four Reasons Why Photo Blogs are Better than the Text Blogs
52 Comments
  • Abhipriya Jan 19,2017 at 5:49 pm

    Hello,
    The output is the following:
    gnu.io.CommPortIdentifier@1e11ba0
    gnu.io.CommPortIdentifier@13361d1
    COM4: PORT_OWNED
    ATZ
    AT+
    OK
    AT+CMGS=”8056288264″
    HI
    OK
    however we arent getting a sms. help us out.

  • Abhipriya Jan 19,2017 at 5:45 pm

    Hello,
    This code has been a huge help, but there also exists a problem. The output has the following:
    actionPerformed()
    Stable Library
    =========================================
    Native lib Version = RXTX-2.2pre2
    Java lib Version = RXTX-2.1-7
    WARNING: RXTX Version mismatch
    Jar version = RXTX-2.1-7
    native lib Version = RXTX-2.2pre2
    gnu.io.CommPortIdentifier@1e11ba0
    gnu.io.CommPortIdentifier@13361d1
    COM4: PORT_OWNED
    ATZ
    AT+OK
    AT+CMGS=”**********”
    HI
    OK
    we did use a proper mobile number(the * is placed instead of a mobile number) but however we have not received any message so far. It would be considerate if you help us out.

    • Saurabh Jan 19,2017 at 6:24 pm

      Please do check your COMP port is working properly or not, I suggest to change port if required and try again

  • akhilesh shukla Jul 8,2016 at 9:47 am

    Hi Please Help Me i Didi all the suggestions in the comment bUt Not a single Success

    actionPerformed()
    Stable Library
    =========================================
    Native lib Version = RXTX-2.1-7
    Java lib Version = RXTX-2.1-7
    gnu.io.CommPortIdentifier@17984d4
    gnu.io.CommPortIdentifier@15dd901
    gnu.io.CommPortIdentifier@b8e1bb
    COM7: PORT_OWNED
    actionPerformed()
    gnu.io.CommPortIdentifier@1d7c25f
    gnu.io.CommPortIdentifier@13374d0
    gnu.io.CommPortIdentifier@1a514dd
    gnu.io.PortInUseException: Unknown Application
    at gnu.io.CommPortIdentifier.open(CommPortIdentifier.java:354)
    at sendMessage.Connect.connect(Connect.java:81)
    at sendMessage.GUIToSendMessage$2.actionPerformed(GUIToSendMessage.java:92)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$500(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

    • Saurabh Jul 10,2016 at 4:13 pm

      Hi Akhilesh, what error msg you are getting and what problem ur facing? Did u checked your port is configured properly ? Will surely troubleshoot your problem please provide more insights

  • GEITSPR Jun 30,2016 at 5:52 am

    The application send only one message, if someone wants to send a second one it says that it doesn’t initialise the card. Why?

    • Saurabh Jul 2,2016 at 12:08 am

      Hi GEITSPR, you have to tweak code as and keep in loop to iterate or simply making daemon thread of same can help you. You are getting this msg coz ur retrying to use same instance. Let me know if you need any more help

  • sadeem
    Twitter:
    May 9,2016 at 9:50 am

    hi , its work great but plz tell me how i catch the exception or any response if i put wrong cell no ?

    • Saurabh May 11,2016 at 3:38 pm

      Put some java-script validation in input field of MOBILE number.
      You can simply get it by googling, Have a look into this script
      http://goo.gl/ZfUpS

      • sadeem
        Twitter:
        May 12,2016 at 7:00 am

        thanks for your reply but dear i am more concern about how to get the AT command response in a string as i try this way of getting response but some time its work but some time its not , i am unable to figure out the problem please help me in this regards , I shall be very thankful to you .
        public void send(String cmd) {
        try {
        outputStream.write(cmd.getBytes());
        BufferedReader reader = new BufferedReader(new InputStreamReader (serialPort.getInputStream()));
        String line = “”;
        line = reader.readLine();
        while (!line.equals(null)) {
        if (line.equalsIgnoreCase(“OK”) || (line.indexOf(“+CMGS”) != -1) || line.equalsIgnoreCase(“ERROR”)) {
        output = line;
        System.out.println(“—-command—“+cmd+”——output—-“+output);

        }
        line = reader.readLine();
        }
        }
        catch (Exception e) {
        }
        }

  • Rakesh Kumar Dec 24,2015 at 7:27 pm

    Sir everything working properly but sms not delivered i think send(message + ‘\032’); is not executing.

    • Saurabh Dec 27,2015 at 11:22 pm

      yes it may due to this

  • BananaMan May 12,2015 at 4:42 pm

    Hello, i am using win 64bit, java 8 and newest rxtx library, using an Android mobile phone with its drivers via usb to make it act as a COM device and I get the following error:
    run:
    actionPerformed()
    gnu.io.CommPortIdentifier@5f8232b8
    gnu.io.CommPortIdentifier@270be6b1
    COM3: PORT_OWNED

    OK
    AT+CREG?
    +CREG: 0,1
    OK
    AT+CMGF=1
    OK
    AT+CMGS=”+37063931891″ // +370 being my country code, if i dont add country code i get the same error.
    +CMS ERROR: 330

    Any idea of what am I doing wrong and how to fix this error?
    Thank you.

  • percival Mar 17,2015 at 8:21 am

    good day sir..just want to ask for any solution why it doesn’t work on my laptop..rxtxserial error..thanks

  • Usman Mar 3,2015 at 10:32 pm

    and this aplication not supported more then 150 char 🙁

  • Usman Mar 1,2015 at 11:25 pm

    Error is please help me

    run:
    actionPerformed()
    WARNING: RXTX Version mismatch
    Jar version = RXTX-2.2pre1
    native lib Version = RXTX-2.2pre2
    gnu.io.CommPortIdentifier@4b35caf9
    COM3: PORT_OWNED
    ATZ

    OK
    AT+CREG?

    • Saurabh Mar 2,2015 at 6:53 pm

      Please install latest java 1.7 64bit/32-bit version according to your machine.
      Let me know if you face any issue
      Saurabh recently posted..Current App Development Trends You Don’t want to MissMy Profile

      • Usman Mar 2,2015 at 11:21 pm

        i hav downloaded from official website java 1.7 new SE version but he still show actionPerformed()
        WARNING: RXTX Version mismatch
        Jar version = RXTX-2.2pre1
        native lib Version = RXTX-2.2pre2
        gnu.io.CommPortIdentifier@1c265af5
        COM3: PORT_OWNED
        ATZ

        OK

        Thanks for previous reply and thanks for next reply

        • Saurabh Mar 3,2015 at 12:35 am

          HI Usman,

          goto http://rxtx.qbang.org/wiki/index.php/Download
          and download rxtx-2.1-7-bins-r2.zip from “Binary” column.
          goto C:\Users\Downloads\rxtx-2.1-7-bins-r2.zip\rxtx-2.1-7-bins-r2
          and copy RXTXcomm to
          C:\Program Files\processing-1.5.1\java\lib\ext
          goto C:\Users\Downloads\rxtx-2.1-7-bins-r2.zip\rxtx-2.1-7-bins-r2\Windows\i368-mingw32
          and copy both rxtxParallel.dll and rxtxSerial.dll to
          C:\Program Files\processing-1.5.1\java\bin

          This will help you. Let me know

      • Usman Mar 3,2015 at 10:04 pm

        can you please give me your facebook or twiter id please ..
        last night even i hav compatibility error my sms deliver successfully but now again
        at command ok but sending sms error
        thanks for reply
        im waiting

      • ZIN Mar 17,2015 at 8:11 am

        please help me..

        this is the error

        java.lang.UnsatisfiedLinkError: C:\Program Files (x86)\Java\jdk1.6.0_04\bin\rxtxSerial.dll: Can’t load AMD 64-bit .dll on a IA 32-bit platform thrown while loading gnu.io.RXTXCommDriver
        Exception in thread “AWT-EventQueue-0” java.lang.UnsatisfiedLinkError: C:\Program Files (x86)\Java\jdk1.6.0_04\bin\rxtxSerial.dll: Can’t load AMD 64-bit .dll on a IA 32-bit platform

  • Usman Mar 1,2015 at 2:58 pm

    im Using 64bit Windows 8.1 this is not working its show 32bit error ?
    any help for 64bit ?

    • Saurabh Mar 2,2015 at 6:48 pm

      Please install Java 1.7 64bit version and also include 64bit rxtxSerial.dll inside system32 folder.you can get it from google.
      I also suggest to refer previous comments of this thread.Many people face same issue and fixed.
      Let me know if it will not get resolved

    • Saurabh Mar 2,2015 at 6:50 pm

      Install Java 1.7 64 bit version and include rxtxSerial.dll 64 bit DLL inside System32 folder,You can get it from Google easily.
      Even I recommended to refer previous comments for reference
      Saurabh recently posted..Current App Development Trends You Don’t want to MissMy Profile

  • James Dec 8,2014 at 10:32 pm

    I have run the problem successfully but it says the rxtxserial.dll is not for 64 bit platform. do you have for 64 bit platform. i;m in rush because need to show to my lecturer the method. thank you

    • Saurabh Dec 13,2014 at 1:37 am

      Hi James,
      Its versioning issue.

      the JVM you are running is 64-bit but the DLL rxtxSerial.dll is 32-bit. To resolve, either:

      Obtain 64-bit rxtxSerial.dll, or
      Install and use 32-bit JVM with the current rxtxSerial.dll

      If you have Java 7, then you need to put the files here for 64bit version:
      C:\Program Files\Java\jre7\bin\rxtxSerial.dll
      C:\Program Files\Java\jre7\lib\ext\RXTXComm.jar

      The 64-bit RXTX files are here:
      rxtxSerial.dll [http://goo.gl/sFoyfO]
      RXTXComm.jar [http://goo.gl/iCw53g]

      let me know if you need any help
      Saurabh recently posted..Disable WhatsApps Blue Check Mark Message Read Confirmation My Profile

  • Jose Nov 14,2014 at 5:39 am

    Hello:
    First at all thanks a lot for your post. It is very useful. I have a problem. When run the program i not received any messages, but when i debug step by step the messages is sent. I think i due to some lost thread, i tried to solve it but i can´t. I hope someone can help. Thanks again,
    Regards, Jose

    • Saurabh Nov 18,2014 at 5:10 pm

      Hi Jose,

      can you please sent me your code, will look into it and let you know. I think its happening because of Comp port availability.
      Saurabh recently posted..Disable WhatsApps Blue Check Mark Message Read Confirmation My Profile

      • Jose Nov 18,2014 at 7:57 pm

        Hello Saraubh:
        Thanks for your replay. I am using the same code in the example. I found the problem is for the time for execution the AT command in the modem. The pc speed is more than fast than the execution in the modem. I solved it with Thread.sleep(500); in the sent method. But i am worried because I don´t know if this solution is not good when the traffic will be high.

        This is my code:

        public void send(String cmd) {
        try {
        outputStream.write(cmd.getBytes());
        Thread.sleep(500);
        } catch (IOException | InterruptedException e) {
        e.printStackTrace();
        }
        }

        Thanks again,
        Regards, Jose

  • haritha Sep 7,2013 at 11:12 am

    Hi
    Thank you . I have deployed this code into the eclipse. but can u please tel me what is COM Port number?

    • Saurabh Sep 8,2013 at 12:43 am

      Hi haritha COMP port is nothing but port no. that you are using terminal option.
      Let us know if you need any help

  • P R Karthik Feb 10,2013 at 9:47 pm

    HI ..

    Program is very good and running well..But , is there a way to send multiple recepients or a kind of a loop ..??

    Thanks..

    • Saurabh Feb 11,2013 at 11:24 pm

      Nope bro, You have to code it accordingly or you have to purchase SMS gateway. Above java sms sending code only work for single sms service

  • sruthi Jan 24,2013 at 5:44 pm

    hi,
    Actually you post is very nice and helpfull..
    i am doing my project thst to send the sms from PC connected with mobile/gsm modem to another mobile….but when i run your code the messege not sending the output is..

    actionPerformed()
    Stable Library
    =========================================
    Native lib Version = RXTX-2.1-7
    Java lib Version = RXTX-2.1-7
    gnu.io.CommPortIdentifier@f3d6a5
    COM1: PORT_OWNED

    what will i do.can you help me

    • Saurabh Jan 27,2013 at 2:08 am

      Hi Sruthi, can u please tell us which OS ur using,I mean 32-bit or 64-bit? Win xp or win7? A terminal issue may occur if you are using different lib. Your inputs will help us to solve your query

      • BED Feb 19,2013 at 8:36 am

        I also use OS win 64 bits. I have the same result as @sruthi

        • Saurabh Feb 20,2013 at 12:26 am

          Its depend on you how you will code it

  • Bhushan Ladde Dec 28,2012 at 12:44 pm

    I did all set up properly. I solve all the issues also. I get an sms also. But now I was facing a small problem. SMS is come Like this,

    mare tATZ
    AT+CREG?
    AT+CMGF=1AT+CMGS=”mobile number”
    Message

    Now the problem is that , I can not able to send only a message. AT commands also send along with that Message. And sometimes, It is not giving FULL sms. It is escaping some part of the SMS. What should I do?

    Thanks in advance….

    You did a great job.

    Please reply if you know.

  • Bhushan Ladde Dec 26,2012 at 2:58 pm

    I did all the setup but i was get a following Error:-

    java.lang.UnsatisfiedLinkError: no rxtxSerial in java.library.path thrown while loading gnu.io.RXTXCommDriver
    Exception in thread “AWT-EventQueue-0” java.lang.UnsatisfiedLinkError: no rxtxSerial in java.library.path

    where is the exactly problem.?
    Thanks in advance.

    • Saurabh Dec 26,2012 at 5:11 pm

      Hi bhushan,
      Look like some liblary files are missing in ur SDk kindly download and include it into ur project check this http://goo.gl/8RuvP & this too http://goo.gl/IA1oY. I already tested my code and its working fine .Let me know if any help needed

      • Bhushan Ladde Dec 28,2012 at 12:48 pm

        Yup, It is working fine.

      • Pushpendu Paul Feb 7,2013 at 1:35 pm

        when i run this code it gives me error like unsuppported major.minor version 51. can you tell me why this problem occur and what i have to do for this ?

        • Saurabh Feb 7,2013 at 11:45 pm

          Hi try to install BOTH jre / jdk of same version. i.e JRE 1.7/ JRE 1.7

    • Amit Dec 26,2012 at 6:20 pm

      Can you include file rxtxSerial.dll in your System32 folder. Please check that and give reply.

  • Nickolsint*****martin Jul 31,2012 at 4:24 pm

    hi

    first of all i would say that you described it in very easey language and paragraph by paragraph with diagram.

    i will use this code after completed java. i am persuing java

    thanks

    • Saurabh Aug 1,2012 at 3:18 pm

      Sure let me know about it 🙂

  • Sibin Jul 31,2012 at 10:48 am

    Thanks nice post.I am not a good programmer , didn’t understand clearly , let me give some more setails
    Sibin recently posted..How to check whether the sites you visit daily contains virus/malwaresMy Profile

    • Saurabh Jul 31,2012 at 2:38 pm

      Hi to understand it clearly kindly refer attached files !

Leave Your Comment

Your Comment*

Your Name*
Your Webpage

CommentLuv badge

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Pin It on Pinterest

Share This