11

我想使用 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 参数来说是不可能的。
4

4 回答 4

3

For me this works nicely according to a tutorial on vogella.com. I also tried your minimal code sample and got no error during OLE client creation. I used SWT 4.3, by the way.

A little off-topic, but does it have to be Outlook? I mean, do you just want to automate e-mail sending - you could use JavaMail and do it headlessly, i.e. without automating an actual GUI client. The only reasons I can imagine for wishing to use Outlook or another e-mail client are:

  • You need the sent message in your out-box for reference.
  • Outlook is connected to an Exchange server which is configured to not accept SMTP connections as used by JavaMail.
  • You might want to compose a basic message and show it to the user so she can add attachments or edit the text interactively before sending.

But if it is just about automating e-mail sending, as I said I would recommend JavaMail.


Update: I downloaded SWT from its home page, in my case the latest stable release 4.3 for Windows. In the ZIP archive the file you need is swt.jar.

My sample code looks like this and is working fine:

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];
    }
}

I commented out the attachments part at the end and also the first OLE command because for me it also works without it. It does not do any damage to use it though, maybe you need it. Just give it a try.

The reason why I commented out the header "From" line is that it has no effect. For changing the sender you probably need another code snippet to switch either the Outlook profile or within a profile switch several preconfigured senders. By default it will just use your default profile.

Tell me if it helps.

于 2013-08-10T11:23:21.690 回答
0

com,

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

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

于 2014-10-25T12:03:07.067 回答
0

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

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

于 2017-11-23T11:44:50.730 回答
-1

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

<!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")

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

于 2013-08-13T12:46:03.127 回答