11

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.
Loki
  • 4,065
  • 4
  • 29
  • 51
  • Is your goal sending an email you user, so that user find that email in his mailbox ? If yes then use JavaMail API. – Sachin Thapa Aug 16 '13 at 05:14
  • I want to create the E-Mail for the User to see and edit. Not for him in his mailbox. It should just open a new Mail with some fields filled in. The User himself can change everything and then click on send. And I don't have access to the SMTP of the Exchange Server. That's why I cant use JavaMail. – Loki Aug 16 '13 at 06:30
  • You can use `java.awt.Desktop.mail(URI)' to launch the mail composing window of the user's default mail client. See http://stackoverflow.com/a/2357924/1369991 for more. – Andreas Mayer May 20 '14 at 14:44
  • I know this one is old, but still listed as unanswered. Would you please accept and upvote my answer if it seems appropriate? Thanks. – kriegaex Jun 09 '14 at 12:41
  • I am sorry to say this, but your answer helped me not at all. It's still unanswered, and I found a workaround, though, I'm not working on this project anymore. So sorry, but as your answer is not what I was looking for I'll not accept it and won't upvote it. – Loki Jul 14 '14 at 10:42
  • @Loki: I am facing the same issue(result = -2147221164). Did you find a solution? I am using 64bit Windows system, 64bit JVM and 64bit SWT. I have also referred other stackoverflow questions and eclipse bug reports but found no help... The problem is I need to add html tables and open data in new email tab on outlook.. http://stackoverflow.com/questions/6045816/to-open-outlook-mail-from-java-program-and-to-attach-file-to-the-mail-from-direc – razor Oct 07 '15 at 07:16
  • @Loki: Sorry..it seems u have moved on from this project.. read your last comment just now.. – razor Oct 07 '15 at 07:18
  • MS Outlook is 32 bit.. – razor Oct 07 '15 at 07:40

4 Answers4

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.

kriegaex
  • 63,017
  • 15
  • 111
  • 202
  • Point 2 and 3 are correct ;). I want to give the user some kind of template where he can edit in some text. And in my company we use Outlook so thats why it has to be Outlook. Do I need to create this Eclipse RCP Project to get OLE to work, like in the Vogella Tutorial? – Loki Aug 12 '13 at 06:07
  • No, RCP is not necessary. I created a normal Java project, only downloaded the SWT library (JAR file). BTW, it was my very first try with SWT classes and OLE from Java, so if I managed to do it it must have been dead simple. ;) – kriegaex Aug 12 '13 at 14:22
  • Well I downloaded it, too. And used the library. I have x64. But nothing works. Can you post me your downloadlink? Maybe I have a wrong version. – Loki Aug 12 '13 at 14:58
  • Sorry, I was on the road. I have updated my answer now with download instructions and sample code. If OLE does not work for you, probably you need to fix your setup, not the Java code. But this is just an assumption. Please also update your question with details about OS, Java version, SWT version, Outlook version. Maybe you have a problem there. – kriegaex Aug 14 '13 at 09:38
  • Doesn't work for me. Well I had the problem with the x64 and x86 Files of some dlls I used on a project. I'm trying now to create the .jars, even if the code gives me the error. Maybe it just works as jar. When I try to run the x86 version I get the error, but I think it could work if it's a .jar file using the x86 SWT version. – Loki Aug 14 '13 at 16:28
  • I am on 32-bit WinXP and Outlook 2007. Maybe there is a bug or an incompatibility on 64-bit and/or x64. Try to google up similar problems or ask on an SWT support channel (forum, mailing list etc.). – kriegaex Aug 14 '13 at 18:33
  • I'm on x64 Win 8 Outlook 2013 and i get `Failed to create Ole Client. result = -2147221164` – Moritz Roessler Aug 16 '13 at 11:47
  • Is your Eclipse and/or JDK/JRE also 64-bit? Someone mentions a possible conflict at https://bugs.eclipse.org/bugs/show_bug.cgi?id=371124. – kriegaex Aug 17 '13 at 09:34
  • @kriegaex Yep, at least it says so if i try using the 32 bit swt library – Moritz Roessler Aug 19 '13 at 09:32
0

if you are using something with web, this can help you:

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

but in a application you can start a process mailto:

like

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

it will work with all e-mail clients

rodolfoprado
  • 126
  • 7
  • it answers the question "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?" – rodolfoprado Aug 13 '13 at 16:58
0

com,

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

with the above code Outlook mail is opened with predefined mailto, Subject and Body of the mail, could you please explain me how can we Add the address in CC also.

VSanka
  • 85
  • 2
  • 9
0

Your MS Outlook might be 32-bit (x86). So a 64-bit (x64) SWT cannot start the outlook. You need to use 32-bit SWT Jar File which will not run on 64-bit JVM. So you need to install 32-Bit JVM (JRE).

Even if you are running 64-bit Windows, you can still download and install the 32-bit (x86) JRE and run your application.

mrchidam
  • 194
  • 7