如何在Outlook中创建电子邮件并使其对用户可见

Lok*_*oki 11 java email outlook ole

我想使用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");
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

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)
Run Code Online (Sandbox Code Playgroud)

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

编辑

好吧,如果没有人知道为什么OLE不适合我,我还有另外一个问题.是否有可能,或者是否有一个库,可以创建一个Outlook电子邮件并进行设置(主题,正文等)但不发送它但是让用户可以看到更改内容?

编辑2

x86和x64 jar文件没有运行,同样的错误.我还得到了最新版本的SWT for x86和x64.操作系统也是x64和java,所以我不能使用x86 SWT库.使用x64时会出现上述错误.Outlook版本为15(Outlook 2013).

希望这有帮助吗?

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

  • 我想跟踪进程的状态.我想知道电子​​邮件什么时候关闭/发送.
  • 我想将一张图片(BufferedImage)从剪贴板中插入到Body中,这对于mailto参数来说根本不可能.

kri*_*aex 3

对我来说,根据vogella.com 上的教程,这非常有效。我还尝试了您的最小代码示例,并且在 OLE 客户端创建期间没有出现错误。顺便说一下,我使用的是 SWT 4.3。

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

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

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


更新:我从其主页下载了 SWT,在我的例子中是 Windows 的最新稳定版本 4.3 。在 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];
    }
}
Run Code Online (Sandbox Code Playgroud)

我在最后注释掉了附件部分以及第一个 OLE 命令,因为对我来说,没有它它也可以工作。不过使用它不会造成任何损害,也许你需要它。尝试一下吧。

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

告诉我是否有帮助。