当它在 Eclipse 中工作时,为什么我不能在 64 位系统上使用 JAR 打开 32 位 Outlook...?

Why cant I open 32bit Outlook on a 64bit System using a JAR when it worked in Eclipse...?

我正在开发的 Java 应用程序是供其他人用来帮助我们进行日常活动的内部工具。使用该工具时(在调用下面代码的 JButton 按下时)我希望它为用户打开一个新的 Outlook 电子邮件 see/edit.

起初我是在 64 位 Eclipse 中开发这个应用程序的,尽管我进行了所有研究,但仍无法让 SWT 打开 Outlook。在我遇到一些问题 运行 64 位与 32 位 SWT 之后,我有了检查 Outlook 的想法,果然公司使用的是 32 位。我加载了一个 32 位的 eclipse,导入了我的项目,将 SWT 切换到 32 位,它完全按预期工作。

我注意到 运行 的进程是 javaw.exe*32,但 64 位 Eclipse 正在使用进程 javaw.exe。我从 32 位 Eclipse 导出了 JAR 并试了一下,但没有出现 EMail。我检查了 JRE 的安装并看到了 32 位和 64 位,但我的公司有一项政策,在 Java 控制面板中只强制使用 64 位 JRE。我与其他一些人一起工作,并安装并启用了 here。 JAR 仍然无法打开电子邮件。我什至尝试禁用 64 位,但它仍然不起作用。

有什么办法可以补救这种情况吗?如果我可以更好地阐述或提供更多信息,请告诉我!

package EDM_PKG;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.ole.win32.OLE;
import org.eclipse.swt.ole.win32.OleAutomation;
import org.eclipse.swt.ole.win32.OleClientSite;
import org.eclipse.swt.ole.win32.OleFrame;
import org.eclipse.swt.ole.win32.Variant;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class CreateEmail {
private static Display display = new Display();
public static void sendEMail(String env) {
    //String currUser = System.getProperty("user.name");
    String msg = getMessage(env);
    String sub = getSubject(env);
    Shell shell = new Shell(display);
    OleFrame frame = new OleFrame(shell, SWT.NONE);

    // This should start outlook if it is not running yet
    //OleClientSite site = new OleClientSite(frame, SWT.NONE,"OVCtl.OVCtl");
    //site.doVerb(OLE.OLEIVERB_INPLACEACTIVATE);

    // Now get the outlook application
    OleClientSite site2 = new OleClientSite(frame, SWT.NONE, "Outlook.Application");
    OleAutomation outlook = new OleAutomation(site2);

    OleAutomation mail = invoke(outlook, "CreateItem", 0 /* Mail item */).getAutomation();

    setProperty(mail, "BodyFormat", 2 /* HTML */);
    setProperty(mail, "Subject", sub);
    setProperty(mail, "To", "example@gmail.com");
    setProperty(mail, "HtmlBody", msg);

    invoke(mail, "Display" /* or "Send" */);
    display = null;
}
private static String getMessage(String env) {
    String msg = "";
    if (env.equalsIgnoreCase("quas")) {
        msg = "<html><body>The <b>USER</b> excel has been migrated to <b>QUAS.</b><br><br> This email was generated by Excel Datatable Migrator.</body></html>";
    } else if (env.equalsIgnoreCase("prod")) {
        msg = "<html><body>The <b>QUAS</b> excel has been migrated to <b>PROD.</b><br><br>This email was generated by Excel Datatable Migrator.</body></html>";
    }  else {
        msg = "Somthing happened with the automated message of EDM. Please contact the user with the eCode: "+System.getProperty("user.name")+".</body></html>";
    }

    return msg;
}
private static String getSubject(String env) {
    String sub = "";
    if (env.equalsIgnoreCase("quas")) {
        sub = "EDM has been used to move USER to QUAS...";
    } else if (env.equalsIgnoreCase("prod")) {
        sub = "EDM has been used to move QUAS to PROD...";
    }  else {
        sub = "Somthing didnt quite work right...";
    }

    return sub;
}
private static OleAutomation getProperty(OleAutomation auto, String name) {
    Variant varResult = auto.getProperty(property(auto, name));
    if (varResult != null && varResult.getType() != OLE.VT_EMPTY) {
        OleAutomation result = varResult.getAutomation();
        varResult.dispose();
        return result;
    }
    return null;
}

private static Variant invoke(OleAutomation auto, String command,
        String value) {
    return auto.invoke(property(auto, command),
            new Variant[] { new Variant(value) });
}

private static Variant invoke(OleAutomation auto, String command) {
    return auto.invoke(property(auto, command));
}

private static Variant invoke(OleAutomation auto, String command, int value) {
    return auto.invoke(property(auto, command),
            new Variant[] { new Variant(value) });
}

private static boolean setProperty(OleAutomation auto, String name,
        String value) {
    return auto.setProperty(property(auto, name), new Variant(value));
}

private static boolean setProperty(OleAutomation auto, String name,
        int value) {
    return auto.setProperty(property(auto, name), new Variant(value));
}

private static int property(OleAutomation auto, String name) {
    return auto.getIDsOfNames(new String[] { name })[0];
}
}

不用重量级难用Ole类。 如果您只想发送电子邮件,只需调用:

Program.launch("mailto:bla@blubb.com&subject=Subject&body=Me‌​ssage here");

这适用于所有架构和操作系统。