如何在Outlook中创建电子邮件并使其可见的用户 [英] How to create an E-Mail in Outlook and make it visible for the User

查看:427
本文介绍了如何在Outlook中创建电子邮件并使其可见的用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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



创建显示,Shell,OLE框架和OLE客户端站点。



但是,我收到以下几个错误:

 显示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);

我收到以下错误:

 线程main中的异常org.eclipse.swt.SWTException:无法创建Ole Client。 result = -2147221164 
在org.eclipse.swt.ole.win32.OLE.error(OLE.java:302)
在org.eclipse.swt.ole.win32.OleClientSite。< init> (OleClientSite.java:242)
at outlooktest.Main.main(Main.java:27)

我不知道OLE,我不知道我做错了什么。有没有一些依赖我失踪了?有人知道这个错误可能是什么吗?我搜索错误代码,但没有找到任何内容。



编辑



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



编辑2



x86和x64 jar文件没有解决,同样的错误。另外我得到了最新版本的SWT for x86和x64。 OS也是x64和java,所以我不能使用x86 SWT库。使用x64会发生上述错误。 Outlook版本是15(Outlook 2013)。



希望这有帮助吗?



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




  • 我想跟踪进程的状态。我想知道电子​​邮件何时关闭/发送。

  • 我想将一张图片(BufferedImage)从剪贴板中插入到Body中,这完全不可能用mailto参数。


解决方案

对于我来说,根据 vogella.com上的教程。我也尝试过您的最小代码示例,并在OLE客户端创建过程中没有发生错误。我使用了SWT 4.3。



有点偏离主题,但它必须是Outlook?我的意思是,您只想自动发送电子邮件 - 您可以使用 JavaMail 并且无头衔,即不自动化一个实际的GUI客户端。想要使用Outlook或另一个电子邮件客户端的唯一原因是:




  • 您需要在您的外部邮件中发送邮件,

  • Outlook连接到配置为不接受由JavaMail使用的SMTP连接的Exchange服务器。

  • 您可能需要撰写基本消息并向用户显示,以便在发送之前可以交互地添加附件或编辑文本。



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






更新:我从其主页下载SWT,在我的情况下,最新的稳定版版本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){
sendMail();
}

public static void sendEMail(){
显示display = new Display();
Shell shell = new Shell(display);
OleFrame frame = new OleFrame(shell,SWT.NONE);

//如果没有运行,应该启动outlook
// OleClientSite site = new OleClientSite(frame,SWT.NONE,OVCtl.OVCtl);
// site.doVerb(OLE.OLEIVERB_INPLACEACTIVATE);

//现在得到outlook应用程序
OleClientSite site2 = new OleClientSite(frame,SWT.NONE,Outlook.Application);
OleAutomation outlook = new OleAutomation(site2);

OleAutomation mail = invoke(outlook,CreateItem,0 / *邮件项* /)。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>这是< b> HTML< / b>测试体。< / body>< / html>);

// if(null!= attachmentPaths){
// for(String attachmentPath:attachmentPaths){
// File file = new File(attachmentPath);
// if(file.exists()){
// OleAutomation attachments = getProperty(mail,Attachments);
// invoke(附件,Add,attachmentPath);
//}
//}
//}

invoke(mail,Display/ *或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();
返回结果;
}
返回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 (值));


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

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

我最后评论了附件部分,第一个OLE命令,因为对我而言,它也没有它。它不会损坏使用它,但也许你需要它。只是试一试。



为什么我发表标题From的原因是它没有效果。要更改发件人,您可能需要另一个代码段来切换Outlook配置文件或配置文件切换器中的几个预配置的发件人。默认情况下,只会使用您的默认配置文件。



告诉我是否有帮助。


I want to create an E-Mail with a Java Application using Outlook and the OLE Client.

I searched for examples and found quite a few. They all start the same way:

Create the Display, the Shell, the OLE Frame and the OLE Client Site.

But I get an error with these few steps:

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

I get the following Error:

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)

I don't know OLE and I'm not sure what I am doing wrong. Are there some dependencies I'm missing? Does somebody know what this error could be? I googled for the Error code but didn't find anything.

EDIT

Well if nobody knows why OLE doesn't work for me I've got another question. Is it possible, or is there a Library, to create an Outlook E-Mail and set it up (subject, body etc) but not send it but make it visible for the User to change things?

EDIT 2

The x86 and x64 jar files didn't work out, same error. Also I got the newest versions of SWT for x86 and x64. OS is x64 and java, too, so I can't use the x86 SWT libraries. With the x64 the error above occurs. The Outlook version is 15 (Outlook 2013).

Hopefully this helps?

I got the creation of E-Mail to work via Processbuilder but only with the mailto: parameter. The problem here is the folllowing:

  • I want to track the status of the Process. I want to know when the E-Mail is closed/send whatsoever.
  • I want to insert a picture (BufferedImage) out of the Clipboard into the Body, which is simply impossible with the mailto argument.

解决方案

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.

这篇关于如何在Outlook中创建电子邮件并使其可见的用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆