Java Reference
In-Depth Information
1.
Create one or more instances of
MessagePart
, passing the contents of each part, its
ID, and the part type to the
MessagePart
constructor.
2.
Create an instance of
MultipartMessage
by invoking
newMessage
on a
MessageConnection
instance.
3.
Add the parts you created in the first step to the
MultipartMessage
using
addMessagePart
.
4.
Set the message's subject (if any) using the
MultipartMessage
's
setSubject
method.
5.
Set the message's recipients using the
MultipartMessage
's
addAddress
method.
Listing 14-2 shows pseudocode for this sequence to send a single PNG image.
Listing 14-2.
Pseudocode to Send a Single PNG Image in a Multipart Message
String address = "mms://" + receiver + ":" + appId;
MessageConnection c = null;
try {
c = (MessageConnection) Connector.open(address);
MultipartMessage mpm = (MultipartMessage)c.newMessage(
MessageConnection.MULTIPART_MESSAGE);
mpm.setSubject("An image");
InputStream is = getClass().getResourceAsStream("/img/i.png");
byte[] bImage = new byte[is.available()];
is.read(bImage);
mpm.addMessagePart(new MessagePart(bImage, 0, bImage.length,
"image/png", "id1", null, null));
c.send(mpm);
} catch (Exception e) { /* recover */ }
finally {
if (c != null) {
try {
c.close();
} catch (IOException e) { /* recover */}
}
}
This code begins in the same manner as Listing 14-1, except that it creates a
MultipartMessage
instance instead of a
TextMessage
instance. Next, it sets its subject to
"An image"
. After that, it uses an
InputStream
instance to read the entire image into