none
Delphi and sending emails from a specific Outlook account RRS feed

  • Question

  • I currently have an application, written in Delphi which generates emails in Outlook without any problems. I am using Delphi 7, which references the Outlook8 unit. Recently a client of mine requested that when the email is created, can it default to a specific Outlook account. Having made searches on the internet, it appears that when using any version of Outlook prior to version 2007 it is not possible to create an email and specify the account you want to send it from. I've also found that within Outlook 2007 and newer that there is a property called SendUsingAccount.

    I've also discovered that there is a way of setting the account through using the Inspector property.

    1.  Do you know of a way I can set the account using Inspector for any version of Outlook prior to Outlook 2007, if so do you have an example?

    2.  If I want to integrate the properties of Outlook 2007 and newer into my application, how would I do this? I can find on the internet units for Outlook8.pas and Outlook2000.pas but nothing for Outlook2007.

    I'd appreciate any help you could give to enable me to send emails from a specific account.

    My current code to send emails via Outlook looks like this:

    procedure EmailViaOutlook(sRecipient, sSubject, sBody: string; sAttachment: Tstrings);
    var
     Outlook   : OutlookApplication;
     Unknown   : IUnknown;
     Result   : HResult;
     MI    : MailItem;
      bOutlookRunning: boolean;
      i: integer;
    const
     olMailItem = 0;
    begin
     // via early binding:
     Result := GetActiveObject(CLASS_OutlookApplication, nil, Unknown);

     

      {Check if Outlook is already running, this uses Outlook 2000}
      if FindWindow('mspim_wnd32','Microsoft Outlook') <> 0 then
        bOutlookRunning := true
      else
        bOutlookRunning := false;

     

     if Result = MK_E_UNAVAILABLE then
      begin
        Outlook := CoOutlookApplication.Create;
        end
     else begin
      // make sure no other error occurred during GetActiveObject
      OleCheck(Result);
      OleCheck(Unknown.QueryInterface(OutlookApplication, Outlook));
     end;

     

     MI := Outlook.CreateItem(olMailItem) as MailItem;

      if trim(sRecipient) <> '' then
        MI.To_ := sRecipient;

     

     for i := 0 to pred(sAttachment.count) do
        MI.Attachments.Add(sAttachment[i],EmptyParam, EmptyParam, EmptyParam);

     

     MI.Subject := sSubject;
     MI.Body := sBody;
      try
        MI.Display(EmptyParam);
      except
      end;


      if not(bOutlookRunning) then
        begin
         Showmessage('Please click OK when the mail has been sent.');
          Outlook.Quit;
          Outlook := nil;
        end;
    end;

    Monday, May 14, 2012 3:19 PM

Answers

All replies

  • 1. Can't do that using the Outlook Object Model - you can try to use Extended MAPI or Redempion: http://www.dimastr.com/redemption/faq.htm#14

    2. You will need to import the appropriate type library - click Project | Import Type Library in the Delphi IDE.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.3 is now available!

    Monday, May 14, 2012 6:42 PM
  • Thanks

    I've got the Outlook 2007 and Outlook 2010 wrappers but these are .pas files (Outlook12_TLB.pas and Outlook14_TLB.pas), how can these files be converted into files which can be imported as a Type library?

    Tuesday, May 15, 2012 8:48 AM
  • You already have the header (PAS) files that I sent you. Why do you need anything else to use Outlook Object Model in your app? Have you tried to use them?


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.3 is now available!

    Tuesday, May 15, 2012 1:46 PM
  • In your reply you said:

    2. You will need to import the appropriate type library - click Project | Import Type Library in the Delphi IDE.

    When I do this and go to Add, the only file types available are .tlb, .olb, .dll, .ocx, .exe ..... that is why I asked whether I need to do something with the PAS files?!

    I guess I'm misunderstanding what I need do with the PAS files you sent me.

    Tuesday, May 15, 2012 6:27 PM
  • If you look at the Outlook_TLB.pas file, you will see that it was imported from MSOUTL.OLB

    // Type Lib: C:\Program Files\Microsoft Office\Office14\MSOUTL.OLB (1)

    Browse to that file when clicking "Project | Import Type Library". Once you do that, you w ill have Outlook_TLB.pas, which I already sent you.

    Why can't you just add that PAS file to a "uses" clause in your application and use the Outlook interfaces defined there?

    I really do not understand what problem you are trying to solve...


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.3 is now available!

    Tuesday, May 15, 2012 8:48 PM
  • Not having used or understood what Import Type Library is  I didn't understand what you was telling me to do.

    I now realise that you generated the Outlook12_TLB.pas and Outlook14_TLB.pas files using this method.

    I added the Outlook12_TLB.pas file to the uses clause in my application and on compiling got the error that it couldn't find the Office12_TLB.dcu file.

    I then generated the Office12_TLB.pas file myself and added it to my application but now get the error Outlook12_TLB.pas "Invalid Compiler directive 'RTTI'

    Any ideas?? Thanks

    Wednesday, May 16, 2012 12:22 PM
  • What is the relevant snippet of your code that causes that compiler error?

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.3 is now available!

    Wednesday, May 16, 2012 1:42 PM
  • In the Outlook12_TLB.pas file:

    uses Windows, ActiveX, Classes, Graphics, Office12_TLB;
    
    {$RTTI EXPLICIT METHODS([]) FIELDS([]) PROPERTIES([])}
    {$WEAKLINKRTTI ON}
    

    The error is on the line begining {$RTTI ..................
    
    
    
    
    
    
    
    Wednesday, May 16, 2012 2:55 PM
  • I created these in Delphi XE2. If you are using Delphi 7, just delete the offending compiler directives.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.3 is now available!

    Wednesday, May 16, 2012 2:57 PM
  • Sorry to be a pain, but I removed the compiler directives as you suggested, now when I compile my application the procedure code listed above does not compile. This code worked originally to create the mail item and to set up the recipient, body, attachment and to then display the email before sending. 

    The following section gives an error:

     if Result = MK_E_UNAVAILABLE then
       begin
         Outlook := CoOutlookApplication.Create;
         end
      else begin
       // make sure no other error occurred during GetActiveObject
       OleCheck(Result);
       OleCheck(Unknown.QueryInterface(OutlookApplication, Outlook));
      end;
     
    
    

    The line "Outlook := CoOutApplication.Create;" gives the compile error

    Incompatible types: '_DApplication' and '_Application'

    Thanks

    
    Wednesday, May 16, 2012 3:25 PM
  • Change that line to

    Outlook := CoOutlookApplication.Create as _DApplication;

    Why are you using a dispinterface ?


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.3 is now available!

    Wednesday, May 16, 2012 3:42 PM
  • I don't know, this code was written some time ago before I joined the company and it's worked so never needed to change ... until now!

    Can you suggest alternative code to do the same thing?

    Wednesday, May 16, 2012 3:56 PM
  • Have you tried to make the change?

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.3 is now available!

    Wednesday, May 16, 2012 4:10 PM
  • I've made the change although it still didn't compile so I changed to the line below

    Outlook := CoOutlookApplication.Create as _Application;

    Now compiles OK! I just need to now add a line into the procedure to set the SendUsingAccount property.

    Thanks for your help.

    Wednesday, May 16, 2012 4:29 PM