Borland DelphiŪ Programming Tips

Tip 1 - Sending an email message via Outlook2002 from Delphi 5/6

  1. Load the ActiveX controls for MAPI 6.0 into the Delphi IDE
  2. Drag both the MAPISession and MAPIMessages controls to the form to be used to send mail. In the example, a Database-based form, with a textbox containing an email address is used.
  3. Change the value of the MAPISession property "DownLoadMail" to "False". If left at the default "True" value, will download mail from the server to the Outlook client, a process that is irrelevant to this application.
  4. Change the MAPISession property "LogonUI" to "False". If left at "True", the user will be prompted to log in each time the routine is invoked.
  5. In the example below, the MAPISession object is named "MS", and the MAPIMessages object is named "MM".
  6. In the event to be used to send the email (in this case a button-click event) use the following code or a reasonable facsimile:

    procedure TMainDlg.BitBtn12Click(Sender: TObject);
    begin
    //MS.NewSession := True; //Use if you wish to force a new session with each execution
    MS.SignOn; // Connects to an existing instance of Outlook
    If MS.SessionID <> 0 then //If the session is valid....
    Begin
    With MM Do
    Begin
    SessionID := MS.SessionID; //This assigns the Session ID acquired above to the message
    Compose; //Opens a buffer to store the message information
    RecipDisplayName := DBEdit74.Text; //In this example, the email address is from a text box
    MsgSubject := 'Test Message'; //Subject of the message is hard-coded in this case
    If RecipDisplayName <> '' then //Makes sure that a non-blank address has been provided
    Send(True) //The "True" parameter tells Outlook to display the New Message form for entry
    else
    MessageDlg('Message Failed, no recipient address.', mtInformation,
    [mbOk], 0);
    End;
    End
    Else
    MessageDlg('Outlook not started or not installed. Can''t send messages.', mtInformation,
    [mbOk], 0);

    MS.SignOff //Closes the session opened above (DOES NOT CLOSE OUTLOOK)

    end;
     

Tip 2 - An easier way to do practically the same thing

  1. Use the shellexecute function to call the mailto: protocol which should already be set up on all Windows computers:
     ToAddr := DBEdit62.Text;

    (add WinAPI to the "uses" section for the unit)

    if ShellExecute(0, 'open', PChar('mailto:'+ToAddr+'?Subject=Camp%20Beale'), '', '', SW_SHOWNORMAL) <= 32
    then ShowMessage('Send of e-mail message failed.');

Tip 3 - What to do when you get the message "File not found: 'System.pas'".

  1. Go to Project | Options, select tab "Directories/Conditionals" and add the Borland LIB file path to the "Search path". In my case (typical) is was "C:\Program Files\Borland\Delphi6\Lib".
  2. Click "OK".
  3. Save.
  4. Re-compile.