Send a message with an attachment

  1. Import the required classes and interfaces.
    import net.rim.blackberry.api.mail.Message;
    import net.rim.blackberry.api.mail.MessagingException;
    import net.rim.blackberry.api.mail.Multipart;
    import net.rim.blackberry.api.mail.Session;
    import net.rim.blackberry.api.mail.SupportedAttachmentPart;
    import net.rim.blackberry.api.mail.Transport;
    
  2. Create a new Multipart object to create a multipart message.
    byte[] data = new byte[256]; 
    MultiPart multipart = new MultiPart();
  3. Create a SupportedAttachmentPart object, designating the Multipart object as its parent, to create each component of the screen.
    SupportedAttachmentPart attach = new SupportedAttachmentPart( multipart,
    "application/x-example", "filename", data);
  4. Invoke MultiPart.addBodyPart(SupportedAttachmentPart) to add each supportedAttachmentPart object to the multipart object.
    multipart.addBodyPart(attach); 
  5. Invoke Message.setContent(Multipart) and provide as a parameter the Multipart object to set the content of the attachment.
    msg.setContent(multipart);
  6. Invoke Session.getTransport() and store the returned object in a variable of type Transport. The Transport object represents the messaging transport protocol.
    Transport trans = Session.getTransport();
  7. Invoke Transport.send(Message).
    try 
    {
       trans.send(msg);
    } 
    catch(MessagingException e) 
    {
       System.out.println(e.getMessage());
    }

Was this information helpful? Send us your comments.