0

I am trying to send an sms from my mobile number to another number using a java program, I am not allowed to use any third party gateways . My code as follows:

import java.net.*; 
import java.io.*; 
import javax.swing.*; 
import javax.net.ssl.HttpsURLConnection; 


class SendSMS 
{ 

public static void main(String args[]) 
{ 
boolean debug=true; 

String phone    ="10 digit number"; 
String message  ="hi"; 
String ppgHost  ="http://localhost:8800/"; 
String username ="";
String password ="";

try
{ 
phone=URLEncoder.encode("8366xxx", "UTF-8"); 

if(debug) 
System.out.println("phone------>"+phone); 
message=URLEncoder.encode("SendMsg via Now.SMS", "UTF-8"); 

if(debug) 
System.out.println("message---->"+message); 
} 
catch (UnsupportedEncodingException e) 
{ 
System.out.println("Encoding not supported"); 
} 

String url_str=ppgHost+"?PhoneNumber="+phone+"&Text="+message; 
//String url_str=ppgHost+"?user="+username+"&password="+password+"&PhoneNumber="+phone+"&Text="+message; 

if(debug) 
System.out.println("url string->"+url_str); 


try
{   
URL url2=new URL(url_str); 

HttpURLConnection connection = (HttpURLConnection) url2.openConnection(); 
connection.setDoOutput(false); 
connection.setDoInput(true); 

if(debug) 
System.out.println("Opened Con->"+connection); 

String res=connection.getResponseMessage(); 

if(debug) 
System.out.println("Get Resp ->"+res); 

int code = connection.getResponseCode () ; 

if ( code == HttpURLConnection.HTTP_OK ) 
{ 
connection.disconnect() ; 
}
}
catch(IOException e)
{
System.out.println("unable to create new url"+e.getMessage());
}

}
}

Please guide me guys. I connected my mobile using data cable into my system and tried to send but I am getting the error as follows:

C:\SMS>java SendSMS
phone------>8366xxx
message---->SendMsg+via+Now.SMS
url string->http://localhost:8800/?PhoneNumber=8366xxx&Text=SendMsg+via+Now.SMS
Opened Con->sun.net.www.protocol.http.HttpURLConnection:http://localhost:8800/?P
honeNumber=8366xxx&Text=SendMsg+via+Now.SMS
unable to create new urlConnection refused: connect

Please help guys

  • I do not want to use any third party gateway
  • The amount of sms will be used from my mobile balance

What I doing wrong here? I am new to this. Almost all the examples are given are using third party libraries and gateways. Help guys!

Santhucool
  • 1,656
  • 2
  • 36
  • 92
  • possible duplicate of [How to send SMS in Java](http://stackoverflow.com/questions/2570410/how-to-send-sms-in-java) – Shabbir Dhangot Jan 06 '15 at 11:28
  • 1
    @SidDhangot its not duplicate just read it, I dont want to use third party gateway – Santhucool Jan 06 '15 at 11:44
  • As far as I am aware, you cant send an SMS for free. You have to pay a company like BT and go through them. – ConMan Jan 06 '15 at 12:50
  • @ConMan there is a way we can connect our device to system and using the balance in device we can send sms!! – Santhucool Jan 06 '15 at 12:59
  • Ehm, your program tries to open a URL via HTPP on localhost passing the phone number and text as GET parameters. So far nothing related to your mobile. Plugging in the data cable doesn't magically start a process on your system listening on port 8800 ready to send SMS via the connected mobile. Sorry, it's hard to say, but you didn't make any progress with your attempt. – vanje Jan 06 '15 at 21:02
  • What kind of mobile do you use? One possible approach would be to create an app running on your mobile opening a socket via WiFi and able to send SMS. Then you can write a Java program to be run on your desktop pc. If your mobile is connected to your local wireless network your Java program can call your app via the socket. So your desktop Java program sends the phone number and the text to the app running on your mobile and the app then sends the SMS. – vanje Jan 06 '15 at 21:09
  • @vanje buddy, my requirement is like that I will have to connect my phone using blutooth to the system. The java program running on the system should take user input like message and sender number from the user and can send sms to the sender. The mobile's(Which are connected via bluetooth) message sender number and all the other details can be included in java program. So that the balance should be deducted from phone's message balance. That is why no gateway needed. Now hope you are clear. Please help me!! – Santhucool Jan 07 '15 at 08:54
  • Whether you connect your mobile via bluetooth or wifi is not so important. Either way you need to create an app for your mobile as a counterpart to your desktop program to be able to send SMS. Maybe this is the type of mobile app/desktop app combination you are looking for: https://play.google.com/store/apps/details?id=com.smscontrolcenter – vanje Jan 07 '15 at 14:21
  • @vanje I mean when we connect the mobile it will act as a modem so we can use AT commands to send SMS. I have tried SMSLib for the same but the issue is it need to configure windows32com.dll into it. In my case I am using windows8.1,64bit so it is not supporting. Any solutions can you suggest? – Santhucool Jan 07 '15 at 15:06
  • Sorry, this is very confusing. First you said, you want to connect your mobile via cable. Now you say via bluetooth. And your code is totally unrelated to both. It seems it's blindly copied from somewhere. You should first nail down your requirements. Do you want to connect your mobile via cable or bluetooth or both? And do you know for real that your specific mobile can be controlled with AT commands? Or is it only an assumption you made because you heard something like that? – vanje Jan 07 '15 at 17:52
  • If you are sure about that, then you only have to research two things: 1: What are the AT commands to send SMS? 2: How can you send AT commands to your mobile while connected to your PC (via bluetooth or cable according to your requirements). Then you can write some code. And if you are running into problems with your code, you can ask specific questions here on stackoverflow. – vanje Jan 07 '15 at 17:55
  • @vanje I have tried RXTX api with AT commands but it is not working properly I am getting errors. If you know about RXTX can you help me buddy? – Santhucool Jan 08 '15 at 03:57
  • I was also looking such type of implementation, got this one http://www.wirelessdevnet.com/channels/java/features/simplewire/ But the issue is the stupid registration – ritesh Mar 10 '15 at 05:51

0 Answers0