How to create an E-Mail in Outlook and make it visible for the User

后端 未结 4 1414
情歌与酒
情歌与酒 2021-01-11 16:06

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:

相关标签:
4条回答
  • 2021-01-11 16:14

    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.

    0 讨论(0)
  • 2021-01-11 16:22

    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.

    0 讨论(0)
  • 2021-01-11 16:26

    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

    0 讨论(0)
  • 2021-01-11 16:28

    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.

    0 讨论(0)
提交回复
热议问题