예제 #1
0
  public static final void main(String[] args) throws Exception {
    if (args.length == 1 && "--help".equals(args[0])) {
      System.out.println(
          "Usage: java [-Dparam=value] " + EmailClient.class.getName() + " [EmailClientType args]");
      System.out.println("\tEmailClientType: mailto or " + Arrays.asList(EmailClientType.values()));
      System.out.println("\tparam: to, subject, body, files (seprated by ',' double it to escape)");
      return;
    }

    final EmailClient client = createFromString(args);
    final String to =
        System.getProperty("to", "Pierre Dupond <*****@*****.**>, [email protected]");
    // ',to=' to test escaping of Thunderbird (passing subject='foo'bar' works)
    final String subject =
        System.getProperty("subject", "Sujé € du courrier ',to='&;\\<> \"autre'\n2nd line");
    final String body =
        System.getProperty("body", "Bonjour,\n\tsingle ' double \" backslash(arrière) \\ slash /");
    final String filesPath = System.getProperty("files");
    final String[] paths =
        filesPath == null || filesPath.length() == 0
            ? new String[0]
            : filesPath.split("(?<!,),(?!,)");
    final File[] f = new File[paths.length];
    for (int i = 0; i < f.length; i++) {
      f[i] = new File(paths[i].replace(",,", ","));
    }
    client.compose(to, subject, body, f);
  }
예제 #2
0
  private static final EmailClient createFromString(final String... args) throws IOException {
    // switch doesn't support null
    if (args.length == 0) return getPreferred();
    else if ("mailto".equals(args[0])) return MailTo;

    final EmailClientType t = EmailClientType.valueOf(args[0]);
    switch (t) {
      case Outlook:
        return Outlook;
      case AppleMail:
        return AppleMail;
      case Thunderbird:
        if (args.length == 2) return Thunderbird.createFromExe(new File(args[1]));
        else if (args.length == 3) return Thunderbird.createFromCommandLine(args[1], args[2]);
        else throw new IllegalArgumentException(t + " needs 1 or 2 arguments");
      default:
        throw new IllegalStateException("Unknown type " + t);
    }
  }