Wireless Messaging API (JSR-120 or WMA) for Java, allows access to wireless communication resources on J2ME devices. MMS is a mulitpart message and WMA api support mulitpart messages.
Find the code snippet here:

package com.neel.managers;
import java.io.IOException;
import javax.microedition.io.Connector;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.ImageItem;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.Spacer;
import javax.microedition.lcdui.TextField;
import javax.wireless.messaging.MessageConnection;
import javax.wireless.messaging.MessagePart;
import javax.wireless.messaging.MultipartMessage;
import com.neel.midlet.Paint;
import com.neel.ui.PaintScreen;

/**
*@author javatechig
* Class Name: MMSManager
* used By : PaintCanvas
*/
public class MMSManager implements Runnable, CommandListener{
PaintScreen paintScreen;
Paint midlet;
Form MMSForm;
String number=null;
String subject=null;
TextField mobileNumber_txt;
TextField subject_txt;

public MMSManager(Paint midlet,PaintScreen paintScreen){
      this.midlet=midlet;
      this.paintScreen=paintScreen;
      MMSForm=new Form("");
      midlet.getDisplay().setCurrent(MMSForm);
      mobileNumber_txt=new TextField("Mobile No:","",12,1);
      subject_txt=new TextField("Subject  :","",12,1);
      MMSForm.append(mobileNumber_txt);
      mobileNumber_txt.setLayout(Item.LAYOUT_CENTER);
      MMSForm.append(new Spacer(2,3));
      MMSForm.append(subject_txt);
      subject_txt.setLayout(Item.LAYOUT_CENTER);
      MMSForm.append(new Spacer(2,10));

      ImageItem imageitem=new ImageItem("",scaleImage(targetImage,170,200), 
                                 Item.LAYOUT_CENTER,"");
      MMSForm.append(imageitem);
      MMSForm.addCommand(new Command("Back",Command.BACK,0));
      MMSForm.addCommand(new Command("Send",Command.OK,0));
      MMSForm.setCommandListener(this);
}

public void commandAction(Command c, Displayable d) {
     if(d==MMSForm && c.getLabel().equals("Back")){
        midlet.getDisplay().setCurrent(paintScreen);
     }
    else if(d==MMSForm && c.getLabel().equals("Send")){
      sendMMS();
    }
}

private void sendMMS() {
      Thread t = new Thread(this);
      t.start();
}

public void run(){
    try { 
       String mmsUrl = "mms://+919535080650:com.test.MyApp";
       MessageConnection connection = (MessageConnection) Connector.open(mmsUrl);
       MultipartMessage message = (MultipartMessage) 
              connection.newMessage(MessageConnection.MULTIPART_MESSAGE);
       message.setAddress(mmsUrl);
       message.setSubject(subject);

       InputStream is = getClass().getResourceAsStream("/image.jpg");
       byte[] imageBytes = new byte[is.available()];
       is.read(imageBytes);

       message.addMessagePart(new MessagePart(imageBytes, 0, imageBytes.length,
                              "image/jpg", "card", "card image", null));
       connection.send(message);
       if (connection != null) {
           try {
               connection.close();
           } catch (IOException ioe) {
               ioe.printStackTrace();
           }
       }
       } catch (Exception e) {
           e.printStackTrace();
       }
}
This article is about: Code Sample, J2ME,

Stacktips

StackTips provides programming tutorials, how-to guides and code snippets on different programming languages. Follow us on Twitter

Join The Discussion

Please note: We reserve the right to delete comments that contains snarky remarks, offensive or off-topic. To know more read our comments policy.