Header Sep
Java Tips, Tricks & Code
My Rating Score
Login to rate page

December 2005
How to send an MMS using JSR 205

[Back]

 

In Sony Ericsson phones supporting Java Platform 6, such as the W550, JSR 205 is introduced and now it is possible to send and receive MMS messages using the Java ME Platform. In this article will give you an example of an MMS client.

Download MIDlet and source code sample>>

The MMS message contains two parts: the MMS header and the message body. The body is a multipart message body; hence each message part is built of a header and a body (see diagram below). As the MMS is a multipart message, we can actually include several images or files in each MMS.

To create the message connection and header we can use the following code:

This MMS is sent to a phone number and to an e-mail address.

String address = "mms://+1555444333";
msgConn = (MessageConnection)Connector.open(address);
           
mulMsg = (MultipartMessage)msgConn.newMessage(MessageConnection.MULTIPART_MESSAGE);

mulMsg.setAddress(address);
mulMsg.setAddress("mms://mail@mail.com");
mulMsg.setSubject("JAVA MMS TEST");

The headers "X-Mms-Delivery-Time" and "X-Mms-Priority" can be set using the setHeader method. Other header fields are accessible indirectly using the MultipartMessage object.

Here we add a text message to the MMS:

String mimeType = "text/plain";
String encoding = "UTF-8";
String text = "Hello from my java midlet";
byte[] contents = text.getBytes(encoding);
MessagePart msgPart = new MessagePart(contents, 0, contents.length, mimeType, "id1", "contentLocation", encoding);
mulMsg.addMessagePart(msgPart);

The MessagePart constructor will take all the header parameters and the message content as a byte array.

Here we add a picture to the MMS:

InputStream is = getClass().getResourceAsStream("image.jpg");
mimeType = "image/jpeg";
contents = new byte[is.available()];
is.read(contents);
msgPart = new MessagePart(contents, 0, contents.length, mimeType, "id2", "bg.jpg", null);
mulMsg.addMessagePart(msgPart);

When the message is created and the message parts are added to the message we can send the MMS. 

msgConn.send(mulMsg);

For more information download the JSR 205 (WMA 2.0) specification here>>

 

My Rating Score
Login to rate page