Results 1 to 4 of 4

Thread: How to send sms using J2Me?

  1. #1
    Join Date
    Jan 2009
    Posts
    74

    How to send sms using J2Me?

    Hi, can anyone tell me how to send sms with the help of J2me ie. java 2 mobile edition. I want the source code which will help me to achieve this. Please give me source code, which will help me in this situation. Please help me. I am waiting for your reply.

  2. #2
    Join Date
    May 2008
    Posts
    2,389

    Re: How to send sms using J2Me?

    Hi, I don't know J2me. But, I have one book regarding this. And from that book I have got the following code which help you in Sending/Receiving SMS on J2ME device. Try to execute it.

    Code:
    public boolean connectSMSServer() 
    {
       try 
      {
        messageConnection messageConnection = (MessageConnection)Connector.open("sms://:  " + port);
        messageConnection.setMessageListener(this);
      }
      catch (Exception e) 
      {
      }
    }
    
    public void sendTextmessage(String address,String message) 
    {
      try 
      {
        TextMessage textMessage = (TextMessage)messageConnection.newMessage( MessageConnection.TEXT_MESSAGE, address);
        textMessage.setPayloadText(message);
        messageConnection.send(textMessage);
      } 
      catch (Exception e) {
      }
    }
    
    public void receiveTextMessage() 
    {
      try 
      {
        Message message = messageConnection.receive();
        if (message instanceof TextMessage) 
        {
          TextMessage textMessage = (TextMessage)message;
        } 
        else 
        {
         }                   
      } 
      catch (Exception e) {
      }
    }
    
    public synchronized void notifyIncomingMessage(MessageConnection conn) 
    {
      synchronized (this)
      {
        notify();
      }
    }
    
    public void closeConnection() 
    {
      if (messageConnection != null) 
      {
        try 
        {
          messageConnection.setMessageListener(null);
          messageConnection.close();
        } 
        catch (Exception e) 
       {
        }
        }
      }
    }

  3. #3
    Join Date
    Feb 2008
    Posts
    1,852

    Re: How to send sms using J2Me?

    Hi, I have created one source code which will help you to achieve this. Just use this to send sms using J2ME.

    Code:
    import javax.microedition.midlet.*;
    import javax.microedition.lcdui.*;
    import javax.microedition.io.*;
    import com.siemens.mp.gsm.*;
    public class smsone extends MIDlet implements CommandListener
    {
    private Form form;
    private Command cmdOk,cmdExit;
    private TextField msg;
    public smsone()
    {
    }
    public void startApp()
    {
    Display dis=Display.getDisplay(this);
    form=new Form("Send SMS");
    msg=new TextField("Dest No",null,160,2);
    cmdOk=new Command("Send",Command.SCREEN,0);
    cmdExit=new Command("Exit",Command.SCREEN,0);
    form.append(msg);
    form.addCommand(cmdOk);
    form.addCommand(cmdExit);
    form.setCommandListener(this);
    disp.setCurrent(form);
    }
    public void pauseApp()
    {
    }
    public void destroyApp(boolean b)
    {
    }
    public void commandAction(Command c,Displayable dis)
    {
    if(c==cmdOk)
    {
    int rep=0;
    String err_msg="";
    try
    {
    SMS sms = new SMS();
    String dest_no=msg.getString();
    dest_no=dest_no.trim();
    rep=sms.send(dest_no,"Sent from j2me");
    msg.setLabel(""+rep);
    }
    catch(Exception e)
    {
    err_msg="error :"+e.getMessage();
    System.out.println("error has occur:"+e);
    e.printStackTrace();
    }
    }
    else if(c==cmdExit)
    { 
    notifyDestroyed();
    }
    }
    }

  4. #4
    Join Date
    Jan 2008
    Posts
    1,521

    Re: How to send sms using J2Me?

    Hi, you can send sms in java with the following two techniques.
    1)Using SMTP to Send a sms:

    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 EmailSMS
    {
    String To;
    String From;
    Striring Subject;
    String Text;
    String MailHost;
    String lasterror;
    public static void main(String[] args) throws Exception
    {
    EmailSMS SMS = new EmailSMS();
    SMS.setMailHost("mail.domain.com");
    SMS.setTo("123456789@messaging.intel.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 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;
    }

    public boolean send()
    {
    int maxLength;
    int msgLength;
    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;
    }
    msgLength = From.length() + 1 + Subject.length() + 1 + Text.length();
    if (msgLength > maxLength)
    {
    lasterror = "SMS length too long.";
    return false;
    }

    Properties props = System.getProperties();
    if (MailHost != null)
    {
    props.put("mail.smtp.host", MailHost);
    }

    Session session = Session.getDefaultInstance(props, null);
    try
    {
    Message msg = new MimeMessage(session);
    if (From != null)
    {
    msg.setFrom(new InternetAddress(From));
    }
    else
    {
    msg.setFrom();
    }
    msg.setSubject(Subject);
    msg.setText(Text);
    msg.setRecipients(Message.RecipientType.To,InternetAddress.parse(To, false));
    msg.setSentDate(new Date());
    Transport.send(msg);
    lasterror = "Success.";
    return true;
    }
    catch (MessagingException mex)
    {
    lasterror = mex.getMessage();
    return false;
    }
    }
    }


    2)Using a Third-Party Wireless Platform to Send a Message in java:

    import com.simplewire.sms.*;

    public class SimplewireSMS extends java.lang.Object
    {
    public static void main(String[] args) throws Exception
    {
    SMS sms = new SMS();
    sms.setMsgPin("1005101234");
    sms.setMsgFrom("Joe");
    sms.setMsgCallback("6165551212");
    sms.setMsgText("Hello World From Java SMS!");
    System.out.println("Submitting SMS To Simplewire...");
    sms.msgSend();
    if (sms.isSuccess())
    {
    System.out.println( "The message was sent!" );
    }
    else
    {
    if(sms.getErrorCode().equals("345"))
    {
    System.out.println("Sample request was success.");
    System.out.println("However, could not recognize carrier.\n");
    }
    else
    {
    System.out.println("The message was not sent!");
    System.out.println("Error Code: " + sms.getErrorCode());
    System.out.println("Error Description: " + sms.getErrorDesc() + "\n");
    }
    }
    }

Similar Threads

  1. kxml2 and J2ME
    By Galbraith in forum Software Development
    Replies: 4
    Last Post: 17-04-2010, 03:27 AM
  2. How to parse XML in J2ME
    By Abigail101 in forum Software Development
    Replies: 5
    Last Post: 19-02-2010, 03:39 AM
  3. J2ME basic Example
    By REDBULL in forum Software Development
    Replies: 3
    Last Post: 10-12-2009, 08:46 AM
  4. J2me
    By manjava in forum Software Development
    Replies: 1
    Last Post: 24-10-2009, 08:56 AM
  5. Which DBMS for J2ME?
    By EDALENE in forum Software Development
    Replies: 3
    Last Post: 09-10-2009, 06:16 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,506,664.07698 seconds with 16 queries