Sunday, January 30, 2011

Send SMS using Java

In most of the forums i found this question.How to send SMS Using java.
For answering this question i spent few hours for you guys.


For sending SMS you need to have the following files
1) javax.comm.properties
2) comm.jar
3) win32com.dll
In this Link you can download the files.Let me know if the given link is broken.So that i will upload the files and change the link here.

Check the java_home twice before copy and paste the files.Because most of the problems occurred by pasting the files in wrong folder. 
javax.comm.properties:
Should falls under
   %JAVA_HOME%/lib
   %JAVA_HOME%/jre/lib
win32com.dll:
Should falls under
    %JAVA_HOME%/bin
    %JAVA_HOME%/jre/bin
    %windir%System32
comm.jar:
 Should falls under
     %JAVA_HOME%/lib
     %JAVA_HOME%/jre/lib/ext

Okay i believe you placed all files in respective folder correctly.
Wait guys i heard your mind voice,you are asking where is the java program.
How i will forget,here you go for java programs

import java.io.IOException;
import java.util.Enumeration;
import javax.comm.CommPortIdentifier;
import javax.comm.PortInUseException;
import javax.comm.SerialPort;
import javax.comm.UnsupportedCommOperationException;
public class SriramSMS {
public void sendsms()   
{  
 static Enumeration portList;
 static CommPortIdentifier portId;
 static SerialPort serialPort;
 static OutputStream outputStream;
 String messageString = "";
 String userName="";
 String phoneNumber="";
 Thread thread;

  try
  {
     userName = "periodicUpdates.blogspot.com";
     phoneNumber = "9876543210";
     messageString = " Hi." +userName+"welcome to this blog" ;

            String line1 = "AT+CMGF=1\r\n";
     String line2 = "AT+CMGS=" + phoneNumber + "\r\n"+messageString+ "\r\n";
     String line3 = "\u001A";

    // Here initialize the driver class
                  String driverName = "com.sun.comm.Win32Driver";
    CommDriver commdriver = (CommDriver)Class.forName(driverName).newInstance();
    commdriver.initialize();

    portList = CommPortIdentifier.getPortIdentifiers();
    while (portList.hasMoreElements())
     {       
      portId = (CommPortIdentifier) portList.nextElement();
      if(portId.getPortType() == CommPortIdentifier.PORT_SERIAL)   {
      System.out.println("SMS Sending........" + portId.getName());
      if((portId.getName().equals("COM1"))) {
       try
        {
         serialPort = (SerialPort) portId.open("SimpleWriteApp",10000);
         System.out.println("sms sending port--->"+serialPort);
         outputStream = serialPort.getOutputStream();
         serialPort.setSerialPortParams(230400,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
         outputStream.write(line1.getBytes());
         outputStream.write(line2.getBytes());
         outputStream.write(line3.getBytes());
         outputStream.flush();
         thread.sleep(5000);
         serialPort.close();
        } catch (PortInUseException portUse) {
         System.out.println("Port In Use " + portUse.toString());
        } catch (UnsupportedCommOperationException unsuppComOperExcep) {
         System.out.println("UnsupportedCommOperationException occured:::"+unsuppComOperExcep.toString());
        }  catch (IOException ioExcep) {
         System.out.println("Error occured while writing to output stream IOException" + ioExcep.toString());
        } catch(Exception excep) {
         System.out.println("Error writing message  with exception while closing " +excep.toString());
        }
     }
    }
   }
  } catch(Exception excep) {
   System.out.println("EXCEPTION raised while writing message@@" +excep.toString());
  }
 }
}
You got the program for sending sms through java program.

Want to Send Mail Using java program then click this Link and for more java program click this Link.
Do post your comments.

52 comments:

friend said...

will it work for a web app in unix box?

Sriram said...

i never attempted.sorry for saying dont know...

Fina Cempluk said...

nice post friends.

Sriram said...

Thank you...

Anonymous said...

good post bro :)

Sriram said...

Thanks brother.
Keep visit this blog.....

Anonymous said...

What to do If I got the following exception:
Error 2 opening registry key HARDWARE\DEVICEMAP\SERIALCOMM
Error 2 opening registry key HARDWARE\DEVICEMAP\SERIALCOMM

Sriram said...

can you please explain when you are facing this error.
And please tell me are you using smslib api or modem to send sms

Anonymous said...

Hey Sriram ur blog is excellent! :)

please tell me how to paste the javaxcomm files in windows...please provide step to step details as i am still a learner :(

Thanks in advance mate!

Anonymous said...

About the error:
I followed to your instructions and downloaded all 3 files and put them where you wrote.

Sriram said...

@Anonymous 1 : Thanks for reading.I believe I gave step by step details in this post itself.
Download all the three files using the link which I have given.
find java_home dir in your machine and paste the same in respective path which I have mentioned in the post.

Let me know if you face any problem.

Keep visit this blog

Sriram said...

@Anonymous 2: whether you connected the modem to your system.

Anonymous said...

is it necessary to connect the mobile modem to the computer?

Sriram said...

yes.
Its necessary to connect your modem or mobile device to your computer

kcer said...

can it be connected with usb and will it cost any $$$??

Sriram said...

Hi Kamilo-sama you can connect it through USB.
you have to pay few bucks for modem.

sebin joseph said...

why this exception occured at the running time of this application?

SMS Sending........COM3
SMS Sending........COM1
sms sending port--->COM1
UnsupportedCommOperationException occured:::javax.comm.UnsupportedCommOperationE
xception: Win32 Comm Driver: Error 0
SMS Sending........COM3
SMS Sending........COM1
Port In Use javax.comm.PortInUseException: Port currently owned by SimpleWriteApp

satyanarayana said...

UnsupportedCommOperationException occured:::javax.comm.UnsupportedCommOperationException: Win32 Comm Driver: Error 0

Pls tell solution for this error

Anonymous said...

This the first blog where I found What I want. Very nice Post.

By the way I have a question: I want to sens SMS via my java application. How can I connect my cellfone the make it work as a modem

Thanks again.

Anonymous said...

Hi guys, below is the error that program returns me

SriramSMS.java:14: cannot find symbol
symbol : class OutputStream
location: class SriramSMS
static OutputStream outputStream;
^

Please help me on it.

nani141443 said...

It can rectified by importing io package

nani141443 said...

I want to know whether we can send an sms to my mobile using a jsp program if yes how can i? I am really in need of it

Sriram said...

@@nani141443...
Yes of course its possible to send msg through jsp...
In the java program you can see the values are hardcoded, instead of that get input from the user and the same to java progm...
Create application, I will guide you to proceed further...
All the best...

Nilesh Patil said...

Nilesh Patil
error :
UnsupportedCommOperationException occured:::javax.comm.UnsupportedCommOperationException: Win32 Comm Driver: Error 0
This may be because baud rate is not correct.
One of the solution for this error is to check what parameter you are passing to setSerialPortParams(...) method

Arun said...

Hi Sriram, thanks for this nice post. I followed the mentioned steps and on running this program I am getting this error:
Error loading win32com: java.lang.UnsatisfiedLinkError: C:\Program Files\Java\jdk1.6.0_23\jre\bin\win32com.dll: Can't load IA 32-bit .dll on a AMD 64-bit platform
Error loading win32com: java.lang.UnsatisfiedLinkError: C:\Program Files\Java\jdk1.6.0_23\jre\bin\win32com.dll: Can't load IA 32-bit .dll on a AMD 64-bit platform
Please suggest. My operating system is Windows 64-bit.

Sriram said...

Arun
I believe problem is because of 64bit version.
Use 64bit dll...
Check in coderanch and stackoverflow, you will get more answer...

nani141443 said...

javax.comm.PortInUseException: Port currently owned by Unknown Windows Application

It is giving this error can i know why it is commming

Unknown said...

hi Sriram,

will it work on 64 bit machine

Regards,
Mahendra

Unknown said...

hi Sriram,

I am using 64 bit machine, an di am getting following error,[SMS Sending........COM3
SMS Sending........COM1
sms sending port--->COM1
UnsupportedCommOperationException occured:::javax.comm.UnsupportedCommOperationException: Win32 Comm Driver: Error 0
SMS Sending........COM3
SMS Sending........COM1
Port In Use javax.comm.PortInUseException: Port currently owned by SimpleWriteApp
]


what would be the solution sriram,


thanks and regards,

Naveen said...

Hi Sriram this is Naveen

I am new to java
i was compiled the code by u r steps.. but how can i run this and how can i send SMS

please help me
Thanks

Unknown said...

Firstly thanks for such a great tutorial. the best i found on sms so far but now i'm facing the weirdest problem. the whole code executes properly but nothing happens the message is not sent. I put a message after the serial port is closed to see if everything worked fine and it executes the code properly but message is not sent. Please help me with this.

YOGESH KHANDELWAL said...

Hi Sriram,
The link you provided is broken. So, please upload the files again and update the link.

Mahantesh said...

Hi Sriram thanks for post.

Particularly which modem want to buy and tell me approximate price of it.And can we send SMS from our own created JSP interface? and how???
Please kindly help out regarding this i am waiting.

Thaks

Abhinay said...

hi sriram...

nice work...

1)but the link u provided is not working as megaupload.com is blocked by fbi.

2) can i connect any mobile phone with data cable to send sms by using ur code??

v.k production said...

megauload if down due to copyrights! Can you upload these files somethere else and provide us with the link ?
NICE JOB ! Nice tutorial thanks

Pravin said...

the download link is not working can you please upload the smae and update the link please.

Subhasri said...

how do i implement this program in linux?? all the codes i can find are for windows systems....there's no win32com.dll in linux obviously, so how can i try this out?

Subhasri said...

how do implement this in linux system? there's no win32com.dll there....

Stanley Mungai said...

Hello I tried Your code and Pasted all the files as required. My Output is :
SMS Sending........COM10
SMS Sending........COM11
SMS Sending........COM6
SMS Sending........COM22
SMS Sending........COM5
SMS Sending........COM10
SMS Sending........COM11
SMS Sending........COM6
SMS Sending........COM22
SMS Sending........COM5

I have connected My Mobile Phone to My Laptop using USB cable. Anything else I need to do Because I cannot See Any sms Sent?

SeraBanglaKobita said...

Hello Sriram Sir,
I am new in java programmer. I follow ur blogs. Its really amazing.
The following msg show me when i deploy this code...

unable to create new urlConnection refused: connect

Unknown said...
This comment has been removed by the author.
Unknown said...

Do we have to do anything other than pasting the files and the code? I am getting errors.It can't resolve the java.comm references. How do i rectify this?

Anonymous said...

Link for downloading files are not working. Can you please look into this issue so that I can try your code!

Prabhu Rajendran said...

Link broken...

Prabhu Rajendran said...

Error loading win32com: java.lang.UnsatisfiedLinkError: no win32com in java.library.path

de said...

Great Article

Online Java Training | Java Training in Chennai | Java 360

Unknown said...

the link for the jar files is unavailable..... please set the link

mightyDragon said...

the link is unavailable. send the link please thanks.

SMS API PHP said...

Thanks for sharing. An SMS API is the programming software that lets you build your own personalized SMS sending and receiving channel. A smart messaging service can give your business a complete SMS API Java system.

Tech News said...


nice blog
devops training in bangalore
hadoop training in bangalore
iot training in bangalore
machine learning training in bangalore
uipath training in bangalore

Tech News said...

Visit Here - BIG DATA AND HADOOP TRAINING IN BANGALORE

Ashleel Londa said...

It was wonerful reading your conent. Thankyou very much. # BOOST Your GOOGLE RANKING.It’s Your Time To Be On #1st Page
Our Motive is not just to create links but to get them indexed as will
Increase Domain Authority (DA).We’re on a mission to increase DA PA of your domain
High Quality Backlink Building Service
Boost DA upto 15+ at cheapest
Boost DA upto 25+ at cheapest
Boost DA upto 35+ at cheapest
Boost DA upto 45+ at cheapest