How to: Outlook Attachments (see code)

Help
csk
2006-11-19
2014-01-10
  • csk
    csk
    2006-11-19

    Hello,

    I like to send an attachment with Outlook. I made the following code, but it doesn't work right.
    Can anyone help me? What is wrong?
    Thank you very much
    Christa

    ActiveXComponent outlook = new ActiveXComponent("Outlook.Application");
        Dispatch new_app = Dispatch.invoke(outlook.getObject(),
                                           "CreateItem",
                                           Dispatch.Get,
                                           new Object[] {"0"}
                                           , new int[0]).toDispatch();

        Dispatch.put(new_app, "To", "bla@aol.de");
        Dispatch.put(new_app, "Subject", "Testmail");
        Dispatch.put(new_app, "Body", "Text aus Editor");
        Dispatch.put(new_app, "ReadReceiptRequested", "false");

          Dispatch attachs = Dispatch.get(new_app,"Attachments").toDispatch();
          Dispatch.put(attachs,"Add","c:/index.php");
          Dispatch.put(new_app, "Attachments", attachs);

        Dispatch.call(new_app, "Send");

     
    • Chuck Williams
      Chuck Williams
      2006-12-21

      Here's how... you were very close!

                      Dispatch mail =
                  Dispatch.invoke(axOutlook.getObject(), "CreateItem", Dispatch.Get,
                                       new Object[] { "0" }, new int[0]).toDispatch();

              Dispatch.put(mail, "To", "foo@bar.com");
              Dispatch.put(mail, "Subject", "Testing");

              String body = "testing"
              Dispatch.put(mail, "Body", body);
              Dispatch.put(mail, "ReadReceiptRequested", "false");

              Dispatch attachs = Dispatch.get(mail, "Attachments").toDispatch();
              Dispatch.call(attachs, "Add", "filename.ext", 4);
              Dispatch.call(mail, "Send");

       
      • Beau Tremblay
        Beau Tremblay
        2014-01-10

        When I used this solution, this call:

        Dispatch.call(attachs, "Add", "filename.ext", 4);

        resulted in attaching the files as shortcuts. This works as expected:

        Dispatch.call(attachs, "Add", "filename.ext");

         
  • nodelphi
    nodelphi
    2011-02-18

    Thank you very much! Its really help.

     

  • Anonymous
    2011-03-11

    With Outlook 2007 i got a security warning not to send unsecure attachments

    what can i do against this?
    I want so send a txt or pdf File.