在Netbeans上使用OLE Outlook的Java

在Netbeans上使用OLE Outlook的Java,java,eclipse,netbeans,ole,outlook-2007,Java,Eclipse,Netbeans,Ole,Outlook 2007,我目前正试图在Java应用程序中设置一个模块,使用MS Outlook 2007发送预定电子邮件(特别是jasper报告),我找到了eclipse的本教程,下载了引用库(swt.jar swt-debug.jar和org.eclipse.core.commands.jar for x64bit),并将它们添加到了我的Netbeans项目类路径中。 我从那个页面上复制了代码来尝试一下 public class SendMail{ public static void main(String

我目前正试图在Java应用程序中设置一个模块,使用MS Outlook 2007发送预定电子邮件(特别是jasper报告),我找到了eclipse的本教程,下载了引用库(swt.jar swt-debug.jar和org.eclipse.core.commands.jar for x64bit),并将它们添加到了我的Netbeans项目类路径中。 我从那个页面上复制了代码来尝试一下

public class SendMail{
    public static void main(String[] args) {
        SendMail outlook = new SendMail();
        try {
            outlook.execute();
        } catch (ExecutionException ex) {
            Logger.getLogger(SendMail.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public Object execute() throws ExecutionException {
        Display display = Display.getCurrent();
        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, "To", "my@email.com"); /*
         * Empty but could also be
         * predefined
         */

        setProperty(mail, "Bcc", "my@email.com"); /*
         * Empty but could also be
         * predefined
         */

        setProperty(mail, "BodyFormat", 2 /* HTML */);
        setProperty(mail, "Subject", "Top News for you");
        setProperty(mail, "HtmlBody",
                "<html>Hello<p>, please find some infos here.</html>");
        File file = new File("c:/temp/test.txt");
        if (file.exists()) {
            OleAutomation attachments = getProperty(mail, "Attachments");
            invoke(attachments, "Add", "c:/temp/test.txt");
        } else {
            JOptionPane.showMessageDialog(null, "Info.",
                    "Attachment not found; will send email with attachment", JOptionPane.QUESTION_MESSAGE);
        }
        invoke(mail, "Display" /* or "Send" */);
        return null;
    }

    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];
    }
}
公共类SendMail{
公共静态void main(字符串[]args){
SendMail outlook=新建SendMail();
试一试{
outlook.execute();
}捕获(ExecutionException ex){
Logger.getLogger(SendMail.class.getName()).log(Level.SEVERE,null,ex);
}
}
公共对象execute()引发ExecutionException{
Display=Display.getCurrent();
外壳=新外壳(显示);
油框架=新油框架(外壳,SWT.无);
//如果outlook尚未运行,则应启动它
OleClientSite=新的OleClientSite(框架,SWT.NONE,“OVCtl.OVCtl”);
地点:多佛尔布(OLE.OLEIVERB_INPLACEACTIVATE);
//现在获取outlook应用程序
OleClientSite2=新的OleClientSite(框架,SWT.NONE,
“Outlook.Application”);
OleAutomation outlook=新的OleAutomation(站点2);
// 
OleAutomation mail=invoke(outlook,“CreateItem”,0/*邮件项目*/)
.getAutomation();
setProperty(邮件,“收件人”my@email.com"); /*
*空的,但也可能是空的
*预定义的
*/
setProperty(邮件,“密件抄送”和“my@email.com"); /*
*空的,但也可能是空的
*预定义的
*/
setProperty(邮件,“BodyFormat”,2/*HTML*/);
setProperty(邮件,“主题”,“你的头条新闻”);
setProperty(邮件,“HtmlBody”,
“你好,请在这里找到一些信息。”);
File File=新文件(“c:/temp/test.txt”);
if(file.exists()){
OleAutomation attachments=getProperty(邮件,“附件”);
调用(附件,“添加”,“c:/temp/test.txt”);
}否则{
JOptionPane.showMessageDialog(null,“Info.”,
“未找到附件;将发送带有附件的电子邮件”,JOptionPane.QUESTION_消息);
}
调用(邮件、“显示”/*或“发送”*/);
返回null;
}
私有静态OleAutomation getProperty(OleAutomation auto,字符串名称){
Variant varResult=auto.getProperty(property(auto,name));
if(varResult!=null&&varResult.getType()!=OLE.VT\u EMPTY){
OleAutomation结果=varResult.getAutomation();
varResult.dispose();
返回结果;
}
返回null;
}
专用静态变量调用(OleAutomation、自动、字符串命令、,
字符串值){
返回auto.invoke(属性(auto,command),
新变量[]{新变量(值)};
}
私有静态变量调用(OleAutomation,String命令){
返回auto.invoke(属性(auto,command));
}
私有静态变量调用(自动、字符串命令、int值){
返回auto.invoke(属性(auto,command),
新变量[]{新变量(值)};
}
私有静态布尔setProperty(OleAutomation,字符串名称,
字符串值){
返回auto.setProperty(属性(auto,name),新变量(value));
}
私有静态布尔setProperty(OleAutomation,字符串名称,
整数值){
返回auto.setProperty(属性(auto,name),新变量(value));
}
私有静态int属性(OleAutomation,字符串名称){
返回auto.getIDsOfNames(新字符串[]{name})[0];
}
}
但它显示了以下错误

Exception in thread "main" org.eclipse.swt.SWTException: Failed to create Ole Client. result = -2147221164
    at org.eclipse.swt.ole.win32.OLE.error(OLE.java:302)
    at org.eclipse.swt.ole.win32.OleClientSite.<init>(OleClientSite.java:242)
    at tutorialjavamail.SendMail.execute(SendMail.java:44)
    at tutorialjavamail.SendMail.main(SendMail.java:33)
Java Result: 1
线程“main”org.eclipse.swt.swt异常:无法创建Ole客户端。结果=-2147221164 位于org.eclipse.swt.ole.win32.ole.error(ole.java:302) 在org.eclipse.swt.ole.win32.OleClientSite上(OleClientSite.java:242) 在tutorialjavamail.SendMail.execute(SendMail.java:44) 位于tutorialjavamail.SendMail.main(SendMail.java:33) Java结果:1
我刚开始使用java处理电子邮件,但我已经查找了JavaMail,但显然它与Microsoft Exchange Server不兼容。

Microsoft目前不建议也不支持从任何无人参与、非交互的客户端应用程序或组件自动化Microsoft Office应用程序(包括ASP、ASP.NET、DCOM和NT服务),因为在此环境中运行Office时,Office可能会表现出不稳定的行为和/或死锁。

如果您正在构建一个在服务器端上下文中运行的解决方案,那么您应该尝试使用安全的组件进行无人值守的执行。或者,您应该尝试找到至少允许部分代码在客户端运行的替代方案。如果您使用服务器端解决方案中的Office应用程序,那么该应用程序将缺少许多e成功运行所必需的功能。此外,您将面临整体解决方案稳定性方面的风险


你可以在文章中阅读更多关于这方面的内容。

哪一行代码引发了异常?
olleclientsite=new-olleclientsite(frame,SWT.NONE,“OVCtl.OVCtl”);
1,我想我的问题是我找不到该progID,我在某个地方读到它在服务器端启动进程,而不是作为Outlook客户端(我将在下面运行代码的地方),我就是找不到Outlook 2007的progID,即使在Regedit上也找不到。谢谢您的回答,但是我实际上是在尝试从客户端计算机访问客户端(读作,我只想在本地机上打开客户端Outlook并通过那里发送邮件)。