示例#1
0
        @Override
        protected boolean composeNative(String to, String subject, String body, File... attachments)
            throws IOException, InterruptedException {
          final DesktopEnvironment de = DesktopEnvironment.getDE();
          final File vbs = FileUtils.getFile(EmailClient.class.getResource("OutlookEmail.vbs"));
          final List<String> l = new ArrayList<String>(6);
          l.add("cscript");
          l.add(de.quoteParamForExec(vbs.getAbsolutePath()));
          if (to != null) l.add(createVBParam("to", to));
          if (subject != null) l.add(createVBParam("subject", subject));
          // at least set a parameter otherwise the usage get displayed
          l.add(createVBParam("unicodeStdIn", "1"));
          for (File attachment : attachments) {
            l.add(de.quoteParamForExec(attachment.getAbsolutePath()));
          }

          final Process process = new ProcessBuilder(l).start();
          // VBScript only knows ASCII and UTF-16
          final Writer writer =
              new BufferedWriter(
                  new OutputStreamWriter(process.getOutputStream(), StringUtils.UTF16));
          writer.write(body);
          writer.close();
          final int returnCode = process.waitFor();
          if (returnCode != 0)
            throw new IllegalStateException("Non zero return code: " + returnCode);
          return true;
        }
示例#2
0
  // see http://kb.mozillazine.org/Command_line_arguments_(Thunderbird)
  // The escape mechanism isn't specified, it turns out we can pass percent encoded strings
  private static final String getTBParam(
      final String to, final String subject, final String body, final File... attachments) {
    // "to='[email protected],[email protected]',cc='*****@*****.**',subject='dinner',body='How
    // about dinner
    // tonight?',attachment='file:///C:/cygwin/Cygwin.bat,file:///C:/cygwin/Cygwin.ico'";

    final List<String> l = new ArrayList<String>(4);
    if (to != null) l.add(createEncodedParam("to", to));
    if (subject != null) l.add(createEncodedParam("subject", subject));
    if (body != null) l.add(createEncodedParam("body", body));
    final List<String> urls = new ArrayList<String>(attachments.length);
    for (final File attachment : attachments) {
      // Thunderbird doesn't parse java URI file:/C:/
      final String rawPath = attachment.toURI().getRawPath();
      // handle UNC paths
      final String tbURL = (rawPath.startsWith("//") ? "file:///" : "file://") + rawPath;
      urls.add(tbURL);
    }
    l.add(createEncodedParam("attachment", CollectionUtils.join(urls, ",")));

    return DesktopEnvironment.getDE().quoteParamForExec(CollectionUtils.join(l, ","));
  }
示例#3
0
  private static EmailClient findPreferred() throws IOException {
    final DesktopEnvironment de = DesktopEnvironment.getDE();
    if (de instanceof Windows) {
      // Tested on XP and 7
      // <SANS NOM> REG_SZ "C:\Program Files\Mozilla
      // Thunderbird\thunderbird.exe" -osint -compose "%1"
      final String out =
          cmdSubstitution("reg", "query", "HKEY_CLASSES_ROOT\\mailto\\shell\\open\\command");

      final Matcher registryMatcher = registryPattern.matcher(out);
      if (registryMatcher.find()) {
        final String cmdLine = registryMatcher.group(1);
        if (cmdLine.contains("thunderbird")) {
          return new ThunderbirdCommandLine(cmdLine, "%1");
        } else if (cmdLine.toLowerCase().contains("outlook")) {
          return Outlook;
        }
      }
    } else if (de instanceof Mac) {
      // (
      // {
      // LSHandlerRoleAll = "com.apple.mail";
      // LSHandlerURLScheme = mailto;
      // }
      // )
      final String bundleID;
      final String dict =
          cmdSubstitution("defaults", "read", "com.apple.LaunchServices", "LSHandlers");
      final Matcher dictMatcher = dictPattern.matcher(dict);
      if (dictMatcher.find()) {
        // LSHandlerRoleAll can be before or after LSHandlerURLScheme
        final String before = dictMatcher.group(1);
        final String after = dictMatcher.group(2);
        assert before == null ^ after == null
            : "Both before and after, or neither before nor after: " + before + " and " + after;
        bundleID = before != null ? before : after;
      } else
        // the default
        bundleID = AppleMailBundleID;

      if (bundleID.equals(AppleMailBundleID)) {
        return AppleMail;
      } else if (bundleID.equals(ThunderbirdBundleID)) {
        // doesn't work if Thunderbird is already open:
        // https://bugzilla.mozilla.org/show_bug.cgi?id=424155
        // https://bugzilla.mozilla.org/show_bug.cgi?id=472891
        // MAYBE find out if launched and let handled=false

        final File appDir = ((Mac) de).getAppDir(bundleID);
        final File exe = new File(appDir, "Contents/MacOS/thunderbird-bin");

        return new ThunderbirdPath(exe);
      }
    } else if (de instanceof Gnome) {
      // evolution %s
      final String cmdLine =
          cmdSubstitution("gconftool", "-g", "/desktop/gnome/url-handlers/mailto/command");
      if (cmdLine.contains("thunderbird")) {
        return new ThunderbirdCommandLine(cmdLine, "%s");
      }
    } else if (de instanceof KDE) {
      // TODO look for EmailClient=/usr/bin/thunderbird in
      // ~/.kde/share/config/emaildefaults or /etc/kde (ou /usr/share/config qui est un
      // lien symbolique vers /etc/kde)
    }

    return MailTo;
  }
示例#4
0
 private static String cmdSubstitution(String... args) throws IOException {
   return DesktopEnvironment.cmdSubstitution(Runtime.getRuntime().exec(args));
 }