aspose file tools*
The moose likes Java Micro Edition and the fly likes hOW CAN I SEND AN SMS Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Mobile » Java Micro Edition
Bookmark "hOW CAN I SEND AN SMS" Watch "hOW CAN I SEND AN SMS" New topic
Author

hOW CAN I SEND AN SMS

Dilwan Sujan Cuttilan
Greenhorn

Joined: Apr 24, 2007
Posts: 3
Hi friends

Can you help me to send sms using a j2me application.

I have developed a j2me application which asks for input from the user. Then it assembles the input from the user for a specific message format.

What i need is this message should be sent to a specific mobile number. I think to send this message using an sms. But i dont know what happens when we send an sms. Whether a socket listning for the sms at the provider or etc

Can you help me to implemt this scenario.
Martijn Verburg
author
Bartender

Joined: Jun 24, 2003
Posts: 3274
    
    5

Hi there,

This is a topic that has been discussed many times at Javaranch, it's usually a good idea to SearchFirst before posting

Please do post here again if you have any further questions though!


Cheers, Martijn - Blog,
Twitter, PCGen, Ikasan, My The Well-Grounded Java Developer book!,
My start-up.
Chandrasekhar Mangipudi
Ranch Hand

Joined: Jan 29, 2006
Posts: 118
HI. You can try this code.

The following example uses Sun Microsystem�s JavaMail package to implement SMTP.
�Download Example Source Code
�Download Sun Microsystem Java Mail (mail.jar)
�Download Sun Microsystem Java Beans Activation Framework (activation.jar)

// Package Imports
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.*;

// Public Class
public class EmailSMS {

// Global Variables
String TO;
String FROM;
String SUBJECT;
String TEXT;
String MAILHOST;
String LASTERROR;

// Main function executed
public static void main(String[] args) throws Exception {
EmailSMS SMS = new EmailSMS();

SMS.setMailHost("mail.domain.com");
SMS.setTo("1234567890@messaging.nextel.com");
SMS.setFrom("name@domain.com");
SMS.setSubject("");
SMS.setText("Hello World!");
boolean ret = SMS.send();

if (ret) {
System.out.println("SMS was sent!");
} else {
System.out.println("SMS was not sent - " + SMS.getLastError());
}
}

// Public Constructor
public EmailSMS() {
TO = null;
FROM = null;
SUBJECT = null;
TEXT = null;
MAILHOST = null;
LASTERROR = "No method called.";
}

public void setTo(String to) {
TO = to;
}

public String getTo() {
return TO;
}

public void setFrom(String from) {
FROM = from;
}

public String getFrom() {
return FROM;
}

public void setSubject(String subject) {
SUBJECT = subject;
}

public String getSubject() {
return SUBJECT;
}

public void setText(String text) {
TEXT = text;
}

public String getText() {
return TEXT;
}

public void setMailHost(String host) {
MAILHOST = host;
}

public String getMailHost() {
return MAILHOST;
}

public String getLastError() {
return LASTERROR;
}

// Will attempt to send the Email SMS and return a boolean meaning it
// either failed or succeeded.
public boolean send() {

// Variables to check message length.
int maxLength;
int msgLength;

// Check to make sure that the parameters are correct
if (TO.indexOf("mobile.att.net") > 0) {
maxLength = 140;
} else if (TO.indexOf("messaging.nextel.com") > 0) {
maxLength = 280;
} else if (TO.indexOf("messaging.sprintpcs.com") > 0) {
maxLength = 100;
} else {
maxLength = 160;
}

// Calculate message length
msgLength = FROM.length() + 1 + SUBJECT.length() + 1 + TEXT.length();

// Typically, there are at least two characters of delimiter
// between the from, subject, and text. This is here to make
// sure the message isn't longer than the device supports.
if (msgLength > maxLength) {
LASTERROR = "SMS length too long.";
return false;
}

// Set Email Properties
Properties props = System.getProperties();

if (MAILHOST != null) {
props.put("mail.smtp.host", MAILHOST);
}

// Get a Session object
Session session = Session.getDefaultInstance(props, null);

try {

// Construct the email
Message msg = new MimeMessage(session);

// Set From
if (FROM != null) {
msg.setFrom(new InternetAddress(FROM));
} else {
msg.setFrom();
}

// Set Subject
msg.setSubject(SUBJECT);

// Set Text
msg.setText(TEXT);

// Add Recipient
msg.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(TO, false));

// Sent Date
msg.setSentDate(new Date());

// Send Email SMS
Transport.send(msg);

LASTERROR = "Success.";
return true;
} catch (MessagingException mex) {
LASTERROR = mex.getMessage();
return false;
}
}
}
[ September 15, 2008: Message edited by: Chandrasekhar Mangipudi ]

Thanks & Regards,
ChandraSekharMangipudi
Dilwan Sujan Cuttilan
Greenhorn

Joined: Apr 24, 2007
Posts: 3
Thank You Friends,

Please understand the problem.

There is a GSM modem with a sim running in my machine. What i need is to send sms to this modem using a j2me application. I am able to send sms to this modem using my phone nokia 3110c via a j2me application. But it is not working with nokia 6300.

And when i press the send button in my j2me application in my mobile it is asking again to verify to send the message. Once i press yes the sms is sent. Why is it happening? And is there a way to get the delivery report?

Please help
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: hOW CAN I SEND AN SMS