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);
  }
示例#2
0
  public static String getDefaultMail() {
    Session session = Session.getDefaultInstance();
    if (session != null) {

      // now the store

      Store store = session.getStore();

      // then the service configuration

      ServiceConfiguration serviceConfig = store.getServiceConfiguration();

      // now get the email address

      return serviceConfig.getEmailAddress();
    }
    return "-";
  }