Create an attachment

by Retired ‎02-16-2010 12:58 PM - edited ‎02-17-2010 03:53 PM (3,956 Views)

Summary


This article applies to the BlackBerry® wireless devices based on Java™.




Description


The following code sample shows how to first create a simple HTML attachment, add it to a message, and then send the message.



//create a multipart
Multipart mp = new Multipart();

//data for the content of the file
String fileData = "<html>just a simple test</html>";
String messageData = "Mail Attachment Demo";

//create the file
SupportedAttachmentPart sap = new SupportedAttachmentPart(mp,"text/html","file.html",fileData.getBytes());

TextBodyPart tbp = new TextBodyPart(mp,messageData);

//add the file to the multipart
mp.addBodyPart(tbp);
mp.addBodyPart(sap);

//create a message in the sent items folder
Folder folders[] = Session.getDefaultInstance().getStore().list(Folder.SENT);

Message message = new Message(folders[0]);

//add recipients to the message and send
try {
Address toAdd = new Address("email@company.com","my email");
Address toAdds[] = new Address[1];
toAdds[0] = toAdd;
message.addRecipients(Message.RecipientType.TO,toAdds);
message.setContent(mp);

Transport.send(message);
} catch (Exception e) {
Dialog.inform(e.toString());
}