public Object invoke(Object obj, Object[] args) throws Exception {
    // input = (email address, subject, body, full attachment path)

    String email = (String) args[0].toString();
    String subject = (String) args[1].toString();
    String body = (String) args[2].toString();
    String attachment = (String) args[3].toString();
    String fName = attachment;
    byte[] data = null;
    FileConnection fconn = null;
    DataInputStream is = null;
    try {
      fconn = (FileConnection) Connector.open(fName, Connector.READ_WRITE);
      is = fconn.openDataInputStream();
      data = IOUtilities.streamToBytes(is);
      is.close();
      fconn.close();
    } catch (Exception ex) {
      Dialog.inform("Error in file path: " + ex.toString());
      return new Boolean(false);
    }
    // create a multipart
    Multipart mp = new Multipart();

    // create the file
    SupportedAttachmentPart sap =
        new SupportedAttachmentPart(mp, "application/x-example", attachment, data);

    TextBodyPart tbp = new TextBodyPart(mp, body);

    // 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]);
    Address toAdd = new Address(email, "my email");
    Address toAdds[] = new Address[1];
    toAdds[0] = toAdd;
    message.addRecipients(Message.RecipientType.TO, toAdds);
    message.setSubject(subject);
    message.setContent(mp);
    Session session = Session.getDefaultInstance();
    Transport trans = session.getTransport();
    // add recipients to the message and send
    boolean sent = false;
    try {
      trans.send(message);
      sent = true;
    } catch (Exception e) {
      Dialog.inform("Error while sending: " + e.toString());
    }

    return new Boolean(sent);
  }
Esempio n. 2
0
  /**
   * This method reads from a text file and returns its contents
   *
   * @param fName Name of the file
   * @return returns the contents of the file if the file is found; null otherwise
   */
  public String readTextFile(String fName) {
    String result = null;
    FileConnection fconn = null;
    DataInputStream is = null;

    // read the file
    try {

      fconn = (FileConnection) Connector.open("file:///SDCard/BlackBerry/" + fName);
      is = fconn.openDataInputStream();
      byte[] data = IOUtilities.streamToBytes(is);
      result = new String(data);
      is
          .close(); // http://www.blackberry.com/knowledgecenterpublic/livelink.exe/fetch/2000/348583/800451/800563/How_To_-_Close_connections.html?nodeid=1261294&vernum=0
      fconn.close();

    } catch (Exception e) {

      System.out.println(e.getMessage());
    }

    return result; // return the file contents
  }