Search code examples
delphioutlookole

Delphi - Adding BCC & CC Recipients to OLE Outlook object


The answer to the post " How is working with Outlook in Delphi different than other email clients? works great. See below.

Using this example how would you go about adding CC and BCC recipients?

USES OleCtrls, ComObj;

procedure TForm1.Button1Click(Sender: TObject);
const
  olMailItem = 0;
var
  Outlook: OLEVariant;
  MailItem: Variant;
  MailInspector : Variant;
  stringlist : TStringList;
begin
  try
   Outlook:=GetActiveOleObject('Outlook.Application') ;
  except
   Outlook:=CreateOleObject('Outlook.Application') ;
  end;
  try
    Stringlist := TStringList.Create;
    MailItem := Outlook.CreateItem(olMailItem) ;
    MailItem.Subject := 'subject here';
    MailItem.Recipients.Add('someone@yahoo.com');
    MailItem.Attachments.Add('c:\boot.ini');
    Stringlist := TStringList.Create;
    StringList.Add('body here');
    MailItem.Body := StringList.text;
    MailInspector := MailItem.GetInspector;
   MailInspector.display(true); //true means modal
 finally
    Outlook := Unassigned;
    StringList.Free;
  end;
end;

Solution

  • The Add() method of the Recipients collection creates and returns a new Recipient object. The Type property of the Recipient class allows to set an integer representing the type of recipient. For MailItem recipients, it can be one of the following OlMailRecipientType constants: olBCC, olCC, olOriginator, or olTo. The default Type for a new mail recipient is olTo.

    MailItem.Recipients.Add('someone@yahoo.com'); // Type=1 olTo
    MailItem.Recipients.Add('joesmoe@yahoo.com').Type := 2; // olCC
    MailItem.Recipients.Add('alice@yahoo.com').Type := 3; // olBCC
    

    You may find the How To: Fill TO,CC and BCC fields in Outlook programmatically article helpful.