Jacob Com Outlook使用Dispatch.call发送邮件

时间:2018-11-20 18:15:19

标签: java eclipse outlook jacob

嘿,我正在尝试使用JACOB COM activex代码从Java发送Outlook电子邮件。

这是我目前的代码:

import com.ibm.rational.test.lt.kernel.services.ITestExecutionServices;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;    
import java.util.HashMap;
import java.util.Map;

public class MailOut implements com.ibm.rational.test.lt.kernel.custom.ICustomCode2 {
    Map<String, Object> params = new HashMap<String, Object>();
    String attachment[] = new String[1];
    String to[] = new String[1];

    public MailOut() {
    }

    public String exec(ITestExecutionServices tes, String[] args) {
        //---------------------------------------------------------------------
        params.put("subject", "Test subject");
        params.put("body", "Please see attached.");

        attachment[0] = "C:\\temp\\about_blank.pdf";
        params.put("attachments", attachment);

        to[0] = "me@here.com";
        params.put("to", to);

        OutlookJACOB mail = new OutlookJACOB();
        mail.createEmail(params);
        //---------------------------------------------------------------------

        return "";
    }

    public class OutlookJACOB
    {
        private ActiveXComponent ol;
        private Dispatch outlook;
        private Object mapi[] = new Object[1];
        private Object email[] = new Object[1];

        public OutlookJACOB()
        {
            mapi[0] = "MAPI";
            email[0] = 0;

            ol = new ActiveXComponent("Outlook.Application");
            //ol.setProperty("Visible", new Variant(true));
            outlook = ol.getObject();
            Dispatch.call(outlook,"GetNamespace",mapi).toDispatch();
        }

        public void createEmail(Map<String, Object> params)
        {
            Dispatch mail = Dispatch.call(outlook,"CreateItem",email).toDispatch();
            Dispatch.put(mail, "Subject", params.get("subject"));
            Dispatch.put(mail, "HTMLBody", params.get("body"));

            String to[] = (String[]) params.get("to");
            String attachments[] = (String[]) params.get("attachments");

            if(to != null)
            {
                if(to.length>0)
                {
                    String _to = "";

                    for(Object t : to)
                    {
                        _to += t + ";";
                    }

                    Dispatch.put(mail, "To", _to);
                }
            }

            if(attachments != null)
            {
                if(attachments.length>0)
                {
                    Dispatch attachs = Dispatch.get(mail, "Attachments").toDispatch();

                    for(Object attachment : attachments)
                    {
                        Dispatch.call(attachs, "Add", attachment);
                    }
                }
            }

            //Dispatch.call(mail, "Send");
            try {
                Dispatch.call(mail, "Send");  
            } catch (com.jacob.com.ComFailException e) {  
                e.printStackTrace();  
            }  

        }
    }
}

该行的错误是此行:

Dispatch.call(mail, "Send"); 

和错误状态:

com.jacob.com.ComFailException:
A COM exception has been encountered:
At Invoke of: Send
Description: 80004004 / Operation aborted

现在我知道我的代码是正确的,因为如果这样做:

Dispatch.call(mail, "Save");

然后将确实创建的电子邮件放入Outlook中的“草稿”文件夹中: enter image description here

那么,在吞噬狼吞吞中,为了发送它而不保存它,我在这里错过了什么?

1 个答案:

答案 0 :(得分:0)

您可以参考以下代码:

ActiveXComponent oOutlook = new ActiveXComponent("Outlook.Application");
        try { 
            DispatchEvents events = new DispatchEvents( oOutlook,
                    new InvocationProxy() {
                        public Variant invoke(String name, Variant[] args) {
                            _log.debug("Outlook got a "+name);
                            return null;
                        }
                    }
            );
        } catch (ComException cex) { 
            System.err.println("HR: " + Integer.toHexString(cex.getHResult())); 
            cex.printStackTrace();
        }
        Dispatch mail = Dispatch.invoke( oOutlook.getObject(), "CreateItem", Dispatch.Get, new Object[] { "0" }, new int[0]).toDispatch();

        Dispatch.put(mail, "To", email );
        Dispatch.put(mail, "Cc", cc );
        Dispatch.put(mail, "Subject", "I am broke" );
        Dispatch.put(mail, "Body", "Dear Beloved, Please send me cash.  I am King of Nigeria.  Thanks.");
        Dispatch.put(mail, "ReadReceiptRequested", "true" );

        Dispatch attachments = Dispatch.get(mail, "Attachments").toDispatch();
        Dispatch.call(attachments, "Add", attachment );
        Dispatch.call(mail,"Send");
        oOutlook.safeRelease();
        ComThread.Release();

有关更多信息,请参考此链接:

JACOB - Java COM Bridge