Java sample program to send sms through nowsms gateway

Java sample program to send sms through nowsms gateway SearchSearch
Author Message
Imraj sk
New member
Username: Imraj

Post Number: 2
Registered: 12-2008
Posted on Wednesday, December 10, 2008 - 03:09 am:   

Hi can any one please send me the java sample code to send sms from pc to mobile through nowsms gateway.. i have found some links in nowsms but they are not working..i have founf the follwoing code but its not working... can anyt one send me the full java sample code and tell me how to implement.... Thanks in advance...
here is the code i have with me..


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


class Smssending
{

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

String phone="+91988698866";
String message="Hi welcome to new technologies";
String ppgHost="http://localhost:8800/";
try{
phone=URLEncoder.encode(getMobileId(), "UTF-8");
if(debug)
System.out.println("phone---->"+phone);
message=URLEncoder.encode(getMessage(), "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;
if(debug)
System.out.println("path name---url_str----->"+url_str);
URL url2=new URL(url_str);
HttpURLConnection connection = (HttpURLConnection) url2.openConnection();
connection.setDoOutput(false);
connection.setDoInput(true);
if(debug)
System.out.println("connection :"+connection);
String res=connection.getResponseMessage();
if(debug)
System.out.println("res :"+res);

int code = connection.getResponseCode () ;
if ( code == HttpURLConnection.HTTP_OK )
{

connection.disconnect() ;

}


}
}

iam getting the following error.. i know becoz of those two variables iam getting the error but how to decare that method getmobileid() and getMessage().


Smssending.java:18: cannot find symbol
symbol : method getMobileId()
location: class Smssending
phone=URLEncoder.encode(getMobileId(), "UTF-8");
^
Smssending.java:21: cannot find symbol
symbol : method getMessage()
location: class Smssending
message=URLEncoder.encode(getMessage(), "UTF-8");
^
2 errors
Des - NowSMS Support
Board Administrator
Username: Desosms

Post Number: 313
Registered: 08-2008
Posted on Wednesday, December 10, 2008 - 04:22 pm:   

Have you tried the change suggested in the following thread:

http://support.nowsms.com/discus/messages/1/24573.html

Please post any follow-up messages to that thread.
Imraj sk
New member
Username: Imraj

Post Number: 3
Registered: 12-2008
Posted on Thursday, December 11, 2008 - 02:35 am:   

I tried with the above link also but its not working...getting following errors
Smssending.java:18: cannot find symbol
symbol : method getMobileId()
location: class Smssending
phone=URLEncoder.encode(getMobileId(), "UTF-8");
^
Smssending.java:21: cannot find symbol
symbol : method getMessage()
location: class Smssending
message=URLEncoder.encode(getMessage(), "UTF-8");
^
2 errors

where should i declare these methods getMessage()and getPhone()
Des - NowSMS Support
Board Administrator
Username: Desosms

Post Number: 320
Registered: 08-2008
Posted on Thursday, December 11, 2008 - 05:12 pm:   

There are too many discussion threads dicussing this same script, it looks like I referenced the wrong one.

This is the suggestion that I offered in one of the other threads:

I am not a Java expert, but I am guessing that the functions getMobileId() and getMessage() simply are not defined.

I suspect the person who originally wrote this code used those functions, but then to try to give you a quick example, took the functions out, and substituted in variable settings ... but didn't modify the code.

I would change "getMobileId()" to "phone" ... and "getMessage()" to "message". (Do not include the "quote" characters, I am just using them as delimiters to indicate what text should be changed.)
PHILIP TEO
New member
Username: Philip_teoph

Post Number: 1
Registered: 02-2009
Posted on Wednesday, February 04, 2009 - 08:53 am:   

Attached please find the working java code for sending SMS via Now.SMS.

text/plainjava code for sending SMS via Now.SMS
sendSMS.java (1.7 k)
Des - NowSMS Support
Board Administrator
Username: Desosms

Post Number: 423
Registered: 08-2008
Posted on Wednesday, February 04, 2009 - 07:36 pm:   

Thanks ... your example inspired me to brush up on my Java skills.

I've uploaded a more flexible Java example for NowSMS at the following link:

http://www.nowsms.com/download/sendsms.java.txt

More information and explanation of how to use this Java code can be found here:

http://blog.nowsms.com/2009/02/nowsms-java-example-send-sms-message.html


--
Des
NowSMS Support
Sajid Ahmed
New member
Username: Sajid

Post Number: 21
Registered: 10-2008
Posted on Wednesday, February 04, 2009 - 07:50 pm:   

Hi ,

Please find attach MultipartPost.rar with this mail.
HttpMultipartRequest.java is a class where i am creating post and FileUploadDemo.java is the file where i am handling it.

please help me to understand how i need to improve and make it work.

i wanna make sure i posted as per link

http://www.nowsms.com/documentation/ProductDocumentation/mms_notifications_and_content/Submitting_MMS_Messages_URL.htm



Thanks & Regards,

Sajid
application/octet-streamMMS
MultipartPost.rar (58.0 k)
Des - NowSMS Support
Board Administrator
Username: Desosms

Post Number: 425
Registered: 08-2008
Posted on Wednesday, February 04, 2009 - 08:23 pm:   

Hi Sajid,

The SMS example was really my first real Java code, so it's going to take awhile to figure out a multipart post for MMS.

Since my Java expertise is not so good ... it would be easier for me to troubleshoot if I could see a trace of your output. Then I can look at what is wrong with the data, which is easier for me to understand than the Java code. Can you capture it with Wireshark and post the capture file?

--
Des
NowSMS Support
Des - NowSMS Support
Board Administrator
Username: Desosms

Post Number: 430
Registered: 08-2008
Posted on Thursday, February 05, 2009 - 01:08 am:   

Sajid,

I'm new to Java, so my examples might be a little rough around the edges.

That said ... I adapted Vlad Patryshev's ClientHttpRequest class to create a sendmms class that might be a little easier to figure out. (I've successfully sent several messages with it.)

The example is here:

http://www.nowsms.com/download/sendmms.java.txt

The comments at the start of the class explain how you use it.

For more information on the original script, see:

http://www.devx.com/Java/Article/17679/1954
http://www.myjavatools.com/

--
Des
NowSMS Support
Sajid Ahmed
New member
Username: Sajid

Post Number: 22
Registered: 10-2008
Posted on Thursday, February 05, 2009 - 07:59 am:   

Hi please find attached final.pcap file with this mail.i captured it using wireshark.in Folllow TCP Stream i got this....


POST /multipart/servlet/FileUploadDemo HTTP/1.1

Accept: */*

Content-Type: multipart/form-data; boundary=--sajid-ahmed--

Content-length: 611559

User-Agent: Java/1.5.0_15

Host: 196.12.36.114:9090

Connection: keep-alive



----sajid-ahmed--

Content-Disposition: form-data; name="MMSSubject"



mms test

----sajid-ahmed--

Content-Disposition: form-data; name="MMSText"



Belive me i make it happen!

----sajid-ahmed--

Content-Disposition: form-data; name="PhoneNumber"



+448080148324

----sajid-ahmed--

Content-Disposition: form-data; name="MMSFrom"



sajid@Teleparadigm.com

----sajid-ahmed--

Content-Disposition: form-data; name="MMSFile"; filename="sajid.jpg"

Content-Type: image/png



....!.Exif..II*...............z...................................................(...........2......


please also help me if the file is mms what should be the content-type=?

if it is .mms extention what wud be content-type?

----sajid-ahmed--

Content-Disposition: form-data; name="MMSFile"; filename="sajid.jpg"

Content-Type: image/jpg

Thanks & Regards,

Sajid



application/octet-streamtcp
final.pcap (226.1 k)
Sajid Ahmed
New member
Username: Sajid

Post Number: 23
Registered: 10-2008
Posted on Thursday, February 05, 2009 - 08:02 am:   

how to validate whether we didn't screw up at using the right counts, and the right boundary strings, and the right escaping?

can we go with Nokia MMS Simulator to test mms but how?
Sajid Ahmed
New member
Username: Sajid

Post Number: 24
Registered: 10-2008
Posted on Thursday, February 05, 2009 - 11:27 am:   

One mistake till now i understood is this boundary, which must not occur in any of the parts i used my name which is wrong

It is up to the sending mail client to choose a boundary string that doesn't clash with the body text. Typically this is done by inserting a long random string.

The encapsulation boundary must not appear inside any of the encapsulated parts. It is crucial that the composing user agent be able to choose and specify the unique boundary that will separate the body parts. Encapsulation boundaries may be no longer than 70 characters, not counting the blank line and leading hyphens

This is implicitly typed plain ASCII text.
--simple boundary
Content-type: text/plain; charset=us-ascii

This is explicitly typed plain ASCII text. It DOES end
with a line break.
--simple boundary--
This is the epilogue. It is also to be ignored.
The use of a Content-Type of multipart in a body part within another multipart entity is explicitly allowed. In such cases, care must be taken to ensure that each nested multipart entity uses a different boundary delimiter.

The use of the multipart Content-Type with only a single body part may be useful in certain contexts, and is explicitly permitted.

Multipart/Mixed
indicates multiple independent body parts to be viewed serially.
Multipart/Alternative


I found this link to be excellent for mime understandings i wud like to share this with you all

http://mgrand.home.mindspring.com/mime.html

The above link gives you the answer for what i was asking about mm7 post where we have two boundaries.

hey after doing all painful work i got this link by DES

Thanks Des

http://mgrand.home.mindspring.com/mime.html


My doubt here if the file is .mms then what will be the Content-Types and MIME Types
Des - NowSMS Support
Board Administrator
Username: Desosms

Post Number: 431
Registered: 08-2008
Posted on Thursday, February 05, 2009 - 01:15 pm:   

Hi Sajid,

In the capture file that you posted, I didn't see the closing boundary. That is likely where the problem is.

Regarding content types in the POST, I believe that we ignore them when you are using this interface. (The reason for this is because if the files are submitted via our web form, the content types specified by the web browser in the upload will not likely be correct.)

Instead, we determine the content type to be used in the resulting MMS message based upon the file extension (e.g., .jpg, .txt, .3gp). The MMSCTYPE.INI file is used for these mappings. If the extension is not found in the MMSCTYPE.INI, we then also consult the Windows registry on the PC running NowSMS for additional file extension to content type mappings. But unless you're doing something highly unusual, the content types in MMSCTYPE.INI should be fine for most MMS messages.

If you want to send a pre-compiled ".mms" file, then only include one "MMSFILE" object. The fact that it is a pre-compiled ".mms" file will be recognised based upon the file extension.

--
Des
NowSMS Support

P.S. - For others reading this, the example Java code that I put together for submitting an MMS message can be found here: http://www.nowsms.com/download/sendmms.java.txt
Sajid Ahmed
New member
Username: Sajid

Post Number: 25
Registered: 10-2008
Posted on Thursday, February 05, 2009 - 05:24 pm:   

Hi Des,

Thanks a lot for your feed back.

Can we go with Nokia MMS Simulator to test mms but how?

Now i am making .mms using mmscomp.exe from C:\Program Files\NowSMS

Is there any other alternative to make it programatically(.mms files).

This mmscomp.exe is being executed programatically using java by me.

From the file "mmsctype.ini" i understood

application/vnd.wap.mms-message=.mms

Thanks
Des - NowSMS Support
Board Administrator
Username: Desosms

Post Number: 436
Registered: 08-2008
Posted on Friday, February 06, 2009 - 12:32 am:   

Hi Sajid,

There is no way to send an MMS message to the Nokia simulator ... it does not implement the actual over the air MMS protocol. Unfortunately, that is common with almost all software simulators.

If you are interested in this technically, the following thread has pointers to more information: http://support.nowsms.com/discus/messages/485/16730.html

But the short answer is that the simulator you are using cannot receive MMS messages using the standard protocols.

It is not necessary to use MMSCOMP when sending an MMS message with NowSMS. The compiled ".mms" file is the format of an actual MMS message when it is retrieved "over the air" in a live production environment. However, the other MMS application protocols (such as MM7) do not actually use this format ... as they tend to use standard MIME encoding instead. (The compiled ".mms" format is really just a special binary implementation of MIME which uses binary headers and tokens instead of text.)

So ... if you are using the Java code above to send MMS messages, it is not necessary to create a ".mms" file. Just specify the different file components of the MMS message as separate "MMSFILE" references.

For example, in the sample Java code, it would be:

mms.addparameter ("MMSFile", new File("f:\\temp\\test.jpg"));
mms.addparameter ("MMSFile", new File("f:\\temp\\test2.jpg"));

If you have a SMIL component, this can be one of the "MMSFile" objects. NowSMS will automatically encode them into a compiled ".mms" file if it is necessary for the outbound transport format.

--
Des
NowSMS Support
Sajid Ahmed
New member
Username: Sajid

Post Number: 27
Registered: 10-2008
Posted on Friday, February 06, 2009 - 01:49 am:   

Hi Des,

Sound too good.In my case i will be having lots of developers who will compose .mms using above parameter concept as i said.but later i wont be sending to server instead i will store .mms in db so that we can retrive it wth the id and send at any time to any number of users :-)

Thanks a lot.if still if you have any other alternative to make .mms file please update me.


Thanks & Regards,

Sajid
bharath bs
New member
Username: Bs_bharath

Post Number: 1
Registered: 02-2009
Posted on Thursday, February 12, 2009 - 08:29 am:   

I requir to send jar file by sms to a user who donot have access to web or gprs.Is this posible
Des - NowSMS Support
Board Administrator
Username: Desosms

Post Number: 461
Registered: 08-2008
Posted on Thursday, February 12, 2009 - 02:43 pm:   


quote:

I requir to send jar file by sms to a user who donot have access to web or gprs.Is this posible




If they have MMS, yes. A JAR file can be included in an MMS message.

(Sometimes user accounts have GPRS access only for sending/receiving MMS.)

Otherwise, no.

(An SMS message can only contain 140 bytes of binary data, so even spanning multiple SMS messages ... if this was possible ... it would not be very practical.)

--
Des
NowSMS Support