Saturday, May 9, 2009

send SMS using u'y wey2sms account (Java)

This program help you to send sms using way2sms account

Its not written by by me I have just modified it


smsconfig.properties
user=way2sms username
password=way2sms password
proxy_enabled=true/false
proxy_host=if true ip of the proxy
proxy_port=port number of proxy
protocol=http

/**
* SMSConfig.java
* Created on Mar 11, 2009 6:36:23 AM
*/
package sms;

import java.util.ResourceBundle;

/**
* @author Nimish T
*
*/
public class SMSConfig {

/**
*
*/
public SMSConfig() {
// TODO Auto-generated constructor stub
}
private static ResourceBundle resourceBundle;
public static String USERNAME;
public static String PASSWORD;
public static boolean PROXY;
public static String PROXY_HOST;
public static int PROXY_PORT;
public static String PROTOCOL;
static {
try {
resourceBundle=ResourceBundle.getBundle("sms.smsconfig");
init();
} catch (Exception e) {
//e.getMessage();
e.printStackTrace();
}
}
private static void init(){
USERNAME=resourceBundle.getString("user");
PASSWORD=resourceBundle.getString("password");
PROXY=Boolean.valueOf(resourceBundle.getString("proxy_enabled"));
if(PROXY){
PROXY_HOST=resourceBundle.getString("proxy_host");
PROXY_PORT=Integer.parseInt(resourceBundle.getString("proxy_port"));
PROTOCOL=resourceBundle.getString("protocol");
}

}
}



/**
* SMS.java
* Created on Mar 10, 2009 7:37:35 PM
*/
package sms;

/**
* @author Nimish T
*
*/

import java.io.*;
import java.net.*;
import java.util.Vector;

public class SMS
{
public static void send(String uid, String pwd, String phone, String msg) throws IOException {

if (uid == null || 0 == uid.length())
throw new IllegalArgumentException("User ID should be present.");
else
uid = URLEncoder.encode(uid, "UTF-8");

if (pwd == null || 0 == pwd.length())
throw new IllegalArgumentException("Password should be present.");
else
pwd = URLEncoder.encode(pwd, "UTF-8");

if (phone == null || 0 == phone.length())
throw new IllegalArgumentException("At least one phone number should be present.");

if (msg == null || 0 == msg.length())
throw new IllegalArgumentException("SMS message should be present.");
else
msg = URLEncoder.encode(msg, "UTF-8");

Vector numbers = new Vector();
String pharr[];
if (phone.indexOf(';') >= 0) {
pharr = phone.split(";");
for (String t : pharr) {
try
{
numbers.add(Long.valueOf(t));
}
catch (NumberFormatException ex)
{
throw new IllegalArgumentException("Give proper phone numbers.");
}
}
} else {
try
{
numbers.add(Long.valueOf(phone));
}
catch (NumberFormatException ex)
{
throw new IllegalArgumentException("Give proper phone numbers.");
}
}

if (0 == numbers.size())
throw new IllegalArgumentException("At least one proper phone number should be present to send SMS.");

/*==================================================================*/

// Login
String temp = "";
String content = "username=" + uid + "&password=" + pwd + "&q=Deepika%20Padukone%20Photo%20Gallery";
URL u=null;
if(SMSConfig.PROXY){
u = new URL(SMSConfig.PROTOCOL,SMSConfig.PROXY_HOST,SMSConfig.PROXY_PORT,"http://wwwd.way2sms.com/auth.cl");
} else {
u = new URL("http://wwwd.way2sms.com/auth.cl");
}
HttpURLConnection uc = (HttpURLConnection) u.openConnection();
uc.setDoOutput(true);
uc.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.5) Gecko/2008120122 Firefox/3.0.5");
uc.setRequestProperty("Content-Length", String.valueOf(content.length()));
uc.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
uc.setRequestProperty("Accept", "*/*");
uc.setRequestMethod("POST");
uc.setInstanceFollowRedirects(false); // very important line :)
PrintWriter pw = new PrintWriter(new OutputStreamWriter(uc.getOutputStream()), true);
pw.print(content);
pw.flush();
pw.close();
BufferedReader br = new BufferedReader(new InputStreamReader(uc.getInputStream()));
while ( (temp = br.readLine()) != null ) {}
String cookie = uc.getHeaderField("Set-Cookie");

// Send SMS to each of the phone numbers
u = null; uc = null;
for (long num : numbers)
{
content = "HiddenAction=instantsms&login=&pass=&custid=undefined&MobNo=" + num + "&textArea=" + msg;
if(SMSConfig.PROXY){
u = new URL(SMSConfig.PROTOCOL,SMSConfig.PROXY_HOST,SMSConfig.PROXY_PORT,"http://wwwd.way2sms.com/FirstServletsms?custid=");
} else {
u = new URL("http://wwwd.way2sms.com/FirstServletsms?custid=");
}
uc = (HttpURLConnection) u.openConnection();
uc.setDoOutput(true);
uc.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.5) Gecko/2008120122 Firefox/3.0.5");
uc.setRequestProperty("Content-Length", String.valueOf(content.length()));
uc.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
uc.setRequestProperty("Accept", "*/*");
uc.setRequestProperty("Cookie", cookie);
uc.setRequestMethod("POST");
uc.setInstanceFollowRedirects(false);
pw = new PrintWriter(new OutputStreamWriter(uc.getOutputStream()), true);
pw.print(content);
pw.flush();
pw.close();
br = new BufferedReader(new InputStreamReader(uc.getInputStream()));
while ( (temp = br.readLine()) != null ) {}
br.close();
u = null;
uc = null;
}

// Logout
if(SMSConfig.PROXY){
u = new URL(SMSConfig.PROTOCOL,SMSConfig.PROXY_HOST,SMSConfig.PROXY_PORT,"http://wwwd.way2sms.com/jsp/logout.jsp");
} else {
u = new URL("http://wwwd.way2sms.com/jsp/logout.jsp");
}
uc = (HttpURLConnection) u.openConnection();
uc.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.5) Gecko/2008120122 Firefox/3.0.5");
uc.setRequestProperty("Accept", "*/*");
uc.setRequestProperty("Cookie", cookie);
uc.setRequestMethod("GET");
uc.setInstanceFollowRedirects(false);
br = new BufferedReader(new InputStreamReader(uc.getInputStream()));
while ( (temp = br.readLine()) != null ) {}
br.close();
u = null;
uc = null;
}
}


//Mail send example
public class SMSSender{
public static void main(String a[]){
SMS.send(SMSConfig.USERNAME, SMSConfig.PASSWORD, "9496340776", "My First SMS");
}
}



cellphone application developers cochin kerala
website hosting development kochi kerala

15 comments:

  1. how To Develop This Code Please Guide Me

    ReplyDelete
  2. Thanks to you buddy.......... :)

    chindukrishna@gmail.com

    ReplyDelete
  3. Thanks it works...!

    ReplyDelete
  4. Thank you very much, this code will help many users and it helped me too................

    ReplyDelete
  5. Hi, when i implement this code, with our office LAN (Proxy) settings, while executing this code this is giving Server returned HTTP response code: 403 for URL: http://wwwd.way2sms.com/auth.cl error. However this is nice work from u.

    ReplyDelete
  6. Thanks buddy, it is really nice. I have one query regarding this, can we omit the succeeded part (By Way2SMS.com stuff) from message after receiving to end user, and instead of from telephone number can we display any String which is configurable.

    Thanks in Advance.

    ReplyDelete
  7. i am getting a error for incompitable types for the line
    for(long num : numbers){}

    please help me

    ReplyDelete
  8. Hi In my machine the code has no error it runs with no issue still i am able to send sms. I am using mac OS. Can any one help me to fix the issue.

    My email id is -: naresh_sahoo1981@yahoo.com
    Thanks,
    Naresh

    ReplyDelete
  9. @Anonymous Hey change the declaration, of

    Vector numbers = new Vector();

    to this:

    Vector<Long> numbers = new Vector<Long>();

    Now, it will work :)

    ReplyDelete
  10. Hi, when i implement this code ,while executing this code this is giving Server returned HTTP response code: 500 for URL: http://wwwd.way2sms.com/auth.cl error.
    plz hlp me solve ths prob.
    However this is verynice work from u.

    ReplyDelete

LinkWithin

Related Posts with Thumbnails