tags:

views:

71

answers:

0

I am trying to attach an image to an email via Microsoft Outlook's COM interface and the com4j library:

_Application application = ClassFactory.createApplication();

_MailItem mailItem = application.createItem(OlItemType.olMailItem).queryInterface(_MailItem.class);

Attachments attachments = mailItem.attachments();
attachments.add("C:\\kitten.jpg", "image/jpeg", "1", "test");

Unfortunately, the last line results in an error:

Exception in thread "main" com4j.ComException: 80020005 Type mismatch. : Type mismatch. : .\invoke.cpp:460
    at com4j.Wrapper.invoke(Wrapper.java:122)
    at $Proxy11.add(Unknown Source)
    at test.TestApplication.main(TestApplication.java:20)
Caused by: com4j.ComException: 80020005 Type mismatch. : Type mismatch. : .\invoke.cpp:460
    at com4j.Native.invoke(Native Method)
    at com4j.StandardComMethod.invoke(StandardComMethod.java:95)
    at com4j.Wrapper$InvocationThunk.call(Wrapper.java:258)
    at com4j.Task.invoke(Task.java:44)
    at com4j.ComThread.run0(ComThread.java:149)
    at com4j.ComThread.run(ComThread.java:125)

I have confirmed that the file does exist at that location.

As per the tutorial, I had generated the Java interfaces using Outlook's type library:

java
  -jar tlbimp.jar
  -o outlook
  -p outlook
  "C:\Program Files\Microsoft Office\Office12\MSOUTL.OLB"

The command generated this source code for the Attachments interface:

@IID("{0006303C-0000-0000-C000-000000000046}")
public interface Attachments extends Com4jObject {
    @VTID(7)
    outlook._Application application();

    @VTID(8)
    outlook.OlObjectClass _class();

    @VTID(9)
    outlook._NameSpace session();

    @VTID(10)
    @ReturnValue(type=NativeType.Dispatch)
    com4j.Com4jObject parent();

    @VTID(11)
    int count();

    @VTID(12)
    outlook.Attachment item(
        @MarshalAs(NativeType.VARIANT) java.lang.Object index);

    @VTID(13)
    outlook.Attachment add(
        @MarshalAs(NativeType.VARIANT) java.lang.Object source,
        @MarshalAs(NativeType.VARIANT) java.lang.Object type,
        @MarshalAs(NativeType.VARIANT) java.lang.Object position,
        @MarshalAs(NativeType.VARIANT) java.lang.Object displayName);

    @VTID(14)
    void remove(
        int index);

}

I have tried the following with no success:

  • Changing the java.lang.Object parameter types to java.lang.String
  • Deleting the @MarshalAs(NativeType.VARIANT) annotation