How to create email in Outlook and make it visible to the user

I want to create E-Mail with Java application using Outlook and OLE client.

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

Create a screen, skin, OLE frame, and OLE client site.

But I am getting 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 am getting 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 any dependencies I am missing? Does anyone know what this error is? I googled for the error code but couldn't find anything.

EDIT

Well, if no one knows why OLE isn't working for me, I have another question. Is it possible, or is there a library, to create an Outlook email and customize it (subject, body, etc.), but not send it, but make it visible to the user in order to change something?

EDIT 2

X86 and x64 jar files didn't work, 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 x86 SWT libraries. An error occurs with x64. Outlook version is 15 (Outlook 2013).

Hope this helps?

I got an E-Mail creation to work through Processbuilder, but only with the mailto: parameter. The problem is as follows:

  • I want to track the status of a process. I want to know when the email is closed / sent at all.
  • I want to insert an image (BufferedImage) from the clipboard into the body, which is simply not possible with the mailto argument.
+11


source to share


4 answers


For me it works well according to the tutorial at vogella.com . I also tried your minimal code sample and got no errors while building the OLE client. By the way, I used SWT 4.3.

A bit off-topic, but should it be Outlook? What I mean is, you just want to automate the sending of email - you can use JavaMail and do it without a headgear, that is, without automating the real GUI client. The only reasons I can imagine wanting to use Outlook or another email client are:

  • You need to post the posted message to your source code for reference.
  • Outlook is connected to an Exchange server that is configured to not accept SMTP connections used by JavaMail.
  • You might want to compose a basic message and show it to the user so she can add attachments or edit text interactively before sending.

But if it's just the automation of sending email, as I said, I would recommend JavaMail.


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



My example code looks like this and works 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 some of the attachments at the end as well as the first OLE command because for me it also works without it. It doesn't do any harm, but maybe you need it. Just try it.

The reason I commented out the "From" title bar is because it has no effect. To change the sender, you probably need another piece of code to switch the Outlook profile, or within the profile switch of multiple preconfigured senders. By default, it just uses your default profile.

Tell me if it helps.

+3


source


com,

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

      



with the above code. Outlook Mail is open with predefined mailto, Subject and Mail Body, could you please explain to me how we can add an address to CC.

0


source


Your MS Outlook can be 32-bit (x86). So 64-bit (x64) SWT cannot run Outlook. You need to use a 32 bit SWT Jar, which will not work on a 64 bit JVM. Therefore, you need to install 32 bit JVM (JRE).

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

0


source


If you are using something online, this might 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 the app you can start the mailto process:

like

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

      

it will work with all email clients

-1


source







All Articles