- 19.1 Why Messaging?
- 19.2 Sending Messages
- 19.3 Sending Binary Messages
- 19.4 Sending Multipart Messages
- 19.5 Receiving Messages
- 19.6 A Simple Messaging Application
- 19.7 Summary
19.4 Sending Multipart Messages
MMS is the next generation of SMS. It enables larger messages, with multiple addresses, a subject, and different parts. In essence, you can use MMS to send one or more files from one device to another. One powerful application for MMS is sending pictures you have just taken with your phone's camera around the world to your friends. Be aware that getting MMS working between different carriers might be a challenge.
The exact size limit on MMS messages depends on the phone and the wireless carrier. Maximum sizes of 100 kB are typical, but some phones and carriers can go as high as 300 kB or 1 MB.
JSR 205, WMA 2.0, adds support for MMS with the MultipartMessage class, a sibling of TextMessage and BinaryMessage. A MultipartMessage keeps track of multiple addresses, a message subject, and some number of MessageParts, which are essentially files with associated content types.
Before you learn about MessagePart, take a look at the basic structure for sending a MultipartMessage.
public void sendMultipart(String address, String subject, MessagePart[] parts) throws IOException, InterruptedException { String cs = "mms://+" + address + ":com.jonathanknudsen.Hermes"; MessageConnection mc = (MessageConnection) Connector.open(cs); MultipartMessage mm = (MultipartMessage) mc.newMessage(MessageConnection.MULTIPART_MESSAGE); mm.setSubject(subject); for (int i = 0; i < parts.length; i++) mm.addMessagePart(parts[i]); mc.send(mm); }
This should look familiar by now: get a MessageConnection, get a message, populate it, and kick it out the door.
The connection string is a little different for multipart messages. It starts with mms instead of sms, for one thing, and it includes an application identifier, which is a lot like the port number for SMS messages, but more reliable. In SMS messages, port numbers are used to map to a specific application. For example, if you send a message on port 50,000, you're expecting the receiving application to be listening on port 50,000. There are lots of port numbers from which to choose, but let's face it: sooner or later some kids in a garage are going to write an application that uses the same port number as yours. No central registry exists that ensures port numbers do not overlap between applications.
Instead of port numbers, MMS messages use an application identifier. Messages are sent and received for a specific application identifier. The convention for application identifiers is an inverted domain name plus an application name. In the earlier example, I used com.jonathanknudsen.Hermes to identify my application. The chances of someone else using the same application identifier accidentally are very slim. This is a good system, but note that the application identifier can be a maximum of 32 characters, which is kind of short.
MMS messages are souped-up compared to SMS messages. You can set a message subject, for one thing. Furthermore, you can add more destination addresses. In the earlier example, the message is going to a single address as specified in the connection string. Use addAddress() in MultipartMessage to supply more addresses. You have to specify both an address type and the address itself. The address type is a string, one of to, cc, or bcc, which have the same meanings as for e-mail.
Each file in a multipart message has a content type, a content ID, an optional location, and an optional encoding scheme. The content type is specified as a MIME type, the same type scheme used by Web servers and in e-mail attachments. The content ID should be unique for each part of a multipart message. The location is usually just a filename, and the encoding scheme is useful if you are including encoded text in a part.
Here's a simple case, which encodes a Unicode string using UTF-8 and creates a MessagePart for a text string:
private MessagePart makePart(String text, String id) throws SizeExceededException, UnsupportedEncodingException { String encoding = "UTF-8"; byte[] encoded = text.getBytes(encoding); return new MessagePart(encoded, "text/plain", id, null, encoding); }
This method creates a MessagePart from a byte array. A more common case is to create a MessagePart from an InputStream. The other parameters in the constructor are the same. Here is a method that creates a MessagePart from a resource file in the MIDlet suite:
private MessagePart makePart(String filename, String type, String id) throws IOException, SizeExceededException { InputStream in = this.getClass().getResourceAsStream(filename); return new MessagePart(in, type, id, filename, null); }