How to send SMS using Java

I want to send SMS to a mobile phone from a web application, is this possible? How to do it?

+7
java web-applications sms
source share
8 answers

You can use this free sample Java program to send SMS from your PC using a GSM modem connected to your computer to your COM port. You also need to download and install Java comm api from Sun.

This program needs the following java files to work.

  • SerialConnection.java (this file is used to connect to your COM port from your java program)

  • SerialConnectionException.java (This file is designed to handle serial connection exceptions in your Java program)

  • SerialParameters.java (this program is used to set the properties of your COM port to connect to your COM port from your java program)

  • Sender.java (This is a program that implements runnable and sends SMS using a serial connection)

  • SMSClient.java (This java class is the main class that can be created in your own Java program and called to send SMS. In turn, this program will use all of the above four files to send your SMS message).

Download sample Java SMS program files

/* * * A free Java sample program * A list of java programs to send SMS using your COM serial connection * and a GSM modem * * @author William Alexander * free for use as long as this comment is included * in the program as it is * * More Free Java programs available for download * at http://www.java-samples.com * * * Note: to use this program you need to download all the 5 java files * mentioned on top * */ public class SMSClient implements Runnable{ public final static int SYNCHRONOUS=0; public final static int ASYNCHRONOUS=1; private Thread myThread=null; private int mode=-1; private String recipient=null; private String message=null; public int status=-1; public long messageNo=-1; public SMSClient(int mode) { this.mode=mode; } public int sendMessage (String recipient, String message){ this.recipient=recipient; this.message=message; //System.out.println("recipient: " + recipient + " message: " + message); myThread = new Thread(this); myThread.start(); // run(); return status; } public void run(){ Sender aSender = new Sender(recipient,message); try{ //send message aSender.send (); // System.out.println("sending ... "); //in SYNCHRONOUS mode wait for return : 0 for OK, //-2 for timeout, -1 for other errors if (mode==SYNCHRONOUS) { while (aSender.status == -1){ myThread.sleep (1000); } } if (aSender.status == 0) messageNo=aSender.messageNo ; }catch (Exception e){ e.printStackTrace(); } this.status=aSender.status ; aSender=null; } } 
0
source share

The easiest way to do this is to use an SMS gateway.

There is a lot there, the one I used is Clickatel , to which I just send an XML message, and the gateway does the rest with almost nothing.

I did this using java and apache commons HTTP Client

+9
source share

Here you can find the Java SMS API project in the source forge.

In addition, for the infrastructure you will need an SMS Gateway . Some companies provide you with APIs that are as light as pie to make a program.

+2
source share

Step 1. Download Mail.jar and Activation.jar (see Resources for links) and save them in the Java library directory on the local drive of your computer.

Step 2

Run the new Java class in the Java Integrated Development Environment (IDE) and name it "MyMobileJava.java".

Step 3

Enter the following Java libraries at the beginning of your Java class. These libraries include the necessary Java Mail and Communications API resources and other supporting I / O and Internet class libraries for sending SMS text messages.

 import java.io.*; import java.net.InetAddress; import java.util.Properties; import java.util.Date; import javax.mail.*; import javax.mail.internet.*; import javax.activation.*; 

Step-4 Place the following Java code after the library import statements to create an instance of the Java class and assign default SMS text message values.

 public class SMTPSend { public SMTPSend() { } public void msgsend() { String username = "MySMSUsername"; String password = "MyPassword"; String smtphost = "MySMSHost.com"; String compression = "My SMS Compression Information"; String from = "mySMSUsername@MySMSHost.com"; String to = "PhoneNumberToText@sms.MySMSHost.com"; String body = "Hello SMS World!"; Transport myTransport = null; 

Step-5 Create Java code to create a new communication session, which will then be used to configure the information contained in the text message. This information will then be prepared for dispatch. Enter the following Java code in your Java class at the end of the code you entered in step 4.

  try { Properties props = System.getProperties(); props.put("mail.smtp.auth", "true"); Session mailSession = Session.getDefaultInstance(props, null); Message msg = new MimeMessage(mailSession); msg.setFrom(new InternetAddress(from)); InternetAddress[] address = {new InternetAddress(to)}; msg.setRecipients(Message.RecipientType.TO, address); msg.setSubject(compression); msg.setText(body); msg.setSentDate(new Date()); 

Step-6 Send a text message by connecting to your SMS host address, save the changes in the message and then send the information. To do this, enter the following Java code to end the Java class.

  myTransport = mailSession.getTransport("smtp"); myTransport.connect(smtphost, username, password); msg.saveChanges(); myTransport.sendMessage(msg, msg.getAllRecipients()); myTransport.close(); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] argv) { SMTPSend smtpSend = new SMTPSend(); smtpSend.msgsend(); } } //enter code here` 
+2
source share

The easiest way to do this is to find an operator that supports sms via mail.

Ex. Do you have Telia / Comviq / Chello or much more. If you are sending an email; yournumber@youroperator.com will send your email via sms to your phone.

0
source share

Please see SMSLib ( http://smslib.org ), an open source library for sending and receiving SMS using a GMS modem or mobile phone. This is a really great library.

0
source share

I wrote a small maven library to access the free (for customers only ) web interface of the Swiss mobile phone operators Sunrise and Orange. You find the source at http://github.com/resmo/libjsms

0
source share

Just download all the messages on your cell phone Email-to-SMS ( SMS gateway ) and send an email to this message by email address.

0
source share

All Articles