【问题标题】:How to create an E-Mail in Outlook and make it visible for the User如何在 Outlook 中创建电子邮件并使其对用户可见
【发布时间】:2013-08-06 03:16:19
【问题描述】:

我想通过使用 Outlook 和 OLE 客户端的 Java 应用程序创建电子邮件。

我搜索了一些例子,发现了很多。它们都以相同的方式开始:

创建显示、外壳、OLE 框架和 OLE 客户端站点。

但我在这几个步骤中遇到了错误:

Display display = new Display();
Shell shell = new Shell(display);

shell.setText("Outlook Automation");
shell.setLayout(new FillLayout());

OleFrame frm = new OleFrame(shell, SWT.NONE);

OleClientSite site = new OleClientSite(frm, SWT.NONE,
                "Outlook.Application");

我收到以下错误:

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 outlooktest.Main.main(Main.java:27)

我不知道 OLE,我不确定我做错了什么。我缺少一些依赖项吗?有人知道这个错误可能是什么吗?我用谷歌搜索了错误代码,但没有找到任何东西。

编辑

如果没有人知道为什么 OLE 对我不起作用,我还有另一个问题。是否可以创建 Outlook 电子邮件并设置它(主题、正文等)但不发送但让用户可以看到它来更改内容,或者是否有库?

编辑 2

x86 和 x64 jar 文件无法正常工作,同样的错误。我还获得了适用于 x86 和 x64 的最新版本的 SWT。操作系统也是 x64 和 java,所以我不能使用 x86 SWT 库。对于 x64,会发生上述错误。 Outlook 版本为 15(Outlook 2013)。

希望这有帮助吗?

我通过 Processbuilder 创建了电子邮件,但只能使用 mailto: 参数。这里的问题如下:

  • 我想跟踪进程的状态。我想知道电子​​邮件何时关闭/发送。
  • 我想将剪贴板中的图片 (BufferedImage) 插入正文中,而使用 mailto 参数是不可能的。

【问题讨论】:

  • 您的目标是向您的用户发送电子邮件,以便用户在他的邮箱中找到该电子邮件吗?如果是,则使用 JavaMail API。
  • 我想创建电子邮件供用户查看和编辑。在他的邮箱里不适合他。它应该只打开一个填写了一些字段的新邮件。用户自己可以更改所有内容,然后单击发送。而且我无权访问 Exchange Server 的 SMTP。这就是我不能使用 JavaMail 的原因。
  • 您可以使用 `java.awt.Desktop.mail(URI)' 来启动用户默认邮件客户端的邮件撰写窗口。请参阅stackoverflow.com/a/2357924/1369991 了解更多信息。
  • 我知道这是旧的,但仍列为未答复。如果它看起来合适,请您接受并支持我的回答吗?谢谢。
  • 很抱歉这么说,但您的回答对我一点帮助都没有。它仍然没有答案,我找到了一个解决方法,不过,我不再从事这个项目了。很抱歉,但由于您的答案不是我想要的,所以我不会接受它,也不会投票。

标签: java email outlook ole


【解决方案1】:

您的 MS Outlook 可能是 32 位 (x86)。所以 64 位 (x64) SWT 无法启动 Outlook。您需要使用不能在 64 位 JVM 上运行的 32 位 SWT Jar 文件。所以你需要安装 32-Bit JVM (JRE)。

即使您运行的是 64 位 Windows,您仍然可以下载并安装 32 位 (x86) JRE 并运行您的应用程序。

【讨论】:

  • 不,Outlook 是一个进程外 COM 库。您可以从 32 位进程访问 64 位版本的 Outlook,反之亦然。
【解决方案2】:

com,

System.Diagnostics.Process.Start("mailto:someone@example.com?Subject=Hello%20again&body=your%20textBody%20here")

使用上述代码打开 Outlook 邮件时,会使用预定义的 mailto、邮件主题和正文,请您解释一下我们如何在 CC 中添加地址。

【讨论】:

    【解决方案3】:

    根据tutorial on vogella.com,这对我来说效果很好。我还尝试了您的最小代码示例,并且在 OLE 客户端创建过程中没有出错。顺便说一句,我使用的是 SWT 4.3。

    有点离题,但一定是 Outlook 吗?我的意思是,您是否只想自动化发送电子邮件 - 您可以使用 JavaMail 并无头地进行,即无需自动化实际的 GUI 客户端。我想使用 Outlook 或其他电子邮件客户端的唯一原因是:

    • 您需要发件箱中发送的消息以供参考。
    • Outlook 已连接到配置为不接受 JavaMail 使用的 SMTP 连接的 Exchange 服务器。
    • 您可能希望编写基本消息并将其显示给用户,以便她可以在发送前添加附件或以交互方式编辑文本。

    但如果只是关于自动发送电子邮件,正如我所说,我会推荐 JavaMail。


    更新:我从它的 home page 下载了 SWT,在我的例子中是最新的稳定版 release 4.3 for Windows。在 ZIP 存档中,您需要的文件是 swt.jar

    我的示例代码如下所示,并且运行良好:

    package de.scrum_master.ole;
    
    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 OutlookMail {
        public static void main(String[] args) {
            sendEMail();
        }
    
        public static void sendEMail() {
            Display display = new Display();
            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", "My test subject");
    //      setProperty(mail, "From", "my@sender.org");
            setProperty(mail, "To", "<John Doe> my@recipient.org");
            setProperty(mail, "HtmlBody", "<html><body>This is an <b>HTML</b> test body.</body></html>");
    
    //      if (null != attachmentPaths) {
    //          for (String attachmentPath : attachmentPaths) {
    //              File file = new File(attachmentPath);
    //              if (file.exists()) {
    //                  OleAutomation attachments = getProperty(mail, "Attachments");
    //                  invoke(attachments, "Add", attachmentPath);
    //              }
    //          }
    //      }
    
            invoke(mail, "Display" /* or "Send" */);
    
        }
    
        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 命令,因为对我来说,没有它它也可以工作。使用它不会造成任何损害,也许你需要它。试试看吧。

    我注释掉标题“From”行的原因是它没有效果。要更改发件人,您可能需要另一个代码 sn-p 来切换 Outlook 配置文件或在配置文件中切换多个预配置的发件人。默认情况下,它只会使用您的默认配置文件。

    如果有帮助,请告诉我。

    【讨论】:

    • 第 2 点和第 3 点是正确的 ;)。我想给用户某种模板,他可以在其中编辑一些文本。在我的公司中,我们使用 Outlook,这就是为什么它必须是 Outlook。我是否需要创建这个 Eclipse RCP 项目才能让 OLE 工作,就像在 Vogella 教程中一样?
    • 不,RCP 不是必需的。我创建了一个普通的 Java 项目,只下载了 SWT 库(JAR 文件)。顺便说一句,这是我第一次尝试使用 Java 中的 SWT 类和 OLE,所以如果我设法做到了,那一定非常简单。 ;)
    • 我也下载了。并使用了图书馆。我有 x64。但没有任何效果。你能把你的下载链接发给我吗?也许我的版本有误。
    • 对不起,我在路上。我现在用下载说明和示例代码更新了我的答案。如果 OLE 不适合您,您可能需要修复您的设置,而不是 Java 代码。但这只是一个假设。还请使用有关操作系统、Java 版本、SWT 版本、Outlook 版本的详细信息更新您的问题。也许你有问题。
    • 对我不起作用。好吧,我在项目中使用的一些 dll 的 x64 和 x86 文件有问题。我现在正在尝试创建 .jar,即使代码给了我错误。也许它只是作为罐子工作。当我尝试运行 x86 版本时出现错误,但我认为如果它是使用 x86 SWT 版本的 .jar 文件,它可以工作。
    【解决方案4】:

    如果您在网络上使用某些东西,这可以帮助您:

    <!DOCTYPE html>
    <html>
    <body>
    
    <p>
    This is an email link:
    <a href="mailto:someone@example.com?Subject=Hello%20again&body=your%20textBody%20here" target="_top">
    Send Mail</a>
    </p>
    
    <p>
    <b>Note:</b> Spaces between words should be replaced by %20 to ensure that the browser will display the text properly.
    </p>
    
    </body>
    </html>
    

    但是在应用程序中你可以启动一个进程mailto:

    喜欢

    System.Diagnostics.Process.Start("mailto:someone@example.com?Subject=Hello%20again&body=your%20textBody%20here")
    

    它适用于所有电子邮件客户端

    【讨论】:

    • 它回答了“编辑好吧,如果没有人知道为什么 OLE 对我不起作用,我还有另一个问题。是否有可能,或者是否有一个库来创建 Outlook 电子邮件并设置它(主题、正文等)但不发送它但让用户可以看到它来更改内容?”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-13
    • 1970-01-01
    • 2010-11-30
    • 2017-10-28
    • 1970-01-01
    • 2013-07-30
    • 2010-11-20
    相关资源
    最近更新 更多